TCUtil.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCUtil.h 894 2008-10-28 21:51:36Z the_____tiger $
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_

Copyright (c) Thomas Goessler 2003 - 2008