cel_endian.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00004 //
00005 // (C) 2003-2006 Celartem Technology Inc. All rights reserved.
00006 //----------------------------------------------------------------------------
00007 
00008 #ifndef _cel_endian_h_
00009 #define _cel_endian_h_
00010 
00011 #include "cel_system.h"
00012 
00013 namespace Celartem
00014 {
00018     enum Endian
00019     {
00020         endianBig = 1234,   
00021         endianLittle = 4321, 
00022 
00025         endianHost = ENDIAN_HOST
00026     };
00027 
00035     template<class T> T swap(const T& n);
00036 
00037     // 'signed char' is special; we need independent definition for this.
00038     template<> signed char swap<signed char>(const signed char& num);
00039 
00040     template<> char swap<char>(const char& num);
00041     template<> short swap<short>(const short& num);
00042     template<> int swap<int>(const int& num);
00043     template<> long swap<long>(const long& num);
00044     template<> long long swap<long long>(const long long& num);
00045 
00046     template<> unsigned char swap<unsigned char>(const unsigned char& num);
00047     template<> unsigned short swap<unsigned short>(const unsigned short& num);
00048     template<> unsigned int swap<unsigned int>(const unsigned int& num);
00049     template<> unsigned long swap<unsigned long>(const unsigned long& num);
00050     template<> unsigned long long swap<unsigned long long>(const unsigned long long& num);
00051 
00052     template<> float swap<float>(const float& num);
00053     template<> double swap<double>(const double& num);
00054 
00062     template<class T> T toBig(const T& num)
00063     {
00064         return (endianHost == endianLittle) ? swap(num) : num;
00065     }
00066 
00074     template<class T> T toLittle(const T& num)
00075     {
00076         return (endianHost == endianBig) ? swap(num) : num;
00077     }
00078 
00087     template<class T> T fromBig(const T& num)
00088     {
00089         return (endianHost == endianLittle) ? swap(num) : num;
00090     }
00091 
00100     template<class T> T fromLittle(const T& num)
00101     {
00102         return (endianHost == endianBig) ? swap(num) : num;
00103     }
00104 
00114     template<class T> void write(
00115         void * CEL_RESTRICT buffer, T t, Endian endian /*= endianHost*/)
00116     {
00117         if(endian != endianHost)
00118             t = swap<T>(t);
00119         std::memcpy(buffer, &t, sizeof(T));
00120     }
00121 
00131     template<class T> void writeArray(
00132         void * CEL_RESTRICT buffer, T * CEL_RESTRICT array, size_t count,
00133         Endian endian /*= endianHost*/)
00134     {
00135         for(size_t i = 0; i < count; i++)
00136         {
00137             write<T>(buffer, array[i], endian);
00138             buffer = ((T*)buffer) + 1;
00139         }
00140     }
00141 
00151     template<class T> T read(
00152         const void * CEL_RESTRICT buffer, Endian endian /*= endianHost*/)
00153     {
00154         T t;
00155         std::memcpy(&t, buffer, sizeof(T));
00156         if(endian != endianHost)
00157             return swap<T>(t);
00158         return t;
00159     }
00160 
00168     template<class T> void swapBytes(
00169         const void * CEL_RESTRICT ioBuffer, size_t inBytes)
00170     {
00171         T *p = (T *)ioBuffer;
00172         T const * const end = p + inBytes / sizeof(T);
00173         for(; p < end; p++)
00174             *p = swap<T>(*p);
00175     }
00176 
00177 } // namespace Celartem
00178 
00179 #endif // _cel_endian_h_

This document is automatically generated using doxygen 1.5.4 at Fri Jun 27 18:21:54 2008.