zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 <cstdint>
21 
23 // We get htons and htonl from here
24 #ifdef _WIN32
25 #include <WinSock2.h>
26 #else
27 #include <netinet/in.h>
28 #endif
29 
30 #include "compatibility.hpp"
31 
32 namespace zmqpp
33 {
34 
41 ZMQPP_COMPARABLE_ENUM order {
42  big_endian,
44 };
45 
60 inline uint64_t swap_if_needed(uint64_t const value_to_check)
61 {
62  static order host_order = (htonl(42) == 42) ? order::big_endian : order::little_endian;
63 
64  if (order::big_endian == host_order)
65  {
66  return value_to_check;
67  }
68 
69  union {
70  uint64_t integer;
71  uint8_t bytes[8];
72  } value { value_to_check };
73 
74  std::swap(value.bytes[0], value.bytes[7]);
75  std::swap(value.bytes[1], value.bytes[6]);
76  std::swap(value.bytes[2], value.bytes[5]);
77  std::swap(value.bytes[3], value.bytes[4]);
78 
79  return value.integer;
80 }
81 
91 #ifndef htonll
92 inline uint64_t htonll(uint64_t const hostlonglong)
93 {
94  return zmqpp::swap_if_needed(hostlonglong);
95 }
96 #endif
97 
107 #ifndef ntohll
108 inline uint64_t ntohll(uint64_t const networklonglong)
109 {
110  return zmqpp::swap_if_needed(networklonglong);
111 }
112 #endif
113 
120 inline float htonf(float value)
121 {
122  assert(sizeof(float) == sizeof(uint32_t));
123 
124  uint32_t temp;
125  memcpy(&temp, &value, sizeof(uint32_t));
126  temp = htonl( temp );
127  memcpy(&value, &temp, sizeof(uint32_t));
128 
129  return value;
130 }
131 
138 inline float ntohf(float value)
139 {
140  assert(sizeof(float) == sizeof(uint32_t));
141 
142  uint32_t temp;
143  memcpy(&temp, &value, sizeof(uint32_t));
144  temp = ntohl( temp );
145  memcpy(&value, &temp, sizeof(uint32_t));
146 
147  return value;
148 }
149 
156 inline double htond(double value)
157 {
158  assert(sizeof(double) == sizeof(uint64_t));
159 
160  uint64_t temp;
161  memcpy(&temp, &value, sizeof(uint64_t));
162  temp = htonll(temp);
163  memcpy(&value, &temp, sizeof(uint64_t));
164 
165  return value;
166 }
167 
174 inline double ntohd(double value)
175 {
176  assert(sizeof(double) == sizeof(uint64_t));
177 
178  uint64_t temp;
179  memcpy(&temp, &value, sizeof(uint64_t));
180  temp = ntohll(temp);
181  memcpy(&value, &temp, sizeof(uint64_t));
182 
183  return value;
184 }
185 
186 }
187 
188 #endif /* INET_HPP_ */
float htonf(float value)
Definition: inet.hpp:120
C++ wrapper around zmq.
Definition: actor.cpp:29
double htond(double value)
Definition: inet.hpp:156
float ntohf(float value)
Definition: inet.hpp:138
uint64_t swap_if_needed(uint64_t const value_to_check)
Definition: inet.hpp:60
double ntohd(double value)
Definition: inet.hpp:174
uint64_t htonll(uint64_t const hostlonglong)
Definition: inet.hpp:92
order
Possible byte order types.
Definition: inet.hpp:41
uint64_t ntohll(uint64_t const networklonglong)
Definition: inet.hpp:108