AutoReleasePool.h 1.8 KB

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