# HLAbasicType.hh

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // HLAbasicType.hh - IEEE 1516.2 compliant datatypes
    00003 // Copyright (C) 2008  Petr Gotthard <petr.gotthard@centrum.cz>
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License version 2.1, as published by the Free Software Foundation.
    00008 //
    00009 // This library is distributed in the hope that it will be useful,
    00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012 // Lesser General Public License for more details.
    00013 //
    00014 // $Id: HLAbasicType.hh,v 1.4 2008/11/18 17:38:59 gotthardp Exp $
    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 /* HLAbasicType<DATATYPE, STORAGE, ENCODING>
    00026  * defines a user-convenient DATATYPE, stored in STORAGE using given ENCODING.
    00027  *
    00028  * The data are stored in a buffer of sizeof(STORAGE).
    00029  *
    00030  * The buffer is casted to a DATATYPE that provide data access operators. The
    00031  * data can be accessed in an usual way.
    00032  * The DATATYPE may have any sizeof(), but must have static-cast to STORAGE.
    00033  *
    00034  * For example:
    00035  * typedef HLAbasicType<long, uint32_t, LittleEndian> HLAinteger32BE;
    00036  * HLAdata<HLAinteger32BE> value;
    00037  *
    00038  * value = 42;
    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 /* IEEE 1516.2, Table 23:
    00175  * Basic data representation table
    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 /* IEEE 1516.2, Table 25:
    00194  * Simple datatype table
    00195  */
    00196 typedef HLAoctet HLAASCIIchar;
    00197 typedef HLAoctetPairBE HLAunicodeChar;
    00198 typedef HLAoctet HLAbyte;
    00199
    00200 /* Additional datatypes used by RPR-FOM
    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 } // namespace libhla
    00207
    00208 #endif // _HLATYPES_BASICTYPE_HH
    00209
    00210 // $Id: HLAbasicType.hh,v 1.4 2008/11/18 17:38:59 gotthardp Exp $
    00211