endianness.inl
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
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_ENDIANNESS_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_ENDIANNESS_INL
00047
00048 namespace lass
00049 {
00050 namespace num
00051 {
00052
00053
00054
00055 namespace impl
00056 {
00057
00058 template <size_t numOfBytes>
00059 struct Revertor
00060 {
00061
00062 static void revert(void* ioIn)
00063 {
00064 std::reverse(static_cast<Tuint8*>(ioIn), static_cast<Tuint8*>(ioIn) + numOfBytes);
00065 }
00066 };
00067
00068 template <>
00069 struct Revertor<0>
00070 {
00071 static void revert(void* )
00072 {
00073 }
00074 };
00075
00076 template <>
00077 struct Revertor<1>
00078 {
00079 static void revert(void* )
00080 {
00081 }
00082 };
00083
00084 template <>
00085 struct Revertor<2>
00086 {
00087 static void revert(void* ioIn)
00088 {
00089 Tuint16& temp = *static_cast<Tuint16*>(ioIn);
00090 temp = ((temp & 0x00ff) << 8) | (temp >> 8);
00091 }
00092 };
00093
00094 template <>
00095 struct Revertor<4>
00096 {
00097 static void revert(void* ioIn)
00098 {
00099 Tuint32& temp = *static_cast<Tuint32*>(ioIn);
00100 temp = ((temp & 0x000000ff) << 24) | ((temp & 0x0000ff00) << 8) |
00101 ((temp & 0x00ff0000) >> 8) | (temp >> 24);
00102 }
00103 };
00104
00105
00106
00107 template <size_t numOfBytes, Endianness outEndian, Endianness inEndian>
00108 struct EndianCaster
00109 {
00110 inline static void cast(void* ioIn)
00111 {
00112 Revertor<numOfBytes>::revert(ioIn);
00113 }
00114 };
00115
00116 template <size_t numOfBytes, Endianness endian>
00117 struct EndianCaster<numOfBytes, endian, endian>
00118 {
00119 inline static void cast(void* ioIn)
00120 {
00121 }
00122 };
00123
00124 }
00125
00126
00127
00128 template <Endianness outEndian, Endianness inEndian, typename T>
00129 inline T endianCast(T iIn)
00130 {
00131 impl::EndianCaster<sizeof(T), outEndian, inEndian>::cast(reinterpret_cast<void*>(&iIn));
00132 return iIn;
00133 }
00134
00135 template <typename T>
00136 inline T fixEndianness(T iIn, Endianness iEndianness)
00137 {
00138 return iEndianness == systemEndian ? iIn : endianCast<bigEndian, littleEndian>(iIn);
00139 }
00140
00141 }
00142
00143 }
00144
00145 #endif
00146
00147