TCUtil.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 _TC_UTIL_H_
00037 #define _TC_UTIL_H_
00038
00039 #include "TCTypes.h"
00040
00041 namespace TC
00042 {
00058 namespace Util
00059 {
00061 template <class T>
00062 inline T Abs(const T& x)
00063 {
00064 return (x < 0) ? -x : x;
00065 }
00066
00068 template <class T>
00069 inline const T& Max(const T& x, const T& y)
00070 {
00071 return (x > y) ? x : y;
00072 }
00074 template <class T>
00075 inline const T& Max(const T& x, const T& y, const T& z)
00076 {
00077 return Max(x, Max(y, z));
00078 }
00079
00081 template <class T>
00082 inline const T& Min(const T& x, const T& y)
00083 {
00084 return (x < y) ? x : y;
00085 }
00087 template <class T>
00088 inline const T& Min(const T& x, const T& y, const T& z)
00089 {
00090 return Min(x, Min(y, z));
00091 }
00092
00093
00095 template <class T>
00096 inline void Swap(T& x, T& y)
00097 {
00098 T tmp = x;
00099 x = y;
00100 y = tmp;
00101 }
00102
00104 template <class T>
00105 inline void FreeMemoryOfStlContainer(T& container)
00106 {
00107 T tmp;
00108 container.swap(tmp);
00109 }
00110
00112 template <class T>
00113 inline void MinimizeMemoryOfStlContainer(T& container)
00114 {
00115 T tmp(container);
00116 container.swap(tmp);
00117 }
00118
00123 template <class T>
00124 inline void SafeRelease(T& mem)
00125 {
00126 if (mem != 0)
00127 {
00128 delete mem;
00129 mem = 0;
00130 }
00131 }
00132
00137 template <class T>
00138 inline void SafeReleaseArray(T& mem)
00139 {
00140 if (mem != 0)
00141 {
00142 delete[] mem;
00143 mem = 0;
00144 }
00145 }
00146
00148 template <class T>
00149 uint32 ArraySize(const T& array)
00150 {
00151 return sizeof(array)/sizeof(array[0]);
00152 }
00153
00155 inline void SwapBytes(uint8&)
00156 {
00157 }
00159 inline void SwapBytes(uint16& val)
00160 {
00161 val = (val >> 8) | (val << 8);
00162 }
00164 inline void SwapBytes(uint32& val)
00165 {
00166 val = (val >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8) | (val << 24);
00167 }
00169 inline void SwapBytes(uint64& val)
00170 {
00171 uint32 *val_ptr = (uint32 *)&val;
00172 uint32 tmp1 = val_ptr[0];
00173 uint32 tmp2 = val_ptr[1];
00174 SwapBytes(tmp1);
00175 SwapBytes(tmp2);
00176 val_ptr[0] = tmp2;
00177 val_ptr[1] = tmp1;
00178 }
00179
00181 inline void SwapBytes(sint8&)
00182 {
00183 }
00185 inline void SwapBytes(sint16& val)
00186 {
00187 SwapBytes((uint16&)val);
00188 }
00190 inline void SwapBytes(sint32& val)
00191 {
00192 SwapBytes((uint32&)val);
00193 }
00195 inline void SwapBytes(sint64& val)
00196 {
00197 SwapBytes((uint64&)val);
00198 }
00200 inline void SwapBytes(float& val)
00201 {
00202 SwapBytes((uint32&)val);
00203 }
00205 inline void SwapBytes(double& val)
00206 {
00207 SwapBytes((uint64&)val);
00208 }
00209
00211 template <class T>
00212 void SwapBytes(T &val)
00213 {
00214 uchar *buffer = (uchar*)&val;
00215 for (uint32 i = 0; i<sizeof(val)/2; i++)
00216 {
00217 uchar b = buffer[i];
00218
00219 buffer[i] = buffer[sizeof(val)/2 - i - 1];
00220 buffer[sizeof(val)/2 - i - 1] = b;
00221 }
00222 }
00224 inline bool IsBigEndian()
00225 {
00226 union
00227 {
00228 sint32 int_val;
00229 char bytes[4];
00230 } data;
00231 data.int_val = 1;
00232
00233 return (data.bytes[3] == 1);
00234 }
00236 inline bool IsLittleEndian()
00237 {
00238 return !IsBigEndian();
00239 }
00240 }
00241
00245 }
00246
00247 #endif // _TC_UTIL_H_