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