libcommonc++  0.7
ByteOrder.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_ByteOrder_hxx
24 #define __ccxx_ByteOrder_hxx
25 
26 #include <commonc++/Common.h++>
27 
28 #ifdef CCXX_OS_POSIX
29 #include <netinet/in.h>
30 #endif
31 
32 namespace ccxx {
33 
40 {
41  public:
42 
44  inline static uint16_t hostToNetwork(uint16_t n)
45  { return(htons(n)); }
46 
48  inline static uint32_t hostToNetwork(uint32_t n)
49  { return(htonl(n)); }
50 
52  static uint64_t hostToNetwork(uint64_t n);
53 
55  inline static uint16_t networkToHost(uint16_t n)
56  { return(ntohs(n)); }
57 
59  inline static uint32_t networkToHost(uint32_t n)
60  { return(ntohl(n)); }
61 
63  static uint64_t networkToHost(uint64_t n);
64 
71  static bool isBigEndian();
72 
79  inline static bool isLittleEndian()
80  { return(!isBigEndian()); }
81 
87  inline static Endianness getEndianness()
88  { return(isBigEndian() ? BigEndian : LittleEndian); }
89 
97  template<typename T> static T& reverseBytes(T& value)
98  {
99  _reverseBytes(reinterpret_cast<byte_t*>(&value), sizeof(T));
100  return(value);
101  }
102 
103  private:
104 
105  static void _reverseBytes(byte_t* buf, size_t length);
106 
107  ByteOrder(); // not supported
109 };
110 
111 } // namespace ccxx
112 
113 #endif // __ccxx_ByteOrder_hxx
static Endianness getEndianness()
Get the endianness of the host system.
Definition: ByteOrder.h++:87
static uint16_t networkToHost(uint16_t n)
Convert an unsigned 16-bit integer from network to host byte-order.
Definition: ByteOrder.h++:55
static bool isLittleEndian()
Determine if the host system is little-endian.
Definition: ByteOrder.h++:79
Big-endian (network byte order).
Definition: Common.h++:323
Byte-order conversion routines.
Definition: ByteOrder.h++:39
static uint32_t hostToNetwork(uint32_t n)
Convert an unsigned 32-bit integer from host to network byte-order.
Definition: ByteOrder.h++:48
static uint16_t hostToNetwork(uint16_t n)
Convert an unsigned 16-bit integer from host to network byte-order.
Definition: ByteOrder.h++:44
#define COMMONCPP_API
Definition: Common.h++:126
static T & reverseBytes(T &value)
Reverse the bytes in a value.
Definition: ByteOrder.h++:97
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
Endianness
Byte endianness.
Definition: Common.h++:321
Definition: AllocationMap.c++:25
Little-endian.
Definition: Common.h++:325
static uint32_t networkToHost(uint32_t n)
Convert an unsigned 32-bit integer from network to host byte-order.
Definition: ByteOrder.h++:59
unsigned char byte_t
An unsigned 8-bit value.
Definition: Integers.h++:68