]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSSet.h
6637fa2a860052d717154c072bb12ab219ab7042
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOSet.h created by rsulack on Thu 11-Jun-1998 */
29 /* IOSet.h converted to C++ by gvdl on Fri 1998-10-30 */
34 #include <libkern/c++/OSCollection.h>
42 * This header declares the OSSet collection class.
50 * OSSet provides an unordered set store of objects.
53 * OSSet is a container for Libkern C++ objects
55 * @link //apple_ref/doc/class/OSMetaClassBase OSMetaClassBase@/link,
56 * in particular @link //apple_ref/doc/class/OSObject OSObject@/link).
57 * Storage and access follow basic set logic: you can add or remove an object,
58 * and test whether the set contains a particular object.
59 * A given object is only stored in the set once,
60 * and there is no ordering of objects in the set.
61 * A subclass @link //apple_ref/doc/class/OSOrderedSet OSOrderedSet@/link,
62 * provides for ordered set logic.
64 * As with all Libkern collection classes,
65 * OSSet retains objects added to it,
66 * and releases objects removed from it.
67 * An OSSet also grows as necessary to accommodate new objects,
68 * <i>unlike</i> Core Foundation collections (it does not, however, shrink).
70 * <b>Use Restrictions</b>
72 * With very few exceptions in the I/O Kit, all Libkern-based C++
73 * classes, functions, and macros are <b>unsafe</b>
74 * to use in a primary interrupt context.
75 * Consult the I/O Kit documentation related to primary interrupts
76 * for more information.
78 * OSSet provides no concurrency protection;
79 * it's up to the usage context to provide any protection necessary.
80 * Some portions of the I/O Kit, such as
81 * @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link,
82 * handle synchronization via defined member functions for setting
85 class OSSet
: public OSCollection
87 OSDeclareDefaultStructors(OSSet
)
88 friend class OSSerialize
;
95 * OSCollectionIterator interfaces.
97 virtual unsigned int iteratorSize() const APPLE_KEXT_OVERRIDE
;
98 virtual bool initIterator(void * iterator
) const APPLE_KEXT_OVERRIDE
;
99 virtual bool getNextObjectForIterator(void * iterator
, OSObject
** ret
) const APPLE_KEXT_OVERRIDE
;
101 struct ExpansionData
{ };
103 /* Reserved for future use. (Internal use only) */
104 ExpansionData
* reserved
;
110 * @function withCapacity
113 * Creates and initializes an empty OSSet.
115 * @param capacity The initial storage capacity of the new set object.
118 * An empty instance of OSSet
119 * with a retain count of 1;
120 * <code>NULL</code> on failure.
123 * <code>capacity</code> must be nonzero.
124 * The new OSSet will grow as needed to accommodate more key/object pairs
125 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
126 * for which the initial capacity is a hard limit).
128 static OSSet
* withCapacity(unsigned int capacity
);
132 * @function withObjects
135 * Creates and initializes an OSSet
136 * populated with objects provided.
138 * @param objects A C array of OSMetaClassBase-derived objects.
139 * @param count The number of objects to be placed into the set.
140 * @param capacity The initial storage capacity of the new set object.
141 * If 0, <code>count</code> is used; otherwise this value
142 * must be greater than or equal to <code>count</code>.
145 * An instance of OSSet
146 * containing the objects provided,
147 * with a retain count of 1;
148 * <code>NULL</code> on failure.
151 * <code>objects</code> must be non-<code>NULL</code>,
152 * and <code>count</code> must be nonzero.
153 * If <code>capacity</code> is nonzero,
154 * it must be greater than or equal to <code>count</code>.
155 * The new OSSet will grow as needed to accommodate more objects
156 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
157 * for which the initial capacity is a hard limit).
159 * The objects in <code>objects</code> are retained for storage in the new set,
162 static OSSet
* withObjects(
163 const OSObject
* objects
[],
165 unsigned int capacity
= 0);
169 * @function withArray
172 * Creates and initializes an OSSet
173 * populated with the contents of an OSArray.
175 * @param array An array whose objects will be stored in the new OSSet.
176 * @param capacity The initial storage capacity of the new set object.
177 * If 0, the capacity is set to the number of objects
178 * in <code>array</code>;
179 * otherwise <code>capacity</code> must be greater than or equal to
180 * the number of objects in <code>array</code>.
182 * An instance of OSSet containing
183 * the objects of <code>array</code>,
184 * with a retain count of 1;
185 * <code>NULL</code> on failure.
188 * Each distinct object in <code>array</code> is added to the new set.
190 * <code>array</code> must be non-<code>NULL</code>.
191 * If <code>capacity</code> is nonzero,
192 * it must be greater than or equal to <code>count</code>.
193 * The new OSSet will grow as needed to accommodate more key-object pairs
194 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
195 * for which the initial capacity is a hard limit).
197 * The objects in <code>array</code> are retained for storage in the new set,
200 static OSSet
* withArray(
201 const OSArray
* array
,
202 unsigned int capacity
= 0);
209 * Creates and initializes an OSSet
210 * populated with the contents of another OSSet.
212 * @param set An OSSet whose contents will be stored
213 * in the new instance.
214 * @param capacity The initial storage capacity of the set object.
215 * If 0, the capacity is set to the number of objects
216 * in <code>set</code>;
217 * otherwise <code>capacity</code> must be greater than or equal to
218 * the number of objects in <code>array</code>.
220 * An instance of OSArray
221 * containing the objects of <code>set</code>,
222 * with a retain count of 1;
223 * <code>NULL</code> on failure.
226 * <code>set</code> must be non-<code>NULL</code>.
227 * If <code>capacity</code> is nonzero,
228 * it must be greater than or equal to <code>count</code>.
229 * The array will grow as needed to accommodate more key-object pairs
230 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
231 * for which the initial capacity is a hard limit).
233 * The objects in <code>set</code> are retained for storage in the new set,
236 static OSSet
* withSet(const OSSet
* set
,
237 unsigned int capacity
= 0);
241 * @function initWithCapacity
244 * Initializes a new instance of OSSet.
246 * @param capacity The initial storage capacity of the new set object.
249 * <code>true</code> on success, <code>false</code> on failure.
252 * Not for general use. Use the static instance creation method
254 * //apple_ref/cpp/clm/OSSet/withCapacity/staticOSSet*\/(unsignedint)
255 * withCapacity@/link</code>
258 * <code>capacity</code> must be nonzero.
259 * The new set will grow as needed to accommodate more key/object pairs
260 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
261 * for which the initial capacity is a hard limit).
263 virtual bool initWithCapacity(unsigned int capacity
);
267 * @function initWithObjects
270 * Initializes a new OSSet populated with objects provided.
272 * @param objects A C array of OSObject-derived objects.
273 * @param count The number of objects to be placed into the set.
274 * @param capacity The initial storage capacity of the new set object.
275 * If 0, <code>count</code> is used; otherwise this value
276 * must be greater than or equal to <code>count</code>.
279 * <code>true</code> on success, <code>false</code> on failure.
282 * Not for general use. Use the static instance creation method
284 * //apple_ref/cpp/clm/OSSet/withObjects/staticOSSet*\/(constOSObject*,unsignedint,unsignedint)
285 * withObjects@/link</code>
288 * <code>objects</code> must be non-<code>NULL</code>,
289 * and <code>count</code> must be nonzero.
290 * If <code>capacity</code> is nonzero, it must be greater than or equal to <code>count</code>.
291 * The new array will grow as needed to accommodate more key-object pairs
292 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
293 * for which the initial capacity is a hard limit).
295 * The objects in <code>objects</code> are retained for storage in the new set,
298 virtual bool initWithObjects(
299 const OSObject
* objects
[],
301 unsigned int capacity
= 0);
305 * @function initWithArray
307 * @abstract Initializes a new OSSet
308 * populated with the contents of an OSArray.
310 * @param array An OSAray whose contents will be placed
311 * in the new instance.
312 * @param capacity The initial storage capacity of the new set object.
313 * If 0, the capacity is set
314 * to the number of objects in <code>array</code>;
315 * otherwise <code>capacity</code> must be greater than or equal to
316 * the number of objects in <code>array</code>.
319 * <code>true</code> on success, <code>false</code> on failure.
322 * Not for general use. Use the static instance creation method
324 * //apple_ref/cpp/clm/OSSet/withArray/staticOSSet*\/(constOSArray*,unsignedint)
325 * withArray@/link</code>
328 * <code>array</code> must be non-<code>NULL</code>.
329 * If <code>capacity</code> is nonzero,
330 * it must be greater than or equal to <code>count</code>.
331 * The new array will grow as needed to accommodate more key-object pairs
332 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
333 * for which the initial capacity is a hard limit).
335 * The objects in <code>array</code> are retained for storage in the new set,
338 virtual bool initWithArray(
339 const OSArray
* array
,
340 unsigned int capacity
= 0);
344 * @function initWithSet
347 * Initializes a new OSSet
348 * populated with the contents of another OSSet.
350 * @param set A set whose contents will be placed in the new instance.
351 * @param capacity The initial storage capacity of the new set object.
352 * If 0, the capacity is set
353 * to the number of objects in <code>set</code>;
354 * otherwise <code>capacity</code> must be greater than or equal to
355 * the number of objects in <code>set</code>.
358 * <code>true</code> on success, <code>false</code> on failure.
361 * Not for general use. Use the static instance creation method
362 * <code>@link withSet withSet@/link</code> instead.
364 * <code>set</code> must be non-<code>NULL</code>.
365 * If <code>capacity</code> is nonzero,
366 * it must be greater than or equal to <code>count</code>.
367 * The new set will grow as needed to accommodate more key-object pairs
368 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
369 * for which the initial capacity is a hard limit).
371 * The objects in <code>set</code> are retained for storage in the new set,
374 virtual bool initWithSet(const OSSet
*set
,
375 unsigned int capacity
= 0);
382 * Deallocates or releases any resources
383 * used by the OSSet instance.
386 * This function should not be called directly;
389 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
390 * release@/link</code>
393 virtual void free() APPLE_KEXT_OVERRIDE
;
400 * Returns the current number of objects within the set.
403 * The current number of objects within the set.
405 virtual unsigned int getCount() const APPLE_KEXT_OVERRIDE
;
409 * @function getCapacity
412 * Returns the number of objects the set
413 * can store without reallocating.
416 * The number objects the set
417 * can store without reallocating.
420 * OSSet objects grow when full to accommodate additional objects.
423 * //apple_ref/cpp/instm/OSSet/getCapacityIncrement/virtualunsignedint/()
424 * getCapacityIncrement@/link</code>
427 * //apple_ref/cpp/instm/OSSet/ensureCapacity/virtualunsignedint/(unsignedint)
428 * ensureCapacity@/link</code>.
430 virtual unsigned int getCapacity() const APPLE_KEXT_OVERRIDE
;
434 * @function getCapacityIncrement
437 * Returns the storage increment of the set.
440 * The storage increment of the set.
443 * An OSSet allocates storage for objects in multiples
444 * of the capacity increment.
446 virtual unsigned int getCapacityIncrement() const APPLE_KEXT_OVERRIDE
;
450 * @function setCapacityIncrement
453 * Sets the storage increment of the set.
456 * The new storage increment of the set,
457 * which may be different from the number requested.
460 * An OSSet allocates storage for objects in multiples
461 * of the capacity increment.
462 * Calling this function does not immediately reallocate storage.
464 virtual unsigned int setCapacityIncrement(unsigned increment
) APPLE_KEXT_OVERRIDE
;
468 * @function ensureCapacity
471 * Ensures the set has enough space
472 * to store the requested number of distinct objects.
474 * @param newCapacity The total number of distinct objects the set
475 * should be able to store.
477 * The new capacity of the set,
478 * which may be different from the number requested
479 * (if smaller, reallocation of storage failed).
482 * This function immediately resizes the set, if necessary,
483 * to accommodate at least <code>newCapacity</code> distinct objects.
484 * If <code>newCapacity</code> is not greater than the current capacity,
485 * or if an allocation error occurs, the original capacity is returned.
487 * There is no way to reduce the capacity of an OSSet.
489 virtual unsigned int ensureCapacity(unsigned int newCapacity
) APPLE_KEXT_OVERRIDE
;
493 * @function flushCollection
496 * Removes and releases all objects within the set.
499 * The set's capacity (and therefore direct memory consumption)
500 * is not reduced by this function.
502 virtual void flushCollection() APPLE_KEXT_OVERRIDE
;
506 * @function setObject
509 * Adds an object to the OSSet if it is not already present.
511 * @param anObject The OSMetaClassBase-derived object to be added to the set.
514 * <code>true</code> if <code>anObject</code> was successfully
515 * added to the set, <code>false</code> otherwise
516 * (including if it was already in the set).
519 * The set adds storage to accomodate the new object, if necessary.
520 * If successfully added, the object is retained.
522 * A <code>false</code> return value can mean either
523 * that <code>anObject</code> is already present in the set,
524 * or that a memory allocation failure occurred.
525 * If you need to know whether the object
526 * is already present, use
527 * <code>@link containsObject containsObject@/link</code>.
529 virtual bool setObject(const OSMetaClassBase
* anObject
);
536 * Adds the contents of an OSArray to the set.
538 * @param array The OSArray object containing the objects to be added.
541 * <code>true</code> if all objects from <code>array</code>
542 * are successfully added the receiver (or were already present),
543 * <code>false</code> otherwise.
546 * This functions adds to the receiving set
547 * all objects from <code>array</code>
548 * that are not already in the receiving set.
549 * Objects added to the receiver are retained.
551 * In releases prior to 10.7, this function would return <code>false</code>
552 * if an object from <code>array</code> was already present in the set,
553 * or if <code>array</code> was empty.
554 * This is no longer the case, so this function correctly returns <code>true</code>
555 * when the semantic of merging is met.
557 virtual bool merge(const OSArray
* array
);
564 * Adds the contents of an OSet to the set.
566 * @param set The OSSet object containing the objects to be added.
569 * <code>true</code> if any object from <code>set</code>
570 * are successfully added the receiver (or were already present),
571 * <code>false</code> otherwise.
574 * This functions adds to the receiving set
575 * all objects from <code>set</code>
576 * that are not already in the receiving set.
577 * Objects added to the receiver are retained.
579 * In releases prior to 10.7, this function would return <code>false</code>
580 * if an object from <code>set</code> was already present in the set,
581 * or if <code>set</code> was empty.
582 * This is no longer the case, so this function correctly returns <code>true</code>
583 * when the semantic of merging is met.
585 virtual bool merge(const OSSet
* set
);
589 * @function removeObject
592 * Removes an object from the set.
594 * @param anObject The OSMetaClassBase-derived object
595 * to be removed from the set.
598 * The object removed from the set is released.
600 virtual void removeObject(const OSMetaClassBase
* anObject
);
604 * @function containsObject
607 * Checks the set for the presence of an object.
609 * @param anObject The OSMetaClassBase-derived object
610 * to check for in the set.
613 * <code>true</code> if <code>anObject</code> is present within the set,
614 * <code>false</code> otherwise.
617 * Pointer equality is used.
618 * This function returns <code>false</code> if passed <code>NULL</code>.
620 virtual bool containsObject(const OSMetaClassBase
* anObject
) const;
627 * Checks the set for the presence of an object.
629 * @param anObject The OSMetaClassBase-derived object
630 * to check for in the set.
633 * <code>true</code> if <code>anObject</code> is present
634 * within the set, <code>false</code> otherwise.
637 * Pointer equality is used. This function returns <code>false</code>
638 * if passed <code>NULL</code>.
640 * <code>@link containsObject containsObject@/link</code>
641 * checks for <code>NULL</code> first,
642 * and is therefore more efficient than this function.
644 virtual bool member(const OSMetaClassBase
* anObject
) const;
648 * @function getAnyObject
651 * Returns an arbitrary (not random) object from the set.
654 * An arbitrary (not random) object
655 * if one exists within the set.
658 * The returned object will be released if removed from the set;
659 * if you plan to store the reference, you should call
661 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
662 * retain@/link</code>
665 virtual OSObject
* getAnyObject() const;
669 * @function isEqualTo
672 * Tests the equality of two OSSet objects.
674 * @param aSet The set object being compared against the receiver.
676 * <code>true</code> if the two sets are equivalent,
677 * <code>false</code> otherwise.
680 * Two OSSet objects are considered equal if they have same count
681 * and the same object pointer values.
683 virtual bool isEqualTo(const OSSet
* aSet
) const;
687 * @function isEqualTo
690 * Tests the equality of an OSSet against an arbitrary object.
692 * @param anObject The object being compared against the receiver.
694 * <code>true</code> if the two objects are equivalent,
695 * <code>false</code> otherwise.
698 * An OSSet object is considered equal to another object if the other object
699 * is derived from OSSet and compares equal as a set.
701 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const APPLE_KEXT_OVERRIDE
;
705 * @function serialize
708 * Archives the receiver into the provided
709 * @link //apple_ref/doc/class/OSSerialize OSSerialize@/link object.
711 * @param serializer The OSSerialize object.
714 * <code>true</code> if serialization succeeds, <code>false</code> if not.
716 virtual bool serialize(OSSerialize
* serializer
) const APPLE_KEXT_OVERRIDE
;
720 * @function setOptions
723 * Recursively sets option bits in the set
724 * and all child collections.
726 * @param options A bitfield whose values turn the options on (1) or off (0).
727 * @param mask A mask indicating which bits
728 * in <code>options</code> to change.
729 * Pass 0 to get the whole current options bitfield
730 * without changing any settings.
731 * @param context Unused.
734 * The options bitfield as it was before the set operation.
737 * Kernel extensions should not call this function.
739 * Child collections' options are changed only if the receiving set's
740 * options actually change.
742 virtual unsigned setOptions(unsigned options
, unsigned mask
, void * context
= 0) APPLE_KEXT_OVERRIDE
;
746 * @function copyCollection
749 * Creates a deep copy of this set and its child collections.
751 * @param cycleDict A dictionary of all of the collections
752 * that have been copied so far,
753 * which is used to track circular references.
754 * To start the copy at the top level,
755 * pass <code>NULL</code>.
758 * The newly copied set, with a retain count of 1,
759 * or <code>NULL</code> if there is insufficient memory to do the copy.
762 * The receiving set, and any collections it contains,
763 * recursively, are copied.
764 * Objects that are not derived from OSCollection are retained
765 * rather than copied.
767 OSCollection
*copyCollection(OSDictionary
*cycleDict
= 0) APPLE_KEXT_OVERRIDE
;
769 OSMetaClassDeclareReservedUnused(OSSet
, 0);
770 OSMetaClassDeclareReservedUnused(OSSet
, 1);
771 OSMetaClassDeclareReservedUnused(OSSet
, 2);
772 OSMetaClassDeclareReservedUnused(OSSet
, 3);
773 OSMetaClassDeclareReservedUnused(OSSet
, 4);
774 OSMetaClassDeclareReservedUnused(OSSet
, 5);
775 OSMetaClassDeclareReservedUnused(OSSet
, 6);
776 OSMetaClassDeclareReservedUnused(OSSet
, 7);
779 #endif /* !_OS_OSSET_H */