zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
inet.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * This file is part of zmqpp.
7  * Copyright (c) 2011-2015 Contributors as noted in the AUTHORS file.
8  */
9 
17 #ifndef ZMQPP_INET_HPP_
18 #define ZMQPP_INET_HPP_
19 
20 #include <utility>
21 #include <cassert>
22 #include <cstdint>
23 
25 // We get htons and htonl from here
26 #ifdef _WIN32
27 #include <WinSock2.h>
28 #else
29 #include <netinet/in.h>
30 #endif
31 
32 #include "compatibility.hpp"
33 
34 namespace zmqpp
35 {
36 
43 ZMQPP_COMPARABLE_ENUM order {
44  big_endian,
46 };
47 
62 inline uint64_t swap_if_needed(uint64_t const value_to_check)
63 {
64  static order host_order = (htonl(42) == 42) ? order::big_endian : order::little_endian;
65 
66  if (order::big_endian == host_order)
67  {
68  return value_to_check;
69  }
70 
71  union {
72  uint64_t integer;
73  uint8_t bytes[8];
74  } value { value_to_check };
75 
76  std::swap(value.bytes[0], value.bytes[7]);
77  std::swap(value.bytes[1], value.bytes[6]);
78  std::swap(value.bytes[2], value.bytes[5]);
79  std::swap(value.bytes[3], value.bytes[4]);
80 
81  return value.integer;
82 }
83 
93 #ifndef htonll
94 inline uint64_t htonll(uint64_t const hostlonglong)
95 {
96  return zmqpp::swap_if_needed(hostlonglong);
97 }
98 #endif
99 
109 #ifndef ntohll
110 inline uint64_t ntohll(uint64_t const networklonglong)
111 {
112  return zmqpp::swap_if_needed(networklonglong);
113 }
114 #endif
115 
122 inline float htonf(float value)
123 {
124  assert(sizeof(float) == sizeof(uint32_t));
125 
126  uint32_t temp;
127  memcpy(&temp, &value, sizeof(uint32_t));
128  temp = htonl( temp );
129  memcpy(&value, &temp, sizeof(uint32_t));
130 
131  return value;
132 }
133 
140 inline float ntohf(float value)
141 {
142  assert(sizeof(float) == sizeof(uint32_t));
143 
144  uint32_t temp;
145  memcpy(&temp, &value, sizeof(uint32_t));
146  temp = ntohl( temp );
147  memcpy(&value, &temp, sizeof(uint32_t));
148 
149  return value;
150 }
151 
158 inline double htond(double value)
159 {
160  assert(sizeof(double) == sizeof(uint64_t));
161 
162  uint64_t temp;
163  memcpy(&temp, &value, sizeof(uint64_t));
164  temp = htonll(temp);
165  memcpy(&value, &temp, sizeof(uint64_t));
166 
167  return value;
168 }
169 
176 inline double ntohd(double value)
177 {
178  assert(sizeof(double) == sizeof(uint64_t));
179 
180  uint64_t temp;
181  memcpy(&temp, &value, sizeof(uint64_t));
182  temp = ntohll(temp);
183  memcpy(&value, &temp, sizeof(uint64_t));
184 
185  return value;
186 }
187 
188 }
189 
190 #endif /* INET_HPP_ */
zmqpp::ntohd
double ntohd(double value)
Definition: inet.hpp:176
zmqpp::ntohll
uint64_t ntohll(uint64_t const networklonglong)
Definition: inet.hpp:110
zmqpp
C++ wrapper around zmq.
Definition: actor.cpp:30
zmqpp::swap_if_needed
uint64_t swap_if_needed(uint64_t const value_to_check)
Definition: inet.hpp:62
zmqpp::order::big_endian
@ big_endian
zmqpp::htonll
uint64_t htonll(uint64_t const hostlonglong)
Definition: inet.hpp:94
compatibility.hpp
zmqpp::ntohf
float ntohf(float value)
Definition: inet.hpp:140
zmqpp::order
order
Possible byte order types.
Definition: inet.hpp:43
zmqpp::htonf
float htonf(float value)
Definition: inet.hpp:122
zmqpp::htond
double htond(double value)
Definition: inet.hpp:158