00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _HLATYPES_BASICTYPE_HH
00018 #define _HLATYPES_BASICTYPE_HH
00019
00020 #include <HLAbuffer.hh>
00021 #include <cstring>
00022
00023 namespace libhla {
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00042
00044 template<class T, int i = sizeof(T)>
00045 struct __swap;
00046
00048 template<class T>
00049 struct LittleEndian
00050 {
00051 inline const T operator()(const T& x) const {
00052 #ifdef HOST_IS_BIG_ENDIAN
00053 return __swap<T>()( x );
00054 #else
00055 return x;
00056 #endif
00057 }
00058 };
00059
00061 template<class T>
00062 struct BigEndian
00063 {
00064 inline const T operator()(const T& x) const {
00065 #ifdef HOST_IS_BIG_ENDIAN
00066 return x;
00067 #else
00068 return __swap<T>()( x );
00069 #endif
00070 }
00071 };
00072
00073 template<class T>
00074 struct __swap<T,1>
00075 {
00076 inline const T operator()(const T& x) const {
00077 return x;
00078 }
00079 };
00080
00081 template<class T>
00082 struct __swap<T,2>
00083 {
00084 inline const T operator()(const T& x) const {
00085 union {
00086 uint16_t u16;
00087 T x;
00088 } result;
00089 result.u16 =
00090 (*(uint16_t*)&x<<8 | *(uint16_t*)&x>>8);
00091 return result.x;
00092 }
00093 };
00094
00095 template<class T>
00096 struct __swap<T,4>
00097 {
00098 inline const T operator()(const T& x) const {
00099 union {
00100 uint32_t u32;
00101 T x;
00102 } result;
00103 result.u32 =
00104 (*(uint32_t*)&x<<24 | *(uint32_t*)&x>>24 |
00105 (*(uint32_t*)&x & 0x0000ff00UL)<<8 |
00106 (*(uint32_t*)&x & 0x00ff0000UL)>>8);
00107 return result.x;
00108 }
00109 };
00110
00111 template<class T>
00112 struct __swap<T,8>
00113 {
00114 inline const T operator()(const T& x) const {
00115 union {
00116 uint64_t u64;
00117 T x;
00118 } result;
00119 result.u64 =
00120 (*(uint64_t*)&x<<56 | *(uint64_t*)&x>>56 |
00121 (*(uint64_t*)&x & 0x000000000000ff00ULL)<<40 |
00122 (*(uint64_t*)&x & 0x0000000000ff0000ULL)<<24 |
00123 (*(uint64_t*)&x & 0x00000000ff000000ULL)<< 8 |
00124 (*(uint64_t*)&x & 0x000000ff00000000ULL)>> 8 |
00125 (*(uint64_t*)&x & 0x0000ff0000000000ULL)>>24 |
00126 (*(uint64_t*)&x & 0x00ff000000000000ULL)>>40);
00127 return result.x;
00128 }
00129 };
00130
00132 template<class T, class S, template<class T>class E>
00133 struct HLAbasicType
00134 {
00135 HLAbasicType& operator = (const T& data)
00136 {
00137 #ifndef NDEBUG
00138 __HLAbuffer::__check_memory(this, __sizeof());
00139 #endif
00140 *(S*)this = E<S>()(data);
00141 return *this;
00142 }
00143
00144 operator T() const
00145 {
00146 #ifndef NDEBUG
00147 __HLAbuffer::__check_memory(this, __sizeof());
00148 #endif
00149 return E<S>()(*(S*)this);
00150 }
00151
00152 static const size_t emptysizeof()
00153 { return __sizeof(); }
00154
00155 static const size_t __sizeof()
00156 { return sizeof(S); }
00157
00158 void copy(void* source)
00159 {
00160 #ifndef NDEBUG
00161 __HLAbuffer::__check_memory(this, __sizeof());
00162 #endif
00163 memcpy((char*)this, source, __sizeof());
00164 }
00165
00166 static const size_t m_octetBoundary = sizeof(S);
00167 static const bool m_isVariable = false;
00168 };
00169
00170 template<class T, class S, template<class T>class E>
00171 std::ostream& PrintBuffer(std::ostream& stream, HLAbasicType<T,S,E>& buffer)
00172 { return __print_buffer(stream, (void*)&buffer, buffer.__sizeof()); }
00173
00174
00175
00176
00177 typedef HLAbasicType<short, int16_t, BigEndian> HLAinteger16BE;
00178 typedef HLAbasicType<long, int32_t, BigEndian> HLAinteger32BE;
00179 typedef HLAbasicType<long long, int64_t, BigEndian> HLAinteger64BE;
00180 typedef HLAbasicType<float, float, BigEndian> HLAfloat32BE;
00181 typedef HLAbasicType<double, double, BigEndian> HLAfloat64BE;
00182 typedef HLAbasicType<wchar_t, wchar_t, BigEndian> HLAoctetPairBE;
00183
00184 typedef HLAbasicType<short, int16_t, LittleEndian> HLAinteger16LE;
00185 typedef HLAbasicType<long, int32_t, LittleEndian> HLAinteger32LE;
00186 typedef HLAbasicType<long long, int64_t, LittleEndian> HLAinteger64LE;
00187 typedef HLAbasicType<float, float, LittleEndian> HLAfloat32LE;
00188 typedef HLAbasicType<double, double, LittleEndian> HLAfloat64LE;
00189 typedef HLAbasicType<wchar_t, wchar_t, LittleEndian> HLAoctetPairLE;
00190
00191 typedef HLAbasicType<char, char, BigEndian> HLAoctet;
00192
00193
00194
00195
00196 typedef HLAoctet HLAASCIIchar;
00197 typedef HLAoctetPairBE HLAunicodeChar;
00198 typedef HLAoctet HLAbyte;
00199
00200
00201
00202 typedef HLAbasicType<unsigned short, uint16_t, BigEndian> Unsignedinteger16BE;
00203 typedef HLAbasicType<unsigned long, uint32_t, BigEndian> Unsignedinteger32BE;
00204 typedef HLAbasicType<unsigned long, uint64_t, BigEndian> Unsignedinteger64BE;
00205
00206 }
00207
00208 #endif // _HLATYPES_BASICTYPE_HH
00209
00210
00211