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
00031
00032 #ifndef __SHARED_PTR_OBJECT_POOL_H
00033 #define __SHARED_PTR_OBJECT_POOL_H
00034
00035 namespace Onikiri
00036 {
00037
00038 template<typename T>
00039 class SharedPtrObjectPool
00040 {
00041 struct SharedPtrObjectPoolTag
00042 {
00043 };
00044
00045 typedef boost::singleton_pool<
00046 SharedPtrObjectPoolTag,
00047 sizeof(T)
00048 > Pool;
00049
00050 struct SharedPtrObjectPoolDeleter
00051 {
00052 void operator()(T* ptr)
00053 {
00054 ptr->~T();
00055 Pool::free(ptr);
00056 };
00057 };
00058 public:
00059
00060 boost::shared_ptr<T> construct()
00061 {
00062 T* const ret = (T*)Pool::malloc();
00063 if(ret == 0)
00064 return boost::shared_ptr<T>( ret );
00065 try { new (ret) T(); }
00066 catch (...) { Pool::free(ret); throw; }
00067
00068 return boost::shared_ptr<T>(
00069 ret,
00070 SharedPtrObjectPoolDeleter() );
00071 };
00072
00073 template <typename Arg0>
00074 boost::shared_ptr<T> construct(const Arg0& a0)
00075 {
00076 T* const ret = (T*)Pool::malloc();
00077 if(ret == 0)
00078 return boost::shared_ptr<T>( ret );
00079 try { new (ret) T(a0); }
00080 catch (...) { Pool::free(ret); throw; }
00081
00082 return boost::shared_ptr<T>(
00083 ret,
00084 SharedPtrObjectPoolDeleter() );
00085 };
00086
00087 template <typename Arg0, typename Arg1>
00088 boost::shared_ptr<T> construct(const Arg0& a0, const Arg1& a1)
00089 {
00090 T* const ret = (T*)Pool::malloc();
00091 if(ret == 0)
00092 return boost::shared_ptr<T>( ret );
00093 try { new (ret) T(a0, a1); }
00094 catch (...) { Pool::free(ret); throw; }
00095
00096 return boost::shared_ptr<T>(
00097 ret,
00098 SharedPtrObjectPoolDeleter() );
00099 };
00100
00101 template <typename Arg0, typename Arg1, typename Arg2>
00102 boost::shared_ptr<T> construct(const Arg0& a0, const Arg1& a1, const Arg2& a2)
00103 {
00104 T* const ret = (T*)Pool::malloc();
00105 if(ret == 0)
00106 return boost::shared_ptr<T>( ret );
00107 try { new (ret) T(a0, a1, a2); }
00108 catch (...) { Pool::free(ret); throw; }
00109
00110 return boost::shared_ptr<T>(
00111 ret,
00112 SharedPtrObjectPoolDeleter() );
00113 };
00114
00115 template <typename Arg0, typename Arg1, typename Arg2, typename Arg3>
00116 boost::shared_ptr<T> construct(
00117 const Arg0& a0, const Arg1& a1, const Arg2& a2, const Arg3& a3)
00118 {
00119 T* const ret = (T*)Pool::malloc();
00120 if(ret == 0)
00121 return boost::shared_ptr<T>( ret );
00122 try { new (ret) T(a0, a1, a2, a3); }
00123 catch (...) { Pool::free(ret); throw; }
00124
00125 return boost::shared_ptr<T>(
00126 ret,
00127 SharedPtrObjectPoolDeleter() );
00128 };
00129 };
00130
00131 template<typename T>
00132 class PooledSharedPtrObject
00133 {
00134 static SharedPtrObjectPool<T>& GetPool()
00135 {
00136 static SharedPtrObjectPool<T> pool;
00137 return pool;
00138 }
00139 public:
00140 static boost::shared_ptr<T> Construct()
00141 {
00142 return GetPool().construct();
00143 };
00144
00145 template <typename Arg0>
00146 static boost::shared_ptr<T> Construct(const Arg0& a0)
00147 {
00148 return GetPool().construct(a0);
00149 };
00150
00151 template <typename Arg0, typename Arg1>
00152 static boost::shared_ptr<T> Construct(const Arg0& a0, const Arg1& a1)
00153 {
00154 return GetPool().construct(a0, a1);
00155 };
00156
00157 template <typename Arg0, typename Arg1, typename Arg2>
00158 static boost::shared_ptr<T> Construct(const Arg0& a0, const Arg1& a1, const Arg2& a2)
00159 {
00160 return GetPool().construct(a0, a1, a2);
00161 };
00162
00163 template <typename Arg0, typename Arg1, typename Arg2, typename Arg3>
00164 static boost::shared_ptr<T> Construct(
00165 const Arg0& a0, const Arg1& a1, const Arg2& a2, const Arg3& a3)
00166 {
00167 return GetPool().construct(a0, a1, a2, a3);
00168 };
00169
00170 };
00171
00172 }
00173
00174 #endif
00175
00176