ByteOrder.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_ByteOrder_hxx
00024 #define __ccxx_ByteOrder_hxx
00025
00026 #include <commonc++/Common.h++>
00027
00028 #ifdef CCXX_OS_WINDOWS
00029 #include <winsock2.h>
00030 #endif
00031
00032 #ifdef CCXX_OS_POSIX
00033 #include <netinet/in.h>
00034 #endif
00035
00036 namespace ccxx {
00037
00043 class COMMONCPP_API ByteOrder
00044 {
00045 public:
00046
00048 inline static uint16_t hostToNetwork(uint16_t n) throw()
00049 { return(htons(n)); }
00050
00052 inline static uint32_t hostToNetwork(uint32_t n) throw()
00053 { return(htonl(n)); }
00054
00056 static uint64_t hostToNetwork(uint64_t n) throw();
00057
00059 inline static uint16_t networkToHost(uint16_t n) throw()
00060 { return(ntohs(n)); }
00061
00063 inline static uint32_t networkToHost(uint32_t n) throw()
00064 { return(ntohl(n)); }
00065
00067 static uint64_t networkToHost(uint64_t n) throw();
00068
00074 static bool isBigEndian() throw();
00075
00081 inline static bool isLittleEndian() throw()
00082 { return(!isBigEndian()); }
00083
00088 inline static Endianness getEndianness() throw()
00089 { return(isBigEndian() ? BigEndian : LittleEndian); }
00090
00097 template<typename T> static T & reverseBytes(T &value) throw()
00098 {
00099 _reverseBytes(reinterpret_cast<byte_t *>(&value), sizeof(T));
00100 return(value);
00101 }
00102
00103 private:
00104
00105 static void _reverseBytes(byte_t *buf, size_t length) throw();
00106
00107 ByteOrder();
00108 CCXX_COPY_DECLS(ByteOrder);
00109 };
00110
00111 };
00112
00113 #endif // __ccxx_ByteOrder_hxx
00114
00115