uvcc
libuv C++ bindings
endian.hpp
1 
2 
3 #ifndef UVCC_ENDIAN__HPP
4 #define UVCC_ENDIAN__HPP
5 
6 #include <cstdint> // uint*_t
7 #ifdef _WIN32
8 #include <Winsock2.h> // htons() htonl() ntohs() ntohl()
9 #else
10 #include <endian.h> // htobe* be*toh
11 #endif
12 
13 
14 namespace uv
15 {
16 /*! \defgroup doxy_group__endian Byte order conversion
17  \ingroup doxy_group__utility
18  \details Templates of cross-platform (Windows/Unix-like systems) functions for byte order conversion between host and network byte encoding.
19  \sa For fairly complete version of C functions see e.g.: \n
20  https://gist.github.com/panzi/6856583 \n
21  https://github.com/blizzard4591/cmake-portable-endian */
22 //! \{
23 
24 //! \cond
25 #ifdef _WIN32
26 # define HOST_to_NET_16(x) ( ::htons((uint16_t)(x)) )
27 # define HOST_to_NET_32(x) ( ::htonl((uint32_t)(x)) )
28 # define NET_to_HOST_16(x) ( ::ntohs((uint16_t)(x)) )
29 # define NET_to_HOST_32(x) ( ::ntohl((uint32_t)(x)) )
30 # ifndef __BYTE_ORDER__
31 # error __BYTE_ORDER__ macro not defined
32 # endif
33 # if defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
34 # define HOST_to_NET_64(x) ( (uint64_t)::htonl((uint32_t)((uint64_t)(x) >> 32)) | ((uint64_t)::htonl((uint32_t)(x)) << 32) )
35 # define NET_to_HOST_64(x) ( (uint64_t)::ntohl((uint32_t)((uint64_t)(x) >> 32)) | ((uint64_t)::ntohl((uint32_t)(x)) << 32) )
36 # elif defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
37 # define HOST_to_NET_64(x) ( (uint64_t)(x) )
38 # define NET_to_HOST_64(x) ( (uint64_t)(x) )
39 # else
40 # error __BYTE_ORDER__ value not supported
41 # endif
42 #else
43 # define HOST_to_NET_16(x) ( htobe16((uint16_t)(x)) )
44 # define HOST_to_NET_32(x) ( htobe32((uint32_t)(x)) )
45 # define HOST_to_NET_64(x) ( htobe64((uint64_t)(x)) )
46 # define NET_to_HOST_16(x) ( be16toh((uint16_t)(x)) )
47 # define NET_to_HOST_32(x) ( be32toh((uint32_t)(x)) )
48 # define NET_to_HOST_64(x) ( be64toh((uint64_t)(x)) )
49 #endif
50 //! \endcond
51 
52 
53 template< typename _I_ > inline uint16_t hton16(const _I_ &_i) { return HOST_to_NET_16(_i); } /*!< \brief Cross-platform `htons()` */
54 template< typename _I_ > inline uint32_t hton32(const _I_ &_i) { return HOST_to_NET_32(_i); } /*!< \brief Cross-platform `htonl()` */
55 template< typename _I_ > inline uint64_t hton64(const _I_ &_i) { return HOST_to_NET_64(_i); } /*!< \brief Cross-platform `htonll()` */
56 
57 template< typename _I_ > inline uint16_t ntoh16(const _I_ &_i) { return NET_to_HOST_16(_i); } /*!< \brief Cross-platform `ntohs()` */
58 template< typename _I_ > inline uint32_t ntoh32(const _I_ &_i) { return NET_to_HOST_32(_i); } /*!< \brief Cross-platform `ntohl()` */
59 template< typename _I_ > inline uint64_t ntoh64(const _I_ &_i) { return NET_to_HOST_64(_i); } /*!< \brief Cross-platform `ntohll()` */
60 
61 
62 #undef HOST_to_NET_16
63 #undef HOST_to_NET_32
64 #undef HOST_to_NET_64
65 #undef NET_to_HOST_16
66 #undef NET_to_HOST_32
67 #undef NET_to_HOST_64
68 
69 //! \}
70 }
71 
72 #endif
Namespace for all uvcc definitions.
Definition: buffer.hpp:17
uint64_t hton64(const _I_ &_i)
Cross-platform htonll()
Definition: endian.hpp:55
uint16_t ntoh16(const _I_ &_i)
Cross-platform ntohs()
Definition: endian.hpp:57
uint64_t ntoh64(const _I_ &_i)
Cross-platform ntohll()
Definition: endian.hpp:59
uint16_t hton16(const _I_ &_i)
Cross-platform htons()
Definition: endian.hpp:53
uint32_t ntoh32(const _I_ &_i)
Cross-platform ntohl()
Definition: endian.hpp:58
uint32_t hton32(const _I_ &_i)
Cross-platform htonl()
Definition: endian.hpp:54