ByteOrder.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2009  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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(); // not supported
00108   CCXX_COPY_DECLS(ByteOrder);
00109 };
00110 
00111 }; // namespace ccxx
00112 
00113 #endif // __ccxx_ByteOrder_hxx
00114 
00115 /* end of header file */

Generated on Sat Apr 17 23:03:05 2010 for libcommonc++ by  doxygen 1.5.9