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
25namespace Poco {
26
27
28template <class C>
29class 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{
44public:
45 AutoReleasePool()
46 /// Creates the AutoReleasePool.
47 {
48 }
49
50 ~AutoReleasePool()
51 /// Destroys the AutoReleasePool and releases
52 /// all objects it currently holds.
53 {
54 release();
55 }
56
57 void add(C* pObject)
58 /// Adds the given object to the AutoReleasePool.
59 /// The object's reference count is not modified
60 {
61 if (pObject)
62 _list.push_back(pObject);
63 }
64
65 void release()
66 /// Releases all objects the AutoReleasePool currently holds
67 /// by calling each object's release() method.
68 {
69 while (!_list.empty())
70 {
71 _list.front()->release();
72 _list.pop_front();
73 }
74 }
75
76private:
77 typedef std::list<C*> ObjectList;
78
79 ObjectList _list;
80};
81
82
83} // namespace Poco
84
85
86#endif // Foundation_AutoReleasePool_INCLUDED
87