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