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