]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSCollection.h
xnu-792.10.96.tar.gz
[apple/xnu.git] / libkern / libkern / c++ / OSCollection.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* IOCollection.h created by gvdl on Thu 1998-10-22 */
23
24 #ifndef _OS_OSCOLLECTION_H
25 #define _OS_OSCOLLECTION_H
26
27 #include <libkern/c++/OSObject.h>
28
29 class OSDictionary;
30
31 /*!
32 @class OSCollection
33 @abstract Abstract super class for all collections.
34 @discussion
35 OSCollection is the abstract super class for all OSObject derived collections and provides the necessary interfaces for managing storage space and iteration through a collection.
36 */
37 class OSCollection : public OSObject
38 {
39 friend class OSCollectionIterator;
40
41 OSDeclareAbstractStructors(OSCollection);
42
43 struct ExpansionData { };
44
45 protected:
46 unsigned int updateStamp;
47
48 private:
49 /* Reserved for future use. (Internal use only) */
50 // ExpansionData *reserved;
51 unsigned int fOptions;
52
53 protected:
54 // Member functions used by the OSCollectionIterator class.
55 /*
56 @function iteratorSize
57 @abstract A pure virtual member function to return the size of the iterator context.
58 @result Returns an integer size for the storage space required to contain context necessary for iterating through a collection.
59 @discussion
60 This member function is called by an OSCollectionIterator object to allow it to allocate enough storage space for the iterator context. This context contains the data necessary to iterate through the collection when getNextObjectForIterator() is called.
61 */
62 virtual unsigned int iteratorSize() const = 0;
63 /*
64 @function initIterator
65 @abstract Pure virtual member function to allocate and initialize the iterator context data.
66 @param iterator The iterator context.
67 @result Returns true if initialization was successful, false otherwise.
68 */
69 virtual bool initIterator(void *iterator) const = 0;
70 /*
71 @function getNextObjectForIterator
72 @abstract A pure virtual member function which returns the next member of a collection.
73 @param iterator The iterator context.
74 @param ret The object returned to the caller.
75 @result Returns true if an object was found, false otherwise.
76 @discussion
77 This is the entry point used by an OSCollectionIterator object to advance to next object in the collection. The iterator context is passed to the receiver to allow it to find the location of the current object and then advance the iterator context to the next object.
78 */
79 virtual bool getNextObjectForIterator(void *iterator, OSObject **ret) const = 0;
80
81 /*
82 @function init
83 @abstract A member function to initialize the OSCollection object.
84 @result Returns true if an object was initialized successfully, false otherwise.
85 @discussion
86 This function is used to initialize state within a newly created OSCollection object.
87 */
88 virtual bool init();
89
90 public:
91 enum {
92 kImmutable = 0x00000001,
93 kMASK = (unsigned) -1
94 };
95
96 /*
97 @function haveUpdated
98 @abstract A member function to track of all updates to the collection.
99 */
100 void haveUpdated();
101
102 /*
103 @function getCount
104 @abstract A pure virtual member function which returns the number of objects in the collection subclass.
105 @results Returns the number objects in a collection.
106 */
107 virtual unsigned int getCount() const = 0;
108 /*
109 @function getCapacity
110 @abstract A pure virtual member function which returns the storage space in the collection subclass.
111 @results Returns the number objects in a collection.
112 */
113 virtual unsigned int getCapacity() const = 0;
114 /*
115 @function getCapacityIncrement
116 @abstract A pure virtual member function which returns the growth factor of the collection subclass.
117 @results Returns the size by which the collection subclass should grow.
118 */
119 virtual unsigned int getCapacityIncrement() const = 0;
120 /*
121 @function setCapacityIncrement
122 @abstract A pure virtual member function which sets the growth factor of the collection subclass.
123 @param increment The new size by which the capacity of the collection should grow.
124 @results Returns the new capacity increment.
125 */
126 virtual unsigned int setCapacityIncrement(unsigned increment) = 0;
127
128 /*
129 @function ensureCapacity
130 @abstract A pure virtual member function which
131 @param newCapacity
132 @result
133 */
134 virtual unsigned int ensureCapacity(unsigned int newCapacity) = 0;
135
136 /*
137 @function flushCollection
138 @abstract A pure virtual member function which
139 */
140 virtual void flushCollection() = 0;
141
142 /*!
143 @function setOptions
144 @abstract This function is used to recursively set option bits in this collection and all child collections.
145 @discussion setOptions is a recursive function but the OSCollection class itself does not know the structure of the particular collection. This means that all derived classes are expected to override this method and recurse if the old value of the option was NOT set, which is why the old value is returned. As this function is a reserved function override it is very multi purpose. It can be used to get & set the options,
146 @param options Set the (options & mask) bits.
147 @param mask The mask of bits which need to be set, 0 to get the current value.
148 @result The options before the set operation, NB setOptions(?,0) returns the current value of this collection.
149 */
150 OSMetaClassDeclareReservedUsed(OSCollection, 0);
151 virtual unsigned setOptions(unsigned options, unsigned mask, void * = 0);
152
153 /*!
154 @function copyCollection
155 @abstract Do a deep copy of a collection tree.
156 @discussion This function copies this collection and all of the contained collections recursively. Objects that don't derive from OSContainter are NOT copied, that is objects like OSString and OSData. To a derive from OSConnection::copyCollection some code is required to be implemented in the derived class, below is the skeleton pseudo code to copy a collection.
157
158 OSCollection * <MyCollection>::copyCollection(OSDictionary *inCycleDict)
159 {
160 bool allocDict = !cycleDict;
161 OSCollection *ret = 0;
162 <MyCollection> *newMyColl = 0;
163
164 if (allocDict)
165 cycleDict = OSDictionary::withCapacity(16);
166 if (!cycleDict)
167 return 0;
168
169 do {
170 // Check to see if we already have a copy of the new dictionary
171 ret = super::copyCollection(cycleDict);
172 if (ret)
173 continue;
174
175 // Your code goes here to copy your collection,
176 // see OSArray & OSDictionary for examples.
177 newMyColl = <MyCollection>::with<MyCollection>(this);
178 if (!newMyColl)
179 continue;
180
181 // Insert object into cycle Dictionary
182 cycleDict->setObject((const OSSymbol *) this, newMyColl);
183
184 // Duplicate any collections in us
185 for (unsigned int i = 0; i < count; i++) {
186 OSObject *obj = getObject(i);
187 OSCollection *coll = OSDynamicCast(OSCollection, obj);
188
189 if (coll) {
190 OSCollection *newColl = coll->copyCollection(cycleDict);
191 if (!newColl)
192 goto abortCopy;
193
194 newMyColl->replaceObject(i, newColl);
195 newColl->release();
196 };
197 };
198
199 ret = newMyColl;
200 newMyColl = 0;
201
202 } while (false);
203
204 abortCopy:
205 if (newMyColl)
206 newMyColl->release();
207
208 if (allocDict)
209 cycleDict->release();
210
211 return ret;
212 }
213
214 @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.
215 @result The newly copied collecton or 0 if insufficient memory
216 */
217 virtual OSCollection *copyCollection(OSDictionary *cycleDict = 0);
218 OSMetaClassDeclareReservedUsed(OSCollection, 1);
219
220 OSMetaClassDeclareReservedUnused(OSCollection, 2);
221 OSMetaClassDeclareReservedUnused(OSCollection, 3);
222 OSMetaClassDeclareReservedUnused(OSCollection, 4);
223 OSMetaClassDeclareReservedUnused(OSCollection, 5);
224 OSMetaClassDeclareReservedUnused(OSCollection, 6);
225 OSMetaClassDeclareReservedUnused(OSCollection, 7);
226 };
227
228 #endif /* !_OS_OSCOLLECTION_H */