Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #ifndef __INK_POOL_H_INCLUDED__
00025 #define __INK_POOL_H_INCLUDED__
00026 
00027 
00028 
00029 
00030 
00031 template<class C> class InkStaticPool {
00032 public:
00033 
00034   InkStaticPool(int size):sz1(size + 1), head(0), tail(0)
00035   {
00036     pool = new C *[sz1];
00037   }
00038 
00039   virtual ~ InkStaticPool() {
00040     cleanUp();
00041     delete[]pool;
00042   }
00043 
00044   C *get();
00045   bool put(C * newObj);
00046   void put_or_delete(C * newObj)
00047   {
00048     if (!put(newObj))
00049       delete newObj;
00050   }
00051 
00052 protected:
00053   void cleanUp(void);
00054 
00055 private:
00056   const int sz1;
00057   int head;
00058   int tail;
00059   C **pool;
00060 };
00061 
00062 template<class C> inline C * InkStaticPool<C>::get()
00063 {
00064   if (head != tail) {
00065     C *res = pool[head++];
00066     head %= sz1;
00067     return (res);
00068   }
00069   return (NULL);
00070 }
00071 
00072 template<class C> inline bool InkStaticPool<C>::put(C * newObj)
00073 {
00074   if (newObj == NULL)
00075     return (false);             
00076 
00077   int newTail = (tail + 1) % sz1;
00078   bool res = (newTail != head);
00079   if (res) {
00080     pool[tail] = newObj;
00081     tail = newTail;
00082   }
00083   return (res);
00084 }
00085 
00086 template<class C> inline void InkStaticPool<C>::cleanUp(void)
00087 {
00088   while (true) {
00089     C *tp = get();
00090     if (tp == NULL)
00091       break;
00092     delete tp;
00093   }
00094 }
00095 
00096 #endif // #ifndef __INK_POOL_H_INCLUDED__