00001 #ifndef COMMONS_POOL_H 00002 #define COMMONS_POOL_H 00003 00004 #include <boost/circular_buffer.hpp> 00005 #include <commons/array.h> 00006 00007 namespace commons { 00008 00009 using namespace boost; 00010 00014 template<typename T> 00015 class pool { 00016 public: 00022 pool(int size) : xs(size), ps(size) { 00023 for (size_t i = 0; i < xs.size(); i++) 00024 ps.push_back(&xs[i]); 00025 } 00026 00031 T *take() { 00032 check(!ps.empty(), "taking from empty pool"); 00033 T *p = ps[0]; 00034 ps.pop_front(); 00035 return p; 00036 } 00037 00044 void drop(T *p) { 00045 check(ps.size() < ps.max_size(), "dropping into full pool"); 00046 ps.push_back(p); 00047 } 00048 00052 size_t size() { 00053 return ps.size(); 00054 } 00055 00056 private: 00057 array<T> xs; 00058 circular_buffer<T*> ps; 00059 }; 00060 00061 } 00062 00063 #endif