TCValue.h
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 #ifndef _TCVALUE_H_
00037 #define _TCVALUE_H_
00038
00039 #include "TCUtil.h"
00040
00041 namespace TC
00042 {
00059 template < class T>
00060 class Value
00061 {
00062 public:
00068 Value()
00069 {
00070 m_val = (T)0;
00071 }
00076 Value(const Value<T> &val)
00077 {
00078 m_val = val.m_val;
00079 }
00084 Value(const T &val)
00085 {
00086 m_val = val;
00087 }
00088
00090 operator const T&() const
00091 {
00092 return m_val;
00093 }
00094
00096 const uchar* GetBytes() const { return reinterpret_cast<const uchar*>(&m_val); }
00098 uchar* GetBytes() { return reinterpret_cast<uchar*>(&m_val); }
00100 uint32 GetNumBytes() const { return (uint32)sizeof(T); }
00101
00103 Value<T>& operator=(const T &a)
00104 {
00105 m_val = a;
00106 return *this;
00107 }
00108
00109 private:
00111 T m_val;
00112 };
00113
00117 template < class T, bool IS_LITTLE_ENDIAN >
00118 class ByteOrderedValue
00119 {
00120 protected:
00126 ByteOrderedValue()
00127 {
00128 m_val = (T)0;
00129 SwapBytes(m_val);
00130 }
00135 ByteOrderedValue(const ByteOrderedValue<T, IS_LITTLE_ENDIAN> &val)
00136 {
00137 m_val = val.m_val;
00138 }
00143 ByteOrderedValue(const T &val)
00144 {
00145 m_val = val;
00146 SwapBytes(m_val);
00147 }
00148
00149 public:
00151 operator T() const
00152 {
00153 T valOut = m_val;
00154 SwapBytes(valOut);
00155 return valOut;
00156 }
00157
00159 const uchar* GetBytes() const { return reinterpret_cast<const uchar*>(&m_val); }
00161 uchar* GetBytes() { return reinterpret_cast<uchar*>(&m_val); }
00163 uint32 GetNumBytes() const { return (uint32)sizeof(T); }
00164
00166 ByteOrderedValue<T, IS_LITTLE_ENDIAN>& operator=(const T &a)
00167 {
00168 m_val = a;
00169 SwapBytes(m_val);
00170 return *this;
00171 }
00172
00173 private:
00175 inline void SwapBytes(T &val) const
00176 {
00177 if (Util::IsLittleEndian()!=IS_LITTLE_ENDIAN)
00178 {
00179 Util::SwapBytes(val);
00180 }
00181 }
00182
00184 T m_val;
00185 };
00186
00191 template <class T>
00192 class LittleEndianValue: public ByteOrderedValue<T, true>
00193 {
00194 public:
00195 LittleEndianValue():ByteOrderedValue<T, true>() {}
00196 LittleEndianValue(const LittleEndianValue<T> &val):ByteOrderedValue<T, true>(val) {}
00197 LittleEndianValue(const T &val):ByteOrderedValue<T, true>(val) {}
00198 };
00199
00204 template <class T>
00205 class BigEndianValue: public ByteOrderedValue<T, false>
00206 {
00207 public:
00208 BigEndianValue():ByteOrderedValue<T, false>() {}
00209 BigEndianValue(const BigEndianValue<T> &val):ByteOrderedValue<T, false>(val) {}
00210 BigEndianValue(const T &val):ByteOrderedValue<T, false>(val) {}
00211 };
00212
00216 }
00217
00218 #endif // _TCVALUE_H_