1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim:cindent:ts=2:et:sw=2:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is mozilla.org code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 1998
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK *****
39 : *
40 : * This Original Code has been modified by IBM Corporation. Modifications made by IBM
41 : * described herein are Copyright (c) International Business Machines Corporation, 2000.
42 : * Modifications to Mozilla code or documentation identified per MPL Section 3.3
43 : *
44 : * Date Modified by Description of modification
45 : * 04/20/2000 IBM Corp. OS/2 VisualAge build.
46 : */
47 :
48 : /**
49 : * nsPropertyTable allows a set of arbitrary key/value pairs to be stored
50 : * for any number of nodes, in a global hashtable rather than on the nodes
51 : * themselves. Nodes can be any type of object; the hashtable keys are
52 : * nsIAtom pointers, and the values are void pointers.
53 : */
54 :
55 : #ifndef nsPropertyTable_h_
56 : #define nsPropertyTable_h_
57 :
58 : #include "nscore.h"
59 :
60 : class nsIAtom;
61 : typedef PRUptrdiff PtrBits;
62 :
63 : typedef void
64 : (*NSPropertyFunc)(void *aObject,
65 : nsIAtom *aPropertyName,
66 : void *aPropertyValue,
67 : void *aData);
68 :
69 : /**
70 : * Callback type for property destructors. |aObject| is the object
71 : * the property is being removed for, |aPropertyName| is the property
72 : * being removed, |aPropertyValue| is the value of the property, and |aData|
73 : * is the opaque destructor data that was passed to SetProperty().
74 : **/
75 : typedef NSPropertyFunc NSPropertyDtorFunc;
76 : class nsINode;
77 : class nsIFrame;
78 :
79 : class nsPropertyOwner
80 : {
81 : public:
82 0 : nsPropertyOwner(const nsPropertyOwner& aOther) : mObject(aOther.mObject) {}
83 :
84 : // These are the types of objects that can own properties. No object should
85 : // inherit more then one of these classes.
86 : // To add support for more types just add to this list.
87 : nsPropertyOwner(const nsINode* aObject) : mObject(aObject) {}
88 : nsPropertyOwner(const nsIFrame* aObject) : mObject(aObject) {}
89 :
90 0 : operator const void*() { return mObject; }
91 0 : const void* get() { return mObject; }
92 :
93 : private:
94 : const void* mObject;
95 : };
96 :
97 : class nsPropertyTable
98 : {
99 : public:
100 : /**
101 : * Get the value of the property |aPropertyName| for node |aObject|.
102 : * |aResult|, if supplied, is filled in with a return status code.
103 : **/
104 : void* GetProperty(nsPropertyOwner aObject,
105 : nsIAtom *aPropertyName,
106 : nsresult *aResult = nsnull)
107 : {
108 : return GetPropertyInternal(aObject, aPropertyName, false, aResult);
109 : }
110 :
111 : /**
112 : * Set the value of the property |aPropertyName| to
113 : * |aPropertyValue| for node |aObject|. |aDtor| is a destructor for the
114 : * property value to be called if the property is removed. It can be null
115 : * if no destructor is required. |aDtorData| is an optional pointer to an
116 : * opaque context to be passed to the property destructor. Note that the
117 : * destructor is global for each property name regardless of node; it is an
118 : * error to set a given property with a different destructor than was used
119 : * before (this will return NS_ERROR_INVALID_ARG). If aOldValue is non-null
120 : * it will contain the old value after the function returns (the destructor
121 : * for the old value will not be run in that case). If |aTransfer| is true
122 : * the property will be transfered to the new table when the property table
123 : * for |aObject| changes (currently the tables for nodes are owned by their
124 : * ownerDocument, so if the ownerDocument for a node changes, its property
125 : * table changes too). If |aTransfer| is false the property will just be
126 : * deleted instead.
127 : */
128 0 : NS_HIDDEN_(nsresult) SetProperty(nsPropertyOwner aObject,
129 : nsIAtom *aPropertyName,
130 : void *aPropertyValue,
131 : NSPropertyDtorFunc aDtor,
132 : void *aDtorData,
133 : bool aTransfer = false,
134 : void **aOldValue = nsnull)
135 : {
136 : return SetPropertyInternal(aObject, aPropertyName, aPropertyValue,
137 0 : aDtor, aDtorData, aTransfer, aOldValue);
138 : }
139 :
140 : /**
141 : * Delete the property |aPropertyName| in the global category for object
142 : * |aObject|. The property's destructor function will be called.
143 : */
144 : NS_HIDDEN_(nsresult) DeleteProperty(nsPropertyOwner aObject,
145 : nsIAtom *aPropertyName);
146 :
147 : /**
148 : * Unset the property |aPropertyName| in the global category for object
149 : * |aObject|, but do not call the property's destructor function. The
150 : * property value is returned.
151 : */
152 : void* UnsetProperty(nsPropertyOwner aObject,
153 : nsIAtom *aPropertyName,
154 : nsresult *aStatus = nsnull)
155 : {
156 : return GetPropertyInternal(aObject, aPropertyName, true, aStatus);
157 : }
158 :
159 : /**
160 : * Deletes all of the properties for object |aObject|, calling the
161 : * destructor function for each property.
162 : */
163 : NS_HIDDEN_(void) DeleteAllPropertiesFor(nsPropertyOwner aObject);
164 :
165 : /**
166 : * Transfers all properties for object |aObject| that were set with the
167 : * |aTransfer| argument as true to |aTable|. Deletes the other properties
168 : * for object |aObject|, calling the destructor function for each property.
169 : * If transfering a property fails, this deletes all the properties for
170 : * object |aObject|.
171 : */
172 : NS_HIDDEN_(nsresult)
173 : TransferOrDeleteAllPropertiesFor(nsPropertyOwner aObject,
174 : nsPropertyTable *aOtherTable);
175 :
176 : /**
177 : * Enumerate the properties for object |aObject|.
178 : * For every property |aCallback| will be called with as arguments |aObject|,
179 : * the property name, the property value and |aData|.
180 : */
181 : NS_HIDDEN_(void) Enumerate(nsPropertyOwner aObject,
182 : NSPropertyFunc aCallback, void *aData);
183 :
184 : /**
185 : * Enumerate all the properties.
186 : * For every property |aCallback| will be called with arguments the owner,
187 : * the property name, the property value and |aData|.
188 : */
189 : NS_HIDDEN_(void) EnumerateAll(NSPropertyFunc aCallback, void *aData);
190 :
191 : /**
192 : * Deletes all of the properties for all objects in the property
193 : * table, calling the destructor function for each property.
194 : */
195 : NS_HIDDEN_(void) DeleteAllProperties();
196 :
197 : nsPropertyTable() : mPropertyList(nsnull) {}
198 : ~nsPropertyTable() {
199 : DeleteAllProperties();
200 : }
201 :
202 : /**
203 : * Function useable as destructor function for property data that is
204 : * XPCOM objects. The function will call NS_IF_RELASE on the value
205 : * to destroy it.
206 : */
207 : static void SupportsDtorFunc(void *aObject, nsIAtom *aPropertyName,
208 : void *aPropertyValue, void *aData);
209 :
210 : class PropertyList;
211 :
212 : private:
213 : NS_HIDDEN_(void) DestroyPropertyList();
214 : NS_HIDDEN_(PropertyList*) GetPropertyListFor(nsIAtom *aPropertyName) const;
215 : NS_HIDDEN_(void*) GetPropertyInternal(nsPropertyOwner aObject,
216 : nsIAtom *aPropertyName,
217 : bool aRemove,
218 : nsresult *aStatus);
219 : NS_HIDDEN_(nsresult) SetPropertyInternal(nsPropertyOwner aObject,
220 : nsIAtom *aPropertyName,
221 : void *aPropertyValue,
222 : NSPropertyDtorFunc aDtor,
223 : void *aDtorData,
224 : bool aTransfer,
225 : void **aOldValue);
226 :
227 : PropertyList *mPropertyList;
228 : };
229 : #endif
|