src/Utility/SharedPtrObjectPool.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
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     };  // template<typename T> class PooledSharedPtrObject
00171 
00172 } //namespace Onikiri
00173 
00174 #endif
00175 
00176 

Onikiri2に対してTue Jun 18 14:34:27 2013に生成されました。  doxygen 1.4.7