home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / audio_sdk / include / AudioLib / ObjectPool.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-07-15  |  2.5 KB  |  118 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 OBJECTPOOL_H__
  12. #define OBJECTPOOL_H__
  13.  
  14. #include "Audio.h"
  15.  
  16. #define DEFINE_POOL(ClassName) \
  17.     public: \
  18.     void SetPoolIndex(int32 iIndex) { m_iPoolIndex = iIndex; } \
  19.     int32 GetPoolIndex()            { return m_iPoolIndex; } \
  20.     static bool ReservePool(uint32 nNumObjs)    { return s_ObjPool.Reserve(nNumObjs); } \
  21.     static ClassName* CreateObject() \
  22.     {  return s_ObjPool.CreateObject();  } \
  23.     static void DestroyObject(ClassName* pObj) \
  24.     {  s_ObjPool.DestroyObject(pObj);  } \
  25.     static void TermPool() \
  26.     {  s_ObjPool.Term();  } \
  27.     friend class ObjectPool<ClassName>;\
  28.     private: \
  29.     int32 m_iPoolIndex; \
  30.     static ObjectPool<ClassName> s_ObjPool;
  31.  
  32. #define IMPLEMENT_POOL(ClassName) \
  33.     ObjectPool<ClassName> ClassName::s_ObjPool; 
  34.  
  35. namespace Audio
  36. {
  37.  
  38. template <class T>
  39. class ObjectPool
  40. {
  41. public:
  42.     ObjectPool()    {}
  43.     ~ObjectPool()    { Term(); }
  44.  
  45.     bool Reserve(uint32 nNumObjs);
  46.     void Term();
  47.  
  48.     T* CreateObject();
  49.     void DestroyObject(T*);
  50.     void DestroyAllObjects();
  51.  
  52.  
  53. private:
  54.  
  55.     std::vector<T*>        m_aPool;
  56.     std::vector<int32>    m_aFree;
  57. };
  58.  
  59. template<class T>
  60. bool ObjectPool<T>::Reserve(uint32 nNumObjs)
  61. {
  62.     T* pT;
  63.     m_aPool.reserve(nNumObjs);
  64.     m_aFree.reserve(nNumObjs);
  65.     int iPoolStart = m_aPool.size();
  66.     for(int i = 0; i < nNumObjs; i++)
  67.     {
  68.         pT = new T;
  69.         if(!pT)
  70.             return false;
  71.         pT->SetPoolIndex(-1);
  72.         m_aPool.push_back(pT);
  73.         m_aFree.push_back(iPoolStart + i);
  74.     }
  75.     return true;
  76. }
  77.  
  78.  
  79. template<class T>
  80. void ObjectPool<T>::Term()
  81. {
  82.     for(int i = 0; i < m_aPool.size(); i++)
  83.         delete m_aPool[i];
  84.     m_aPool.clear();
  85.     m_aFree.clear();
  86. }
  87.  
  88.  
  89. template<class T>
  90. T* ObjectPool<T>::CreateObject()
  91. {
  92.     if(!m_aFree.size())
  93.         Reserve(m_aPool.size() * 2 + 1);
  94.     int32 iIndex = m_aFree.back();
  95.     m_aFree.pop_back();
  96.     T* pObj = m_aPool[iIndex];
  97.     assert(pObj->GetPoolIndex() == -1);
  98.     pObj->SetPoolIndex(iIndex);
  99.     return pObj;
  100. }
  101.  
  102.  
  103. template<class T>
  104. void ObjectPool<T>::DestroyObject(T* pObj)
  105. {
  106.     assert(pObj->GetPoolIndex() != -1);
  107.     m_aFree.push_back(pObj->GetPoolIndex());
  108.     pObj->SetPoolIndex(-1);
  109. }
  110.  
  111.  
  112. }; // namespace Audio
  113.  
  114.  
  115.  
  116.  
  117. #endif // OBJECTPOOL_H__
  118.