]> git.saurik.com Git - apple/xnu.git/blame - libkern/libkern/c++/OSDictionary.h
xnu-792.12.6.tar.gz
[apple/xnu.git] / libkern / libkern / c++ / OSDictionary.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/*
31 * Copyright (c) 1998-1999 Apple Computer, Inc. All rights reserved.
32 *
33 * HISTORY
34 *
35 * OSDictionary.h created by rsulack on Wed 17-Sep-1997
36 * OSDictionary.h converted to C++ by gvdl on Fri 1998-10-30
37 */
38
39#ifndef _IOKIT_IODICTIONARY_H
40#define _IOKIT_IODICTIONARY_H
41
42#include <libkern/c++/OSCollection.h>
43
44class OSArray;
45class OSSymbol;
46class OSString;
47
48/*!
49 @class OSDictionary
50 @abstract A collection class whose instances maintain a list of object references. Objects in the collection are acquired with unique associative keys.
51 @discussion
52 An instance of OSDictionary is a mutable container which contains a list of OSMetaClassBase derived object references and these objects are identified and acquired by unique associative keys. When an object is placed into a dictionary, a unique identifier or key must provided to identify the object within the collection. The key then must be provided to find the object within the collection. If an object is not found within the collection, a 0 is returned. Placing an object into a dictionary for a key, which already identifies an object within that dictionary, will replace the current object with the new object.
53
54 Objects placed into a dictionary are automatically retained and objects removed or replaced are automatically released. All objects are released when the collection is freed.
55*/
56class OSDictionary : public OSCollection
57{
58 OSDeclareDefaultStructors(OSDictionary)
59
60protected:
61 struct dictEntry {
62 const OSSymbol *key;
63 const OSMetaClassBase *value;
64 };
65 dictEntry *dictionary;
66 unsigned int count;
67 unsigned int capacity;
68 unsigned int capacityIncrement;
69
70 struct ExpansionData { };
71
72 /*! @var reserved
73 Reserved for future use. (Internal use only) */
74 ExpansionData *reserved;
75
76 // Member functions used by the OSCollectionIterator class.
77 virtual unsigned int iteratorSize() const;
78 virtual bool initIterator(void *iterator) const;
79 virtual bool getNextObjectForIterator(void *iterator, OSObject **ret) const;
80
81public:
82 /*!
83 @function withCapacity
84 @abstract A static constructor function to create and initialize an instance of OSDictionary.
85 @param capacity The initial storage capacity of the dictionary object.
86 @result Returns an instance of OSDictionary or 0 on failure.
87 */
88 static OSDictionary *withCapacity(unsigned int capacity);
89 /*!
90 @function withObjects
91 @abstract A static constructor function to create and initialize an instance of OSDictionary and populate it with objects provided.
92 @param objects A static array of OSMetaClassBase derived objects.
93 @param keys A static array of OSSymbol keys.
94 @param count The number of items to be placed into the dictionary.
95 @param capacity The initial storage capacity of the dictionary object. If 0, the capacity will be set to the size of 'count', else this value must be greater or equal to 'count'.
96 @result Returns an instance of OSDictionary or 0 on failure.
97 */
98 static OSDictionary *withObjects(const OSObject *objects[],
99 const OSSymbol *keys[],
100 unsigned int count,
101 unsigned int capacity = 0);
102 /*!
103 @function withObjects
104 @abstract A static constructor function to create and initialize an instance of OSDictionary and populate it with objects provided.
105 @param objects A static array of OSMetaClassBase derived objects.
106 @param keys A static array of OSString keys.
107 @param count The number of items to be placed into the dictionary.
108 @param capacity The initial storage capacity of the dictionary object. If 0, the capacity will be set to the size of 'count', else this value must be greater or equal to 'count'.
109 @result Returns an instance of OSDictionary or 0 on failure.
110 */
111 static OSDictionary *withObjects(const OSObject *objects[],
112 const OSString *keys[],
113 unsigned int count,
114 unsigned int capacity = 0);
115 /*!
116 @function withDictionary
117 @abstract A static constructor function to create and initialize an instance of OSDictionary and populate it with objects from another dictionary.
118 @param dict A dictionary whose contents will be placed in the new instance.
119 @param capacity The initial storage capacity of the dictionary object. If 0, the capacity will be set to the number of elements in the dictionary object, else the capacity must be greater than or equal to the number of elements in the dictionary.
120 @result Returns an instance of OSDictionary or 0 on failure.
121 */
122 static OSDictionary *withDictionary(const OSDictionary *dict,
123 unsigned int capacity = 0);
124
125 /*!
126 @function initWithCapacity
127 @abstract A member function to initialize an instance of OSDictionary.
128 @param capacity The initial storage capacity of the dictionary object.
129 @result Returns true if initialization succeeded or false on failure.
130 */
131 virtual bool initWithCapacity(unsigned int capacity);
132 /*!
133 @function initWithObjects
134 @abstract A member function to initialize an instance of OSDictionary and populate it with the provided objects and keys.
135 @param objects A static array of OSMetaClassBase derived objects to be placed into the dictionary.
136 @param keys A static array of OSSymbol keys which identify the corresponding objects provided in the 'objects' parameter.
137 @param count The number of objects provided to the dictionary.
138 @param capacity The initial storage capacity of the dictionary object. If 0, the capacity will be set to the size of 'count', else the capacity must be greater than or equal to the value of 'count'.
139 @result Returns true if initialization succeeded or false on failure.
140 */
141 virtual bool initWithObjects(const OSObject *objects[],
142 const OSSymbol *keys[],
143 unsigned int count,
144 unsigned int capacity = 0);
145 /*!
146 @function initWithObjects
147 @abstract A member function to initialize an instance of OSDictionary and populate it with the provided objects and keys.
148 @param objects A static array of OSMetaClassBase derived objects to be placed into the dictionary.
149 @param keys A static array of OSString keys which identify the corresponding objects provided in the 'objects' parameter.
150 @param count The number of objects provided to the dictionary.
151 @param capacity The initial storage capacity of the dictionary object. If 0, the capacity will be set to the size of 'count', else the capacity must be greater than or equal to the value of 'count'.
152 @result Returns true if initialization succeeded or false on failure.
153 */
154 virtual bool initWithObjects(const OSObject *objects[],
155 const OSString *keys[],
156 unsigned int count,
157 unsigned int capacity = 0);
158 /*!
159 @function initWithDictionary
160 @abstract A member function to initialize an instance of OSDictionary and populate it with the contents of another dictionary.
161 @param dict The dictionary containing the objects to be used to populate the receiving dictionary.
162 @param capacity The initial storage capacity of the dictionary. If 0, the value of capacity will be set to the number of elements in the dictionary object, else the value of capacity must be greater than or equal to the number of elements in the dictionary object.
163 @result Returns true if initialization succeeded or false on failure.
164 */
165 virtual bool initWithDictionary(const OSDictionary *dict,
166 unsigned int capacity = 0);
167 /*!
168 @function free
169 @abstract A member functions to deallocate and release all resources used by the OSDictionary instance.
170 @discussion This function should not be called directly, use release() instead.
171 */
172 virtual void free();
173
174 /*!
175 @function getCount
176 @abstract A member function which returns the current number of objects within the collection.
177 @result Returns the number of objects contained within the dictionary.
178 */
179 virtual unsigned int getCount() const;
180 /*!
181 @function getCapacity
182 @abstract A member function which returns the storage capacity of the collection.
183 @result Returns the storage capacity of the dictionary.
184 */
185 virtual unsigned int getCapacity() const;
186 /*!
187 @function getCapacityIncrement
188 @abstract A member function which returns the growth size for the collection.
189 */
190 virtual unsigned int getCapacityIncrement() const;
191 /*!
192 @function setCapacityIncrement
193 @abstract A member function to set the growth size of the collection.
194 @param increment The new growth size.
195 @result Returns the new capacity increment.
196 */
197 virtual unsigned int setCapacityIncrement(unsigned increment);
198
199 /*!
200 @function ensureCapacity
201 @abstract Member function to grow the size of the collection.
202 @param newCapacity The new capacity for the dictionary to expand to.
203 @result Returns the new capacity of the dictionary or the previous capacity upon error.
204 */
205 virtual unsigned int ensureCapacity(unsigned int newCapacity);
206
207 /*!
208 @function flushCollection
209 @abstract A member function which removes and releases all objects within the collection.
210 */
211 virtual void flushCollection();
212
213 /*!
214 @function setObject
215 @abstract A member function which places an object into the dictionary and identified by a unique key.
216 @param aKey A unique OSSymbol identifying the object placed within the collection.
217 @param anObject The object to be stored in the dictionary. It is automatically retained.
218 @result Returns true if the addition of an object was successful, false otherwise.
219 */
220 virtual bool setObject(const OSSymbol *aKey, const OSMetaClassBase *anObject);
221 /*!
222 @function setObject
223 @abstract A member function which places an object into the dictionary and identified by a unique key.
224 @param aKey A unique OSString identifying the object placed within the collection.
225 @param anObject The object to be stored in the dictionary. It is automatically retained.
226 @result Returns true if the addition of an object was successful, false otherwise.
227 */
228 virtual bool setObject(const OSString *aKey, const OSMetaClassBase *anObject);
229 /*!
230 @function setObject
231 @abstract A member function which places an object into the dictionary and identified by a unique key.
232 @param aKey A unique string identifying the object placed within the collection.
233 @param anObject The object to be stored in the dictionary. It is automatically retained.
234 @result Returns true if the addition of an object was successful, false otherwise.
235 */
236 virtual bool setObject(const char *aKey, const OSMetaClassBase *anObject);
237
238 /*!
239 @function removeObject
240 @abstract A member function which removes an object from the dictionary. The removed object is automatically released.
241 @param aKey A unique OSSymbol identifying the object to be removed from the dictionary.
242 */
243 virtual void removeObject(const OSSymbol *aKey);
244 /*!
245 @function removeObject
246 @abstract A member function which removes an object from the dictionary. The removed object is automatically released.
247 @param aKey A unique OSString identifying the object to be removed from the dictionary.
248 */
249 virtual void removeObject(const OSString *aKey);
250 /*!
251 @function removeObject
252 @abstract A member function which removes an object from the dictionary. The removed object is automatically released.
253 @param aKey A unique string identifying the object to be removed from the dictionary.
254 */
255 virtual void removeObject(const char *aKey);
256
257 /*!
258 @function merge
259 @abstract A member function which merges the contents of a dictionary into the receiver.
260 @param aDictionary The dictionary whose contents are to be merged with the receiver.
261 @result Returns true if the merger is successful, false otherwise.
262 @discussion If there are keys in 'aDictionary' which match keys in the receiving dictionary, then the objects in the receiver are replaced by those from 'aDictionary', the replaced objects are released.
263 */
264 virtual bool merge(const OSDictionary *aDictionary);
265
266 /*!
267 @function getObject
268 @abstract A member function to find an object in the dictionary associated by a given key.
269 @param aKey The unique OSSymbol key identifying the object to be returned to caller.
270 @result Returns a reference to the object corresponding to the given key, or 0 if the key does not exist in the dictionary.
271 */
272 virtual OSObject *getObject(const OSSymbol *aKey) const;
273 /*!
274 @function getObject
275 @abstract A member function to find an object in the dictionary associated by a given key.
276 @param aKey The unique OSString key identifying the object to be returned to caller.
277 @result Returns a reference to the object corresponding to the given key, or 0 if the key does not exist in the dictionary.
278 */
279 virtual OSObject *getObject(const OSString *aKey) const;
280 /*!
281 @function getObject
282 @abstract A member function to find an object in the dictionary associated by a given key.
283 @param aKey The unique string identifying the object to be returned to caller.
284 @result Returns a reference to the object corresponding to the given key, or 0 if the key does not exist in the dictionary.
285 */
286 virtual OSObject *getObject(const char *aKey) const;
287
288 /*!
289 @function isEqualTo
290 @abstract A member function to test the equality of the intersections of two dictionaries.
291 @param aDictionary The dictionary to be compared against the receiver.
292 @param keys An OSArray or OSDictionary containing the keys describing the intersection for the comparison.
293 @result Returns true if the intersections of the two dictionaries are equal.
294 */
295 virtual bool isEqualTo(const OSDictionary *aDictionary, const OSCollection *keys) const;
296 /*!
297 @function isEqualTo
298 @abstract A member function to test the equality of two dictionaries.
299 @param aDictionary The dictionary to be compared against the receiver.
300 @result Returns true if the dictionaries are equal.
301 */
302 virtual bool isEqualTo(const OSDictionary *aDictionary) const;
303 /*!
304 @function isEqualTo
305 @abstract A member function to test the equality between the receiver and an unknown object.
306 @param anObject An object to be compared against the receiver.
307 @result Returns true if the objects are equal.
308 */
309 virtual bool isEqualTo(const OSMetaClassBase *anObject) const;
310
311 /*!
312 @function serialize
313 @abstract A member function which archives the receiver.
314 @param s The OSSerialize object.
315 @result Returns true if serialization was successful, false if not.
316 */
317 virtual bool serialize(OSSerialize *s) const;
318
91447636
A
319 /*!
320 @function setOptions
321 @abstract This function is used to recursively set option bits in this dictionary and all child collections.
322 @param options Set the (options & mask) bits.
323 @param mask The mask of bits which need to be set, 0 to get the current value.
324 @result The options before the set operation, NB setOptions(?,0) returns the current value of this collection.
325 */
326 virtual unsigned setOptions(unsigned options, unsigned mask, void * = 0);
327
328 /*!
329 @function copyCollection
330 @abstract Do a deep copy of this dictionary and its collections.
331 @discussion This function copies this dictionary and all included collections recursively. Objects that don't derive from OSContainter are NOT copied, that is objects like OSString and OSData.
332 @param cycleDict Is a dictionary of all of the collections that have been, to start the copy at the top level just leave this field 0.
333 @result The newly copied collecton or 0 if insufficient memory
334 */
335 OSCollection *copyCollection(OSDictionary *cycleDict = 0);
336
1c79356b
A
337
338 OSMetaClassDeclareReservedUnused(OSDictionary, 0);
339 OSMetaClassDeclareReservedUnused(OSDictionary, 1);
340 OSMetaClassDeclareReservedUnused(OSDictionary, 2);
341 OSMetaClassDeclareReservedUnused(OSDictionary, 3);
342 OSMetaClassDeclareReservedUnused(OSDictionary, 4);
343 OSMetaClassDeclareReservedUnused(OSDictionary, 5);
344 OSMetaClassDeclareReservedUnused(OSDictionary, 6);
345 OSMetaClassDeclareReservedUnused(OSDictionary, 7);
346};
347
348#endif /* !_IOKIT_IODICTIONARY_H */