PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/TCSystem/Modules/Basic/inc/TCUtil.h

https://bitbucket.org/the____tiger/tcfilesync
C++ Header | 255 lines | 153 code | 22 blank | 80 comment | 6 complexity | a9a6670332b0338bbe50624d9138decc MD5 | raw file
Possible License(s): LGPL-2.1
  1. //*******************************************************************************
  2. //
  3. // ******* *** *** *
  4. // * * * *
  5. // * * * *****
  6. // * * *** * * ** * ** ***
  7. // * * * * * * * **** * * *
  8. // * * * * * * * * * * *
  9. // * *** *** * ** ** ** * *
  10. // *
  11. //*******************************************************************************
  12. // see http://sourceforge.net/projects/tcsystem/ for details.
  13. // Copyright (C) 2003 - 2012 Thomas Goessler. All Rights Reserved.
  14. //*******************************************************************************
  15. //
  16. // TCSystem is the legal property of its developers.
  17. // Please refer to the COPYRIGHT file distributed with this source distribution.
  18. //
  19. // This library is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU Lesser General Public
  21. // License as published by the Free Software Foundation; either
  22. // version 2.1 of the License, or (at your option) any later version.
  23. //
  24. // This library is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. // Lesser General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU Lesser General Public
  30. // License along with this library; if not, write to the Free Software
  31. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  32. //*******************************************************************************
  33. // $Id$
  34. //*******************************************************************************
  35. #ifndef _TC_UTIL_H_
  36. #define _TC_UTIL_H_
  37. #include "TCTypes.h"
  38. namespace tc
  39. {
  40. /**
  41. * @addtogroup TC_BASE
  42. * @{
  43. */
  44. /**
  45. * @file
  46. * This header file provides the definition of the namespace tc::util.
  47. *
  48. * @author Thomas Goessler
  49. */
  50. /**
  51. * @brief Defines basic functions like Min, Max, Swap ...
  52. */
  53. namespace util
  54. {
  55. /** @return the absolute value of the given value */
  56. template <class T>
  57. inline T Abs(const T& x)
  58. {
  59. return (x < 0) ? -x : x;
  60. }
  61. /** @return maximum of two values */
  62. template <class T>
  63. inline const T& Max(const T& x, const T& y)
  64. {
  65. return (x > y) ? x : y;
  66. }
  67. /** @return maximum of three values */
  68. template <class T>
  69. inline const T& Max(const T& x, const T& y, const T& z)
  70. {
  71. return Max(x, Max(y, z));
  72. }
  73. /** @return minimum of two values */
  74. template <class T>
  75. inline const T& Min(const T& x, const T& y)
  76. {
  77. return (x < y) ? x : y;
  78. }
  79. /** @return minimum of three values */
  80. template <class T>
  81. inline const T& Min(const T& x, const T& y, const T& z)
  82. {
  83. return Min(x, Min(y, z));
  84. }
  85. /** @return Swap the two variables */
  86. template <class T>
  87. inline void Swap(T& x, T& y)
  88. {
  89. T tmp = x;
  90. x = y;
  91. y = tmp;
  92. }
  93. /** @brief Really frees memory of vector */
  94. template <class T>
  95. inline void FreeMemoryOfStlContainer(T& container)
  96. {
  97. T tmp;
  98. container.swap(tmp);
  99. }
  100. /** @brief Minimizes the memory usage of a vector */
  101. template <class T>
  102. inline void MinimizeMemoryOfStlContainer(T& container)
  103. {
  104. T tmp(container);
  105. container.swap(tmp);
  106. }
  107. /**
  108. * Function template for safe memory pointer deletion
  109. * @param mem memory pointer to delete
  110. */
  111. template <class T>
  112. inline void SafeRelease(T& mem)
  113. {
  114. if (mem != 0)
  115. {
  116. delete mem;
  117. mem = 0;
  118. }
  119. }
  120. /**
  121. * Function template for safe memory pointer deletion of an allocated array
  122. * @param mem memory pointer to delete
  123. */
  124. template <class T>
  125. inline void SafeReleaseArray(T& mem)
  126. {
  127. if (mem != 0)
  128. {
  129. delete[] mem;
  130. mem = 0;
  131. }
  132. }
  133. #ifdef _MSC_VER
  134. #pragma warning (push)
  135. #pragma warning (disable: 4100)
  136. #endif
  137. /** @return The number of elements of an C array */
  138. template <class T>
  139. uint32_t ArraySize(const T& array)
  140. {
  141. return sizeof(array)/sizeof(array[0]);
  142. }
  143. #ifdef _MSC_VER
  144. #pragma warning (pop)
  145. #endif
  146. /** @brief Swaps the bytes of an uint8_t value */
  147. inline void SwapBytes(uint8_t&)
  148. {
  149. }
  150. /** @brief Swaps the bytes of an uint16_t value */
  151. inline void SwapBytes(uint16_t& val)
  152. {
  153. val = (val >> 8) | (val << 8);
  154. }
  155. /** @brief Swaps the bytes of an uint32_t value */
  156. inline void SwapBytes(uint32_t& val)
  157. {
  158. val = (val >> 24) | ((val & 0x00ff0000) >> 8) | ((val & 0x0000ff00) << 8) | (val << 24);
  159. }
  160. /** @brief Swaps the bytes of an uint64_t value */
  161. inline void SwapBytes(uint64_t& val)
  162. {
  163. uint32_t *val_ptr = (uint32_t *)&val;
  164. uint32_t tmp1 = val_ptr[0];
  165. uint32_t tmp2 = val_ptr[1];
  166. SwapBytes(tmp1);
  167. SwapBytes(tmp2);
  168. val_ptr[0] = tmp2;
  169. val_ptr[1] = tmp1;
  170. }
  171. /** @brief Swaps the bytes of an int8_t value */
  172. inline void SwapBytes(int8_t&)
  173. {
  174. }
  175. /** @brief Swaps the bytes of an int16_t value */
  176. inline void SwapBytes(int16_t& val)
  177. {
  178. SwapBytes((uint16_t&)val);
  179. }
  180. /** @brief Swaps the bytes of an int32_t value */
  181. inline void SwapBytes(int32_t& val)
  182. {
  183. SwapBytes((uint32_t&)val);
  184. }
  185. /** @brief Swaps the bytes of an int64_t value */
  186. inline void SwapBytes(int64_t& val)
  187. {
  188. SwapBytes((uint64_t&)val);
  189. }
  190. /** @brief Swaps the bytes of an float value */
  191. inline void SwapBytes(float& val)
  192. {
  193. SwapBytes((uint32_t&)val);
  194. }
  195. /** @brief Swaps the bytes of an double value */
  196. inline void SwapBytes(double& val)
  197. {
  198. SwapBytes((uint64_t&)val);
  199. }
  200. /** @brief Swaps the bytes of any type */
  201. template <class T>
  202. void SwapBytes(T &val)
  203. {
  204. uint8_t *buffer = (uint8_t*)&val;
  205. for (uint32_t i = 0; i<sizeof(val)/2; i++)
  206. {
  207. uint8_t b = buffer[i];
  208. buffer[i] = buffer[sizeof(val)/2 - i - 1];
  209. buffer[sizeof(val)/2 - i - 1] = b;
  210. }
  211. }
  212. /** @return if byte ordering is big endian */
  213. inline bool IsBigEndian()
  214. {
  215. union
  216. {
  217. int32_t int_val;
  218. char bytes[4];
  219. } data;
  220. data.int_val = 1;
  221. return (data.bytes[3] == 1);
  222. }
  223. /** @return if byte ordering is little endian */
  224. inline bool IsLittleEndian()
  225. {
  226. return !IsBigEndian();
  227. }
  228. }
  229. /**
  230. * @}
  231. */
  232. }
  233. #endif // _TC_UTIL_H_