IAutoReleasePool.h
1 
27 #ifndef COSMO_AUTO_RELEASE_POOL_INTERFACE_H
28 #define COSMO_AUTO_RELEASE_POOL_INTERFACE_H
29 
30 #include <Pt/Cosmo/Api.h>
31 
32 namespace Pt {
33 
34 namespace Cosmo {
35 
36 class AutoReleasable;
37 
46 {
47  friend class AutoReleasable;
48 
49  public:
50  virtual ~IAutoReleasePool()
51  { }
52 
53  protected:
54  virtual void onAttachReleasable(AutoReleasable& r) = 0;
55 
56  virtual void onDetachReleasable(AutoReleasable& r) = 0;
57 };
58 
68 {
69  friend class AutoReleasePool;
70 
71  public:
73  : _pool(0)
74  , _next(0)
75  { }
76 
77  explicit AutoReleasable(IAutoReleasePool& pool)
78  : _pool(&pool)
79  , _next(0)
80  {
81  _pool->onAttachReleasable(*this);
82  }
83 
84  AutoReleasable(AutoReleasable&& other) noexcept
85  : _pool(0)
86  , _next(0)
87  {
88  movePool(other);
89  }
90 
91  virtual ~AutoReleasable()
92  {
93  detachPool();
94  }
95 
96  AutoReleasable& operator=(AutoReleasable&& other) noexcept
97  {
98  if(this == &other)
99  return *this;
100 
101  detachPool();
102  movePool(other);
103  return *this;
104  }
105 
106  void reset()
107  {
108  detachPool();
109  onRelease();
110  }
111 
112  protected:
113  virtual void onRelease() = 0;
114 
115  private:
116  void detachPool()
117  {
118  if(_pool)
119  {
120  _pool->onDetachReleasable(*this);
121  _pool = 0;
122  }
123  }
124 
125  void movePool(AutoReleasable& other)
126  {
127  IAutoReleasePool* otherPool = other._pool;
128  if(otherPool)
129  {
130  otherPool->onDetachReleasable(other);
131  other._pool = 0;
132 
133  _pool = otherPool;
134  _pool->onAttachReleasable(*this);
135  }
136  }
137 
138  AutoReleasable(const AutoReleasable& other) = delete;
139 
140  AutoReleasable& operator=(const AutoReleasable& other) = delete;
141 
142  private:
143  IAutoReleasePool* _pool;
144  AutoReleasable* _next;
145 };
146 
147 } // namespace Cosmo
148 
149 } // namespace Pt
150 
151 #endif // include guard
Core module.
Definition: Allocator.h:33
Auto-release pool interface.
Definition: IAutoReleasePool.h:46
Auto-release pool implementation.
Definition: AutoReleasePool.h:45
Auto-releasable resource.
Definition: IAutoReleasePool.h:68