Util--  1.1
objectpool.hh
Go to the documentation of this file.
1 #ifndef SIM_OBJECTPOOL_HH
2 #define SIM_OBJECTPOOL_HH
3 
4 #include <list>
5 #include <boost/noncopyable.hpp>
6 #include <boost/thread/mutex.hpp>
7 #include <utilmm/memory/sweep.hh>
8 
9 namespace utilmm
10 {
11  namespace pools
12  {
21  template<typename T>
22  class object_pool : boost::noncopyable
23  {
24  boost::mutex m_mutex;
25  std::list<T*> m_available;
26 
27  typedef boost::mutex mutex;
28 
29  public:
31  { sweep(m_available.begin(), m_available.end()); }
32 
33  T* get()
34  { mutex::scoped_lock lock(m_mutex);
35  if (m_available.empty())
36  m_available.push_back(new T());
37 
38  T* ret = m_available.front();
39  m_available.pop_front();
40  return ret;
41  }
42 
43  void put(T* object)
44  { mutex::scoped_lock lock(m_mutex);
45  m_available.push_back(object);
46  }
47  };
48 
52  template<typename T>
53  class use : boost::noncopyable
54  {
55  object_pool<T>& m_pool;
56  T* m_object;
57 
58  public:
60  : m_pool(pool), m_object(pool.get()) {}
61  ~use() { m_pool.put(m_object); }
62 
63  T* operator -> () { return m_object; }
64  T const* operator ->() const { return m_object; }
65  T& operator *() { return *m_object; }
66  T const& operator *() const { return *m_object; }
67  };
68  }
69 }
70 
71 #endif
72 
Definition: objectpool.hh:53
~use()
Definition: objectpool.hh:61
T * get()
Definition: objectpool.hh:33
void sweep(Iterator begin, Iterator end)
Definition: sweep.hh:9
Definition: auto_flag.hh:6
~object_pool()
Definition: objectpool.hh:30
use(object_pool< T > &pool)
Definition: objectpool.hh:59
Definition: objectpool.hh:22
void put(T *object)
Definition: objectpool.hh:43

Generated on Mon Oct 8 2018 08:38:45 for Util-- by doxygen 1.8.11
SourceForge.net Project Page