AutoReleasePool.h
1
26
27#ifndef COSMO_AUTO_RELEASE_POOL_H
28#define COSMO_AUTO_RELEASE_POOL_H
29
30#include <Pt/Cosmo/IAutoReleasePool.h>
31
32namespace Pt {
33
34namespace Cosmo {
35
44class AutoReleasePool : public IAutoReleasePool
45{
46 public:
47 AutoReleasePool()
48 : _head(0)
49 { }
50
51 virtual ~AutoReleasePool()
52 {
53 while( _head )
54 _head->reset();
55 }
56
57 void reset()
58 {
59 while( _head )
60 _head->reset();
61 }
62
63 protected:
64 virtual void onAttachReleasable(AutoReleasable& ar) override
65 {
66 ar._next = _head;
67 _head = &ar;
68 }
69
70 virtual void onDetachReleasable(AutoReleasable& ar) override
71 {
72 if(_head == &ar)
73 {
74 _head = _head->_next;
75 ar._next = 0;
76 return;
77 }
78
79 AutoReleasable* prev = _head;
80 AutoReleasable* node = _head->_next;
81
82 while(node)
83 {
84 if(node == &ar)
85 {
86 prev->_next = node->_next;
87 ar._next = 0;
88 break;
89 }
90
91 prev = node;
92 node = node->_next;
93 }
94 }
95
96 private:
97 AutoReleasePool(const AutoReleasePool& other) = delete;
98
99 AutoReleasePool& operator=(const AutoReleasePool& other) = delete;
100
101 private:
102 AutoReleasable* _head;
103};
104
105} // namespace Cosmo
106
107} // namespace Pt
108
109#endif // include guard
Auto-release pool interface.
Definition IAutoReleasePool.h:46
Cosmo Component Model.
Definition Api.h:53
Core module.
Definition Allocator.h:33