zmqpp 4.1.2
C++ bindings for 0mq (libzmq)
Loading...
Searching...
No Matches
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
34namespace zmqpp
35{
36
43ZMQPP_COMPARABLE_ENUM order {
46};
47
62inline 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
94inline uint64_t htonll(uint64_t const hostlonglong)
95{
96 return zmqpp::swap_if_needed(hostlonglong);
97}
98#endif
99
109#ifndef ntohll
110inline uint64_t ntohll(uint64_t const networklonglong)
111{
112 return zmqpp::swap_if_needed(networklonglong);
113}
114#endif
115
122inline 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
140inline 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
158inline 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
176inline 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_ */
C++ wrapper around zmq.
Definition: actor.cpp:30
order
Possible byte order types.
Definition: inet.hpp:43
uint64_t htonll(uint64_t const hostlonglong)
Definition: inet.hpp:94
float htonf(float value)
Definition: inet.hpp:122
uint64_t ntohll(uint64_t const networklonglong)
Definition: inet.hpp:110
float ntohf(float value)
Definition: inet.hpp:140
double ntohd(double value)
Definition: inet.hpp:176
double htond(double value)
Definition: inet.hpp:158
uint64_t swap_if_needed(uint64_t const value_to_check)
Definition: inet.hpp:62