home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / audio_sdk / include / AudioLib / Utilities.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-06-26  |  4.4 KB  |  171 lines

  1. /***********************************************************\
  2. Copyright (C) James Boer, 2002. 
  3. All rights reserved worldwide.
  4.  
  5. This software is provided "as is" without express or implied
  6. warranties. You may freely copy and compile this source into
  7. applications you distribute provided that the copyright text
  8. below is included in the resulting source code, for example:
  9. "Portions Copyright (C) James Boer, 2002"
  10. \***********************************************************/
  11. #ifndef __UTILITIES_H__
  12. #define __UTILITIES_H__
  13.  
  14.  
  15. namespace Audio
  16. {
  17.  
  18. // This template will only work with pointers to single objects
  19. template <class T>
  20. inline void SAFE_DELETE(T& pObj)
  21. {
  22.     if(pObj)
  23.     {
  24.         delete pObj;
  25.         pObj = 0;
  26.     }
  27. }
  28.  
  29. // This template function will work with dynamically allocated arrays
  30. template <class T>
  31. inline void SAFE_DELETE_ARRAY(T& pObjArray)
  32. {
  33.     if(pObjArray)
  34.     {
  35.         delete[] pObjArray;
  36.         pObjArray = 0;
  37.     }
  38. }
  39.  
  40. // Safely releases COM objects and sets the interface ptr to null
  41. template <class T>
  42. inline void SAFE_RELEASE(T& pCOM)
  43. {
  44.     if(pCOM)
  45.     {
  46.         pCOM->Release();
  47.         pCOM = 0;
  48.     }
  49. }
  50.  
  51. // Allows a cleaner way to clear out an STL queue
  52. #define CLEAR_STL_QUEUE(_1) \
  53.     while(!_1.empty()) \
  54.     _1.pop();
  55.  
  56. // These classes allow an STL object to compare object values instead of
  57. // comparing the value of the objects' pointers.
  58. template<class _Ty>
  59. struct ptr_less : std::binary_function<_Ty, _Ty, bool> {
  60.     bool operator()(const _Ty& _X, const _Ty& _Y) const
  61.         {return (*_X < *_Y); }
  62.     };
  63.  
  64. // Handy critical section object ensures that LeaveCriticalSection()
  65. // is always called no matter how the function exits.
  66. class CritSectObj
  67. {
  68. public:
  69.     CritSectObj(LPCRITICAL_SECTION lpCriticalSection)
  70.     {
  71.         m_pCriticalSection = lpCriticalSection;
  72.         EnterCriticalSection(m_pCriticalSection);  
  73.     }
  74.     ~CritSectObj()
  75.     {
  76.         LeaveCriticalSection(m_pCriticalSection);
  77.     }
  78. private:
  79.     LPCRITICAL_SECTION m_pCriticalSection;
  80. };
  81.  
  82. #define CRITICAL_FUNCTION(lpCriticalSection) \
  83.     CritSectObj __CritSectObj(lpCriticalSection);
  84.  
  85. class IAudioStream;
  86. class CreateAudioStream
  87. {
  88. public:
  89.     CreateAudioStream(IAudioStream*& pStream);
  90.     ~CreateAudioStream();
  91. private:
  92.     IAudioStream*        m_pStream;
  93. };
  94.  
  95.  
  96.  
  97. // convinient conversion from bytes to megabytes
  98. inline size_t Megabytes(size_t MB)    
  99. {  return (MB*1048576);  }
  100.  
  101.  
  102. // Handy string conversion classes
  103. static void StringToUpper(std::string& sString)
  104. {
  105.     for(int i = 0; i < sString.length(); i++)
  106.         sString[i] = toupper(sString[i]);
  107. }
  108.  
  109. static void StringToLower(std::string& sString)
  110. {
  111.     for(int i = 0; i < sString.length(); i++)
  112.         sString[i] = tolower(sString[i]);
  113. }
  114.  
  115. // Random number utility classes
  116. static float GetRandom(float fMin, float fMax)
  117. {
  118.     return (float(rand() % RAND_MAX) / float(RAND_MAX)) * (fMax - fMin) + fMin;
  119. }
  120.  
  121. static int GetRandom(int iMin, int iMax)
  122. {
  123.     return (rand() % (iMax - iMin)) + iMin;
  124. }
  125.  
  126. // Value clamping utility functions
  127. template <class T>
  128. inline T ClampMin(T val, T min)
  129. {  return (val < min) ? min : val;  }
  130.  
  131. template <class T>
  132. inline T ClampMax(T val, T max)
  133. {  return (val > max) ? max : val;  }
  134.  
  135. template <class T>
  136. inline T Clamp(T val, T min, T max)
  137. {  return ClampMin(ClampMax(val, max), min);  }
  138.  
  139. //-----------------------------------------------------------------------------
  140. // UNICODE support for converting between CHAR, TCHAR, and WCHAR strings
  141. //-----------------------------------------------------------------------------
  142. void ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource, int cchDestChar = -1 );
  143. void ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource, int cchDestChar = -1 );
  144. void ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
  145. void ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
  146. void ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource, int cchDestChar = -1 );
  147. void ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource, int cchDestChar = -1 );
  148.  
  149.  
  150. // class used for dynamically adjusting a variable over time
  151. class VarAdjust
  152. {
  153. public:
  154.     VarAdjust();
  155.     void Clear();
  156.     void Init(float fStartVal, float fEndVal, float fTime);
  157.     float GetVar();
  158.     bool IsFinished();
  159.  
  160. private:
  161.     float    m_fStartTime;
  162.     float    m_fEndTime;
  163.     float    m_fStartVal;
  164.     float    m_fEndVal;
  165.     bool    m_bFirstCheck;
  166. };
  167.  
  168.  
  169. }; // namespace Audio
  170.  
  171. #endif // __UTILITIES_H__