DOMObject.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // DOMObject.h
  3. //
  4. // Library: XML
  5. // Package: DOM
  6. // Module: DOM
  7. //
  8. // Definition of the DOMObject 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 DOM_DOMObject_INCLUDED
  16. #define DOM_DOMObject_INCLUDED
  17. #include "Poco/XML/XML.h"
  18. namespace Poco {
  19. namespace XML {
  20. class XML_API DOMObject
  21. /// The base class for all objects in the Document Object Model.
  22. ///
  23. /// DOMObject defines the rules for memory management
  24. /// in this implementation of the DOM. Violation of these
  25. /// rules, which are outlined in the following, results
  26. /// in memory leaks or dangling pointers.
  27. ///
  28. /// Every object created by new or by a factory
  29. /// method (for example, Document::create*) must be released
  30. /// with a call to release() or autoRelease() when it
  31. /// is no longer needed.
  32. ///
  33. /// Every object created by cloning or importing another
  34. /// object must be released.
  35. /// For every call to duplicate() there must be a matching
  36. /// call to release().
  37. /// An object obtained via any other way must not be
  38. /// released, except ownership of it has been explicitly
  39. /// taken with a call to duplicate().
  40. ///
  41. /// While DOMObjects are safe for use in multithreaded programs,
  42. /// a DOMObject or one of its subclasses must not be accessed
  43. /// from multiple threads simultaneously.
  44. {
  45. public:
  46. DOMObject();
  47. /// Creates the DOMObject.
  48. /// The object's reference count is initialized to one.
  49. void duplicate() const;
  50. /// Increases the object's reference count.
  51. void release() const;
  52. /// Decreases the object's reference count.
  53. /// If the reference count reaches zero,
  54. /// the object is deleted.
  55. virtual void autoRelease() = 0;
  56. /// Adds the object to an appropriate
  57. /// AutoReleasePool, which is usually the
  58. /// AutoReleasePool managed by the Document
  59. /// to which this object belongs.
  60. protected:
  61. virtual ~DOMObject();
  62. /// Destroys the DOMObject.
  63. private:
  64. DOMObject(const DOMObject&);
  65. DOMObject& operator = (const DOMObject&);
  66. mutable int _rc;
  67. };
  68. //
  69. // inlines
  70. //
  71. inline void DOMObject::duplicate() const
  72. {
  73. ++_rc;
  74. }
  75. inline void DOMObject::release() const
  76. {
  77. if (--_rc == 0)
  78. delete this;
  79. }
  80. } } // namespace Poco::XML
  81. #endif // DOM_DOMObject_INCLUDED