ByteOrder.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  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_POSIX
00029 #include <netinet/in.h>
00030 #endif
00031 
00032 namespace ccxx {
00033 
00039 class COMMONCPP_API ByteOrder
00040 {
00041   public:
00042 
00044   inline static uint16_t hostToNetwork(uint16_t n) throw()
00045   { return(htons(n)); }
00046 
00048   inline static uint32_t hostToNetwork(uint32_t n) throw()
00049   { return(htonl(n)); }
00050 
00052   static uint64_t hostToNetwork(uint64_t n) throw();
00053 
00055   inline static uint16_t networkToHost(uint16_t n) throw()
00056   { return(ntohs(n)); }
00057 
00059   inline static uint32_t networkToHost(uint32_t n) throw()
00060   { return(ntohl(n)); }
00061 
00063   static uint64_t networkToHost(uint64_t n) throw();
00064 
00070   static bool isBigEndian() throw();
00071 
00077   inline static bool isLittleEndian() throw()
00078   { return(!isBigEndian()); }
00079 
00084   inline static Endianness getEndianness() throw()
00085   { return(isBigEndian() ? BigEndian : LittleEndian); }
00086 
00093   template<typename T> static T & reverseBytes(T &value) throw()
00094   {
00095     _reverseBytes(reinterpret_cast<byte_t *>(&value), sizeof(T));
00096     return(value);
00097   }
00098 
00099   private:
00100 
00101   static void _reverseBytes(byte_t *buf, size_t length) throw();
00102 
00103   ByteOrder(); // not supported
00104   CCXX_COPY_DECLS(ByteOrder);
00105 };
00106 
00107 }; // namespace ccxx
00108 
00109 #endif // __ccxx_ByteOrder_hxx
00110 
00111 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3