/src/poco/Foundation/include/Poco/AutoReleasePool.h
Line | Count | Source |
1 | | // |
2 | | // AutoReleasePool.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Core |
6 | | // Module: AutoReleasePool |
7 | | // |
8 | | // Definition of the AutoReleasePool class. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_AutoReleasePool_INCLUDED |
18 | | #define Foundation_AutoReleasePool_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include <list> |
23 | | |
24 | | |
25 | | namespace Poco { |
26 | | |
27 | | |
28 | | template <class C> |
29 | | class AutoReleasePool |
30 | | /// An AutoReleasePool implements simple garbage collection for |
31 | | /// reference-counted objects. |
32 | | /// It temporarily takes ownership of reference-counted objects that |
33 | | /// nobody else wants to take ownership of and releases them |
34 | | /// at a later, appropriate point in time. |
35 | | /// |
36 | | /// Note: The correct way to add an object hold by an AutoPtr<> to |
37 | | /// an AutoReleasePool is by invoking the AutoPtr's duplicate() |
38 | | /// method. Example: |
39 | | /// AutoReleasePool<C> arp; |
40 | | /// AutoPtr<C> ptr = new C; |
41 | | /// ... |
42 | | /// arp.add(ptr.duplicate()); |
43 | | { |
44 | | public: |
45 | 22.8k | AutoReleasePool() = default; |
46 | | /// Creates the AutoReleasePool. |
47 | | |
48 | | ~AutoReleasePool() |
49 | | /// Destroys the AutoReleasePool and releases |
50 | | /// all objects it currently holds. |
51 | 22.8k | { |
52 | 22.8k | release(); |
53 | 22.8k | } |
54 | | |
55 | | void add(C* pObject) |
56 | | /// Adds the given object to the AutoReleasePool. |
57 | | /// The object's reference count is not modified |
58 | 0 | { |
59 | 0 | if (pObject) |
60 | 0 | _list.push_back(pObject); |
61 | 0 | } |
62 | | |
63 | | void release() |
64 | | /// Releases all objects the AutoReleasePool currently holds |
65 | | /// by calling each object's release() method. |
66 | 23.0k | { |
67 | 23.0k | while (!_list.empty()) |
68 | 0 | { |
69 | 0 | _list.front()->release(); |
70 | 0 | _list.pop_front(); |
71 | 0 | } |
72 | 23.0k | } |
73 | | |
74 | | private: |
75 | | using ObjectList = std::list<C *>; |
76 | | |
77 | | ObjectList _list; |
78 | | }; |
79 | | |
80 | | |
81 | | } // namespace Poco |
82 | | |
83 | | |
84 | | #endif // Foundation_AutoReleasePool_INCLUDED |