singleton_object_pool.h
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
00025
00026
00027
00028
00029
00030 #if !defined(SINGLETON_OBJECT_POOL_H)
00031 #define SINGLETON_OBJECT_POOL_H
00032
00033 #define BOOST_POOL_NO_MT
00034 #undef BOOST_HAS_THREADS
00035
00036 #include <boost/pool/pool.hpp>
00037 #include "pool_allocators.h"
00038
00039
00040 template <typename T, typename Alloc = boost::default_user_allocator_new_delete>
00041 class singleton_object_pool
00042 {
00043 private:
00044 struct pool_impl
00045 {
00046 boost::pool<Alloc> p;
00047 pool_impl() : p(sizeof(T), 32)
00048 {
00049 }
00050 };
00051 struct singleton
00052 {
00053 static pool_impl& instance()
00054 {
00055 static pool_impl* thePool = new pool_impl;
00056 return *thePool;
00057 }
00058 };
00059
00060
00061 inline static void free(T* free_me)
00062 {
00063 singleton::instance().p.free(free_me);
00064 }
00065
00066 inline static T* malloc()
00067 {
00068 return static_cast<T*>(singleton::instance().p.malloc());
00069 }
00070 public:
00071 inline static bool is_from(T* t)
00072 {
00073 return singleton::instance().p.is_from(t);
00074 }
00075
00076 static T* construct()
00077 {
00078 T* const temp = malloc();
00079 if(temp == 0) return temp;
00080 new(temp) T();
00081 return temp;
00082 }
00083 template <typename A1>
00084 static T* construct(const A1& a1)
00085 {
00086 T* const temp = malloc();
00087 if(temp == 0) return temp;
00088 new(temp) T(a1);
00089 return temp;
00090 }
00091 template <typename A1, typename A2>
00092 static T* construct(const A1& a1, const A2& a2)
00093 {
00094 T* const temp = malloc();
00095 if(temp == 0) return temp;
00096 new(temp) T(a1, a2);
00097 return temp;
00098 }
00099 template <typename A1, typename A2, typename A3>
00100 static T* construct(const A1& a1, const A2& a2, const A3& a3)
00101 {
00102 T* const temp = malloc();
00103 if(temp == 0) return temp;
00104 new(temp) T(a1, a2, a3);
00105 return temp;
00106 }
00107 template <typename A1, typename A2, typename A3, typename A4>
00108 static T* construct(const A1& a1, const A2& a2, const A3& a3, const A4& a4)
00109 {
00110 T* const temp = malloc();
00111 if(temp == 0) return temp;
00112 new(temp) T(a1, a2, a3, a4);
00113 return temp;
00114 }
00115 template <typename A1, typename A2, typename A3, typename A4, typename A5>
00116 static T* construct(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5)
00117 {
00118 T* const temp = malloc();
00119 if(temp == 0) return temp;
00120 new(temp) T(a1, a2, a3, a4, a5);
00121 return temp;
00122 }
00123 inline static void destroy(T* const kill_me)
00124 {
00125 kill_me->~T();
00126 free(kill_me);
00127 }
00128
00129
00130 };
00131
00132
00133 template <typename T>
00134 struct PoolDestructor
00135 {
00136 inline void operator()(T* e)
00137 {
00138
00139 if(singleton_object_pool<T>::is_from(e)) {
00140 singleton_object_pool<T>::destroy(e);
00141 }
00142
00143 }
00144 };
00145
00146 template <typename T> inline
00147 boost::shared_ptr<T> make_shared(T* t)
00148 {
00149 return boost::shared_ptr<T>(t, PoolDestructor<T>());
00150 }
00151
00152
00153 #endif