AutoReleasePool.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef Foundation_AutoReleasePool_INCLUDED
  16. #define Foundation_AutoReleasePool_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include <list>
  19. namespace Poco {
  20. template <class C>
  21. class AutoReleasePool
  22. /// An AutoReleasePool implements simple garbage collection for
  23. /// reference-counted objects.
  24. /// It temporarily takes ownwership of reference-counted objects that
  25. /// nobody else wants to take ownership of and releases them
  26. /// at a later, appropriate point in time.
  27. ///
  28. /// Note: The correct way to add an object hold by an AutoPtr<> to
  29. /// an AutoReleasePool is by invoking the AutoPtr's duplicate()
  30. /// method. Example:
  31. /// AutoReleasePool<C> arp;
  32. /// AutoPtr<C> ptr = new C;
  33. /// ...
  34. /// arp.add(ptr.duplicate());
  35. {
  36. public:
  37. AutoReleasePool()
  38. /// Creates the AutoReleasePool.
  39. {
  40. }
  41. ~AutoReleasePool()
  42. /// Destroys the AutoReleasePool and releases
  43. /// all objects it currently holds.
  44. {
  45. release();
  46. }
  47. void add(C* pObject)
  48. /// Adds the given object to the AutoReleasePool.
  49. /// The object's reference count is not modified
  50. {
  51. if (pObject)
  52. _list.push_back(pObject);
  53. }
  54. void release()
  55. /// Releases all objects the AutoReleasePool currently holds
  56. /// by calling each object's release() method.
  57. {
  58. while (!_list.empty())
  59. {
  60. _list.front()->release();
  61. _list.pop_front();
  62. }
  63. }
  64. private:
  65. typedef std::list<C*> ObjectList;
  66. ObjectList _list;
  67. };
  68. } // namespace Poco
  69. #endif // Foundation_AutoReleasePool_INCLUDED