]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSSet.h
0e82f7a87706a7f6813724723ab34ac7e16f1568
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
)
94 * OSCollectionIterator interfaces.
96 virtual unsigned int iteratorSize() const;
97 virtual bool initIterator(void * iterator
) const;
98 virtual bool getNextObjectForIterator(void * iterator
, OSObject
** ret
) const;
100 struct ExpansionData
{ };
102 /* Reserved for future use. (Internal use only) */
103 ExpansionData
* reserved
;
109 * @function withCapacity
112 * Creates and initializes an empty OSSet.
114 * @param capacity The initial storage capacity of the new set object.
117 * An empty instance of OSSet
118 * with a retain count of 1;
119 * <code>NULL</code> on failure.
122 * <code>capacity</code> must be nonzero.
123 * The new OSSet will grow as needed to accommodate more key/object pairs
124 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
125 * for which the initial capacity is a hard limit).
127 static OSSet
* withCapacity(unsigned int capacity
);
131 * @function withObjects
134 * Creates and initializes an OSSet
135 * populated with objects provided.
137 * @param objects A C array of OSMetaClassBase-derived objects.
138 * @param count The number of objects to be placed into the set.
139 * @param capacity The initial storage capacity of the new set object.
140 * If 0, <code>count</code> is used; otherwise this value
141 * must be greater than or equal to <code>count</code>.
144 * An instance of OSSet
145 * containing the objects provided,
146 * with a retain count of 1;
147 * <code>NULL</code> on failure.
150 * <code>objects</code> must be non-<code>NULL</code>,
151 * and <code>count</code> must be nonzero.
152 * If <code>capacity</code> is nonzero,
153 * it must be greater than or equal to <code>count</code>.
154 * The new OSSet will grow as needed to accommodate more objects
155 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
156 * for which the initial capacity is a hard limit).
158 * The objects in <code>objects</code> are retained for storage in the new set,
161 static OSSet
* withObjects(
162 const OSObject
* objects
[],
164 unsigned int capacity
= 0);
168 * @function withArray
171 * Creates and initializes an OSSet
172 * populated with the contents of an OSArray.
174 * @param array An array whose objects will be stored in the new OSSet.
175 * @param capacity The initial storage capacity of the new set object.
176 * If 0, the capacity is set to the number of objects
177 * in <code>array</code>;
178 * otherwise <code>capacity</code> must be greater than or equal to
179 * the number of objects in <code>array</code>.
181 * An instance of OSSet containing
182 * the objects of <code>array</code>,
183 * with a retain count of 1;
184 * <code>NULL</code> on failure.
187 * Each distinct object in <code>array</code> is added to the new set.
189 * <code>array</code> must be non-<code>NULL</code>.
190 * If <code>capacity</code> is nonzero,
191 * it must be greater than or equal to <code>count</code>.
192 * The new OSSet will grow as needed to accommodate more key-object pairs
193 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
194 * for which the initial capacity is a hard limit).
196 * The objects in <code>array</code> are retained for storage in the new set,
199 static OSSet
* withArray(
200 const OSArray
* array
,
201 unsigned int capacity
= 0);
208 * Creates and initializes an OSSet
209 * populated with the contents of another OSSet.
211 * @param set An OSSet whose contents will be stored
212 * in the new instance.
213 * @param capacity The initial storage capacity of the set object.
214 * If 0, the capacity is set to the number of objects
215 * in <code>set</code>;
216 * otherwise <code>capacity</code> must be greater than or equal to
217 * the number of objects in <code>array</code>.
219 * An instance of OSArray
220 * containing the objects of <code>set</code>,
221 * with a retain count of 1;
222 * <code>NULL</code> on failure.
225 * <code>set</code> must be non-<code>NULL</code>.
226 * If <code>capacity</code> is nonzero,
227 * it must be greater than or equal to <code>count</code>.
228 * The array will grow as needed to accommodate more key-object pairs
229 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
230 * for which the initial capacity is a hard limit).
232 * The objects in <code>set</code> are retained for storage in the new set,
235 static OSSet
* withSet(const OSSet
* set
,
236 unsigned int capacity
= 0);
240 * @function initWithCapacity
243 * Initializes a new instance of OSSet.
245 * @param capacity The initial storage capacity of the new set object.
248 * <code>true</code> on success, <code>false</code> on failure.
251 * Not for general use. Use the static instance creation method
253 * //apple_ref/cpp/clm/OSSet/withCapacity/staticOSSet*\/(unsignedint)
254 * withCapacity@/link</code>
257 * <code>capacity</code> must be nonzero.
258 * The new set will grow as needed to accommodate more key/object pairs
259 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
260 * for which the initial capacity is a hard limit).
262 virtual bool initWithCapacity(unsigned int capacity
);
266 * @function initWithObjects
269 * Initializes a new OSSet populated with objects provided.
271 * @param objects A C array of OSObject-derived objects.
272 * @param count The number of objects to be placed into the set.
273 * @param capacity The initial storage capacity of the new set object.
274 * If 0, <code>count</code> is used; otherwise this value
275 * must be greater than or equal to <code>count</code>.
278 * <code>true</code> on success, <code>false</code> on failure.
281 * Not for general use. Use the static instance creation method
283 * //apple_ref/cpp/clm/OSSet/withObjects/staticOSSet*\/(constOSObject*,unsignedint,unsignedint)
284 * withObjects@/link</code>
287 * <code>objects</code> must be non-<code>NULL</code>,
288 * and <code>count</code> must be nonzero.
289 * If <code>capacity</code> is nonzero, it must be greater than or equal to <code>count</code>.
290 * The new array will grow as needed to accommodate more key-object pairs
291 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
292 * for which the initial capacity is a hard limit).
294 * The objects in <code>objects</code> are retained for storage in the new set,
297 virtual bool initWithObjects(
298 const OSObject
* objects
[],
300 unsigned int capacity
= 0);
304 * @function initWithArray
306 * @abstract Initializes a new OSSet
307 * populated with the contents of an OSArray.
309 * @param array An OSAray whose contents will be placed
310 * in the new instance.
311 * @param capacity The initial storage capacity of the new set object.
312 * If 0, the capacity is set
313 * to the number of objects in <code>array</code>;
314 * otherwise <code>capacity</code> must be greater than or equal to
315 * the number of objects in <code>array</code>.
318 * <code>true</code> on success, <code>false</code> on failure.
321 * Not for general use. Use the static instance creation method
323 * //apple_ref/cpp/clm/OSSet/withArray/staticOSSet*\/(constOSArray*,unsignedint)
324 * withArray@/link</code>
327 * <code>array</code> must be non-<code>NULL</code>.
328 * If <code>capacity</code> is nonzero,
329 * it must be greater than or equal to <code>count</code>.
330 * The new array will grow as needed to accommodate more key-object pairs
331 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
332 * for which the initial capacity is a hard limit).
334 * The objects in <code>array</code> are retained for storage in the new set,
337 virtual bool initWithArray(
338 const OSArray
* array
,
339 unsigned int capacity
= 0);
343 * @function initWithSet
346 * Initializes a new OSSet
347 * populated with the contents of another OSSet.
349 * @param set A set whose contents will be placed in the new instance.
350 * @param capacity The initial storage capacity of the new set object.
351 * If 0, the capacity is set
352 * to the number of objects in <code>set</code>;
353 * otherwise <code>capacity</code> must be greater than or equal to
354 * the number of objects in <code>set</code>.
357 * <code>true</code> on success, <code>false</code> on failure.
360 * Not for general use. Use the static instance creation method
361 * <code>@link withSet withSet@/link</code> instead.
363 * <code>set</code> must be non-<code>NULL</code>.
364 * If <code>capacity</code> is nonzero,
365 * it must be greater than or equal to <code>count</code>.
366 * The new set will grow as needed to accommodate more key-object pairs
367 * (<i>unlike</i> @link //apple_ref/doc/uid/20001503 CFMutableSet@/link,
368 * for which the initial capacity is a hard limit).
370 * The objects in <code>set</code> are retained for storage in the new set,
373 virtual bool initWithSet(const OSSet
*set
,
374 unsigned int capacity
= 0);
381 * Deallocates or releases any resources
382 * used by the OSSet instance.
385 * This function should not be called directly;
388 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
389 * release@/link</code>
399 * Returns the current number of objects within the set.
402 * The current number of objects within the set.
404 virtual unsigned int getCount() const;
408 * @function getCapacity
411 * Returns the number of objects the set
412 * can store without reallocating.
415 * The number objects the set
416 * can store without reallocating.
419 * OSSet objects grow when full to accommodate additional objects.
422 * //apple_ref/cpp/instm/OSSet/getCapacityIncrement/virtualunsignedint/()
423 * getCapacityIncrement@/link</code>
426 * //apple_ref/cpp/instm/OSSet/ensureCapacity/virtualunsignedint/(unsignedint)
427 * ensureCapacity@/link</code>.
429 virtual unsigned int getCapacity() const;
433 * @function getCapacityIncrement
436 * Returns the storage increment of the set.
439 * The storage increment of the set.
442 * An OSSet allocates storage for objects in multiples
443 * of the capacity increment.
445 virtual unsigned int getCapacityIncrement() const;
449 * @function setCapacityIncrement
452 * Sets the storage increment of the set.
455 * The new storage increment of the set,
456 * which may be different from the number requested.
459 * An OSSet allocates storage for objects in multiples
460 * of the capacity increment.
461 * Calling this function does not immediately reallocate storage.
463 virtual unsigned int setCapacityIncrement(unsigned increment
);
467 * @function ensureCapacity
470 * Ensures the set has enough space
471 * to store the requested number of distinct objects.
473 * @param newCapacity The total number of distinct objects the set
474 * should be able to store.
476 * The new capacity of the set,
477 * which may be different from the number requested
478 * (if smaller, reallocation of storage failed).
481 * This function immediately resizes the set, if necessary,
482 * to accommodate at least <code>newCapacity</code> distinct objects.
483 * If <code>newCapacity</code> is not greater than the current capacity,
484 * or if an allocation error occurs, the original capacity is returned.
486 * There is no way to reduce the capacity of an OSSet.
488 virtual unsigned int ensureCapacity(unsigned int newCapacity
);
492 * @function flushCollection
495 * Removes and releases all objects within the set.
498 * The set's capacity (and therefore direct memory consumption)
499 * is not reduced by this function.
501 virtual void flushCollection();
505 * @function setObject
508 * Adds an object to the OSSet if it is not already present.
510 * @param anObject The OSMetaClassBase-derived object to be added to the set.
513 * <code>true</code> if <code>anObject</code> was successfully
514 * added to the set, <code>false</code> otherwise
515 * (including if it was already in the set).
518 * The set adds storage to accomodate the new object, if necessary.
519 * If successfully added, the object is retained.
521 * A <code>false</code> return value can mean either
522 * that <code>anObject</code> is already present in the set,
523 * or that a memory allocation failure occurred.
524 * If you need to know whether the object
525 * is already present, use
526 * <code>@link containsObject containsObject@/link</code>.
528 virtual bool setObject(const OSMetaClassBase
* anObject
);
535 * Adds the contents of an OSArray to the set.
537 * @param array The OSArray object containing the objects to be added.
540 * <code>true</code> if all objects from <code>array</code>
541 * are successfully added the receiver (or were already present),
542 * <code>false</code> otherwise.
545 * This functions adds to the receiving set
546 * all objects from <code>array</code>
547 * that are not already in the receiving set.
548 * Objects added to the receiver are retained.
550 * In releases prior to 10.7, this function would return <code>false</code>
551 * if an object from <code>array</code> was already present in the set,
552 * or if <code>array</code> was empty.
553 * This is no longer the case, so this function correctly returns <code>true</code>
554 * when the semantic of merging is met.
556 virtual bool merge(const OSArray
* array
);
563 * Adds the contents of an OSet to the set.
565 * @param set The OSSet object containing the objects to be added.
568 * <code>true</code> if any object from <code>set</code>
569 * are successfully added the receiver (or were already present),
570 * <code>false</code> otherwise.
573 * This functions adds to the receiving set
574 * all objects from <code>set</code>
575 * that are not already in the receiving set.
576 * Objects added to the receiver are retained.
578 * In releases prior to 10.7, this function would return <code>false</code>
579 * if an object from <code>set</code> was already present in the set,
580 * or if <code>set</code> was empty.
581 * This is no longer the case, so this function correctly returns <code>true</code>
582 * when the semantic of merging is met.
584 virtual bool merge(const OSSet
* set
);
588 * @function removeObject
591 * Removes an object from the set.
593 * @param anObject The OSMetaClassBase-derived object
594 * to be removed from the set.
597 * The object removed from the set is released.
599 virtual void removeObject(const OSMetaClassBase
* anObject
);
603 * @function containsObject
606 * Checks the set for the presence of an object.
608 * @param anObject The OSMetaClassBase-derived object
609 * to check for in the set.
612 * <code>true</code> if <code>anObject</code> is present within the set,
613 * <code>false</code> otherwise.
616 * Pointer equality is used.
617 * This function returns <code>false</code> if passed <code>NULL</code>.
619 virtual bool containsObject(const OSMetaClassBase
* anObject
) const;
626 * Checks the set for the presence of an object.
628 * @param anObject The OSMetaClassBase-derived object
629 * to check for in the set.
632 * <code>true</code> if <code>anObject</code> is present
633 * within the set, <code>false</code> otherwise.
636 * Pointer equality is used. This function returns <code>false</code>
637 * if passed <code>NULL</code>.
639 * <code>@link containsObject containsObject@/link</code>
640 * checks for <code>NULL</code> first,
641 * and is therefore more efficient than this function.
643 virtual bool member(const OSMetaClassBase
* anObject
) const;
647 * @function getAnyObject
650 * Returns an arbitrary (not random) object from the set.
653 * An arbitrary (not random) object
654 * if one exists within the set.
657 * The returned object will be released if removed from the set;
658 * if you plan to store the reference, you should call
660 * //apple_ref/cpp/instm/OSObject/retain/virtualvoid/()
661 * retain@/link</code>
664 virtual OSObject
* getAnyObject() const;
668 * @function isEqualTo
671 * Tests the equality of two OSSet objects.
673 * @param aSet The set object being compared against the receiver.
675 * <code>true</code> if the two sets are equivalent,
676 * <code>false</code> otherwise.
679 * Two OSSet objects are considered equal if they have same count
680 * and the same object pointer values.
682 virtual bool isEqualTo(const OSSet
* aSet
) const;
686 * @function isEqualTo
689 * Tests the equality of an OSSet against an arbitrary object.
691 * @param anObject The object being compared against the receiver.
693 * <code>true</code> if the two objects are equivalent,
694 * <code>false</code> otherwise.
697 * An OSSet object is considered equal to another object if the other object
698 * is derived from OSSet and compares equal as a set.
700 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
704 * @function serialize
707 * Archives the receiver into the provided
708 * @link //apple_ref/doc/class/OSSerialize OSSerialize@/link object.
710 * @param serializer The OSSerialize object.
713 * <code>true</code> if serialization succeeds, <code>false</code> if not.
715 virtual bool serialize(OSSerialize
* serializer
) const;
719 * @function setOptions
722 * Recursively sets option bits in the set
723 * and all child collections.
725 * @param options A bitfield whose values turn the options on (1) or off (0).
726 * @param mask A mask indicating which bits
727 * in <code>options</code> to change.
728 * Pass 0 to get the whole current options bitfield
729 * without changing any settings.
730 * @param context Unused.
733 * The options bitfield as it was before the set operation.
736 * Kernel extensions should not call this function.
738 * Child collections' options are changed only if the receiving set's
739 * options actually change.
741 virtual unsigned setOptions(unsigned options
, unsigned mask
, void * context
= 0);
745 * @function copyCollection
748 * Creates a deep copy of this set and its child collections.
750 * @param cycleDict A dictionary of all of the collections
751 * that have been copied so far,
752 * which is used to track circular references.
753 * To start the copy at the top level,
754 * pass <code>NULL</code>.
757 * The newly copied set, with a retain count of 1,
758 * or <code>NULL</code> if there is insufficient memory to do the copy.
761 * The receiving set, and any collections it contains,
762 * recursively, are copied.
763 * Objects that are not derived from OSCollection are retained
764 * rather than copied.
766 OSCollection
*copyCollection(OSDictionary
*cycleDict
= 0);
768 OSMetaClassDeclareReservedUnused(OSSet
, 0);
769 OSMetaClassDeclareReservedUnused(OSSet
, 1);
770 OSMetaClassDeclareReservedUnused(OSSet
, 2);
771 OSMetaClassDeclareReservedUnused(OSSet
, 3);
772 OSMetaClassDeclareReservedUnused(OSSet
, 4);
773 OSMetaClassDeclareReservedUnused(OSSet
, 5);
774 OSMetaClassDeclareReservedUnused(OSSet
, 6);
775 OSMetaClassDeclareReservedUnused(OSSet
, 7);
778 #endif /* !_OS_OSSET_H */