1 #ifndef _OBJECTPOOL_H_
\r
2 #define _OBJECTPOOL_H_
\r
4 #include <functional>
\r
6 #include <type_traits>
\r
8 #include <tbb/concurrent_queue.h>
\r
16 template<typename T>
\r
17 struct default_new_delete_allocator
\r
19 static T* construct()
\r
23 template<typename P0>
\r
24 static T* construct(P0&& p0)
\r
25 { return new T(std::forward<P0>(p0)); }
\r
27 template<typename P0, typename P1>
\r
28 static T* construct(P0&& p0, P1&& p1)
\r
29 { return new T(std::forward<P0>(p0), std::forward<P1>(p1)); }
\r
31 template<typename P0, typename P1, typename P2>
\r
32 static T* construct(P0&& p0, P1&& p1, P2&& p2)
\r
33 { return new T(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)); }
\r
35 static void destroy(T* const block)
\r
39 template<typename T, typename allocator = default_new_delete_allocator<T>>
\r
42 typedef std::shared_ptr<T> Ptr;
\r
48 while(pool_.try_pop(item))
\r
49 allocator::destroy(item);
\r
54 T* item = pool_.try_pop(item) ? item : allocator::construct();
\r
55 return Ptr(item, [&](T* item){ pool_.push(item); });
\r
58 template<typename P0>
\r
59 Ptr construct(P0&& p0)
\r
61 T* item = pool_.try_pop(item) ? item : allocator::construct(std::forward<P0>(p0));
\r
62 return Ptr(item, [&](T* item){ pool_.push(item); });
\r
65 template<typename P0, typename P1>
\r
66 Ptr construct(P0&& p0, P1&& p1)
\r
68 T* item = pool_.try_pop(item) ? item : allocator::construct(std::forward<P0>(p0), std::forward<P1>(p1));
\r
69 return Ptr(item, [&](T* item){ pool_.push(item); });
\r
72 template<typename P0, typename P1, typename P2>
\r
73 Ptr construct(P0&& p0, P1&& p1, P1&& p2)
\r
75 T* item = pool_.try_pop(item) ? item : allocator::construct(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2));
\r
76 return Ptr(item, [&](T* item){ pool_.push(item); });
\r
79 tbb::concurrent_queue<T*> pool_;
\r