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 #ifndef __SGL_OBJPOOL_HPP
00026 #define __SGL_OBJPOOL_HPP
00027
00028 #include <sgl.h>
00029 #include <vector>
00030
00031
00032 template <class T>
00033 class sglObjPool : public vector<T*>
00034 {
00035 public:
00036 typedef typename vector<T*>::const_iterator base_iterator;
00037
00038 public:
00039
00040
00041 sglObjPool(unsigned int start, unsigned int increment);
00042 ~sglObjPool();
00043
00044
00045 T* newObject();
00046
00047
00048 base_iterator currEnd() const { return m_curr_end; }
00049
00050
00051 unsigned int currSize() const { return m_curr_end - begin(); }
00052
00053
00054 void reset() { m_curr_end = begin(); }
00055
00056 private:
00057 sglObjPool();
00058 sglObjPool(const sglObjPool &);
00059 sglObjPool &operator=(const sglObjPool &);
00060
00061 private:
00062 unsigned int m_increment;
00063 base_iterator m_curr_end;
00064 };
00065
00066
00067
00068 template <class T>
00069 inline sglObjPool<T>::sglObjPool(unsigned int start, unsigned int increment)
00070 : m_increment(increment)
00071 {
00072 for (unsigned int i = 0; i < start; ++i)
00073 push_back(new T);
00074
00075 m_curr_end = begin();
00076 }
00077
00078
00079 template <class T>
00080 inline sglObjPool<T>::~sglObjPool()
00081 {
00082 for (base_iterator it = begin(); it != end(); ++it)
00083 {
00084 delete *it;
00085 }
00086 }
00087
00088
00089 template <class T>
00090 inline T* sglObjPool<T>::newObject()
00091 {
00092 if (m_curr_end == end())
00093 {
00094 for (unsigned int i = 0; i < m_increment; ++i)
00095 {
00096 push_back(new T);
00097 }
00098
00099
00100 m_curr_end = end() - m_increment;
00101 }
00102
00103 T* obj = *m_curr_end;
00104 ++m_curr_end;
00105 return obj;
00106 }
00107
00108 #endif