Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 : *
7 : * This Original Code has been modified by IBM Corporation. Modifications made by IBM
8 : * described herein are Copyright (c) International Business Machines Corporation, 2000.
9 : * Modifications to Mozilla code or documentation identified per MPL Section 3.3
10 : *
11 : * Date Modified by Description of modification
12 : * 04/20/2000 IBM Corp. OS/2 VisualAge build.
13 : */
14 :
15 : /**
16 : * nsPropertyTable allows a set of arbitrary key/value pairs to be stored
17 : * for any number of nodes, in a global hashtable rather than on the nodes
18 : * themselves. Nodes can be any type of object; the hashtable keys are
19 : * nsIAtom pointers, and the values are void pointers.
20 : */
21 :
22 : #ifndef nsPropertyTable_h_
23 : #define nsPropertyTable_h_
24 :
25 : #include "mozilla/MemoryReporting.h"
26 : #include "nscore.h"
27 :
28 : class nsIAtom;
29 :
30 : typedef void
31 : (*NSPropertyFunc)(void *aObject,
32 : nsIAtom *aPropertyName,
33 : void *aPropertyValue,
34 : void *aData);
35 :
36 : /**
37 : * Callback type for property destructors. |aObject| is the object
38 : * the property is being removed for, |aPropertyName| is the property
39 : * being removed, |aPropertyValue| is the value of the property, and |aData|
40 : * is the opaque destructor data that was passed to SetProperty().
41 : **/
42 : typedef NSPropertyFunc NSPropertyDtorFunc;
43 : class nsINode;
44 : class nsIFrame;
45 :
46 : class nsPropertyOwner
47 : {
48 : public:
49 5331 : nsPropertyOwner(const nsPropertyOwner& aOther) : mObject(aOther.mObject) {}
50 :
51 : // These are the types of objects that can own properties. No object should
52 : // inherit more then one of these classes.
53 : // To add support for more types just add to this list.
54 5350 : MOZ_IMPLICIT nsPropertyOwner(const nsINode* aObject) : mObject(aObject) {}
55 : MOZ_IMPLICIT nsPropertyOwner(const nsIFrame* aObject) : mObject(aObject) {}
56 :
57 8422 : operator const void*() { return mObject; }
58 17 : const void* get() { return mObject; }
59 :
60 : private:
61 : const void* mObject;
62 : };
63 :
64 : class nsPropertyTable
65 : {
66 : public:
67 : /**
68 : * Get the value of the property |aPropertyName| for node |aObject|.
69 : * |aResult|, if supplied, is filled in with a return status code.
70 : **/
71 4424 : void* GetProperty(const nsPropertyOwner& aObject,
72 : nsIAtom *aPropertyName,
73 : nsresult *aResult = nullptr)
74 : {
75 4424 : return GetPropertyInternal(aObject, aPropertyName, false, aResult);
76 : }
77 :
78 : /**
79 : * Set the value of the property |aPropertyName| to
80 : * |aPropertyValue| for node |aObject|. |aDtor| is a destructor for the
81 : * property value to be called if the property is removed. It can be null
82 : * if no destructor is required. |aDtorData| is an optional pointer to an
83 : * opaque context to be passed to the property destructor. Note that the
84 : * destructor is global for each property name regardless of node; it is an
85 : * error to set a given property with a different destructor than was used
86 : * before (this will return NS_ERROR_INVALID_ARG). If aOldValue is non-null
87 : * it will contain the old value after the function returns (the destructor
88 : * for the old value will not be run in that case). If |aTransfer| is true
89 : * the property will be transfered to the new table when the property table
90 : * for |aObject| changes (currently the tables for nodes are owned by their
91 : * ownerDocument, so if the ownerDocument for a node changes, its property
92 : * table changes too). If |aTransfer| is false the property will just be
93 : * deleted instead.
94 : */
95 402 : nsresult SetProperty(const nsPropertyOwner& aObject,
96 : nsIAtom *aPropertyName,
97 : void *aPropertyValue,
98 : NSPropertyDtorFunc aDtor,
99 : void *aDtorData,
100 : bool aTransfer = false,
101 : void **aOldValue = nullptr)
102 : {
103 804 : return SetPropertyInternal(aObject, aPropertyName, aPropertyValue,
104 804 : aDtor, aDtorData, aTransfer, aOldValue);
105 : }
106 :
107 : /**
108 : * Delete the property |aPropertyName| in the global category for object
109 : * |aObject|. The property's destructor function will be called.
110 : */
111 : nsresult DeleteProperty(nsPropertyOwner aObject,
112 : nsIAtom *aPropertyName);
113 :
114 : /**
115 : * Unset the property |aPropertyName| in the global category for object
116 : * |aObject|, but do not call the property's destructor function. The
117 : * property value is returned.
118 : */
119 488 : void* UnsetProperty(const nsPropertyOwner& aObject,
120 : nsIAtom *aPropertyName,
121 : nsresult *aStatus = nullptr)
122 : {
123 488 : return GetPropertyInternal(aObject, aPropertyName, true, aStatus);
124 : }
125 :
126 : /**
127 : * Deletes all of the properties for object |aObject|, calling the
128 : * destructor function for each property.
129 : */
130 : void DeleteAllPropertiesFor(nsPropertyOwner aObject);
131 :
132 : /**
133 : * Transfers all properties for object |aObject| that were set with the
134 : * |aTransfer| argument as true to |aTable|. Deletes the other properties
135 : * for object |aObject|, calling the destructor function for each property.
136 : * If transfering a property fails, this deletes all the properties for
137 : * object |aObject|.
138 : */
139 : nsresult
140 : TransferOrDeleteAllPropertiesFor(nsPropertyOwner aObject,
141 : nsPropertyTable *aOtherTable);
142 :
143 : /**
144 : * Enumerate the properties for object |aObject|.
145 : * For every property |aCallback| will be called with as arguments |aObject|,
146 : * the property name, the property value and |aData|.
147 : */
148 : void Enumerate(nsPropertyOwner aObject,
149 : NSPropertyFunc aCallback, void *aData);
150 :
151 : /**
152 : * Enumerate all the properties.
153 : * For every property |aCallback| will be called with arguments the owner,
154 : * the property name, the property value and |aData|.
155 : */
156 : void EnumerateAll(NSPropertyFunc aCallback, void *aData);
157 :
158 : /**
159 : * Deletes all of the properties for all objects in the property
160 : * table, calling the destructor function for each property.
161 : */
162 : void DeleteAllProperties();
163 :
164 56 : nsPropertyTable() : mPropertyList(nullptr) {}
165 0 : ~nsPropertyTable() {
166 0 : DeleteAllProperties();
167 0 : }
168 :
169 : /**
170 : * Function useable as destructor function for property data that is
171 : * XPCOM objects. The function will call NS_IF_RELASE on the value
172 : * to destroy it.
173 : */
174 : static void SupportsDtorFunc(void *aObject, nsIAtom *aPropertyName,
175 : void *aPropertyValue, void *aData);
176 :
177 : class PropertyList;
178 :
179 : size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
180 : size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
181 :
182 : private:
183 : void DestroyPropertyList();
184 : PropertyList* GetPropertyListFor(nsIAtom *aPropertyName) const;
185 : void* GetPropertyInternal(nsPropertyOwner aObject,
186 : nsIAtom *aPropertyName,
187 : bool aRemove,
188 : nsresult *aStatus);
189 : nsresult SetPropertyInternal(nsPropertyOwner aObject,
190 : nsIAtom *aPropertyName,
191 : void *aPropertyValue,
192 : NSPropertyDtorFunc aDtor,
193 : void *aDtorData,
194 : bool aTransfer,
195 : void **aOldValue);
196 :
197 : PropertyList *mPropertyList;
198 : };
199 : #endif
|