]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSArray.h
6794471c99dab73fdfed27aad86cd6d8358a0669
[apple/xnu.git] / libkern / libkern / c++ / OSArray.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 /* IOArray.h created by rsulack on Thu 11-Sep-1997 */
24 /* IOArray.h converted to C++ by gvdl on Fri 1998-10-30 */
25
26 #ifndef _OS_OSARRAY_H
27 #define _OS_OSARRAY_H
28
29 #include <libkern/c++/OSCollection.h>
30
31 class OSSerialize;
32
33 /*!
34 @class OSArray
35 @abstract A collection class whose instances maintain a list of object references.
36 @discussion
37 An instance of an OSArray is a mutable collection which maintains a list of references to OSMetaClassBase derived objects. Objects are referenced by index, where the index is an integer with a value of 0 to N-1 where N is the number of objects contained within the array.
38
39 Objects placed into an array are automatically retained and objects removed or replaced are automatically released. All objects are released when the array is freed.
40 */
41
42 class OSArray : public OSCollection
43 {
44 friend class OSSet;
45
46 OSDeclareDefaultStructors(OSArray)
47
48 protected:
49 const OSMetaClassBase **array;
50 unsigned int count;
51 unsigned int capacity;
52 unsigned int capacityIncrement;
53
54 struct ExpansionData { };
55
56 /*! @var reserved
57 Reserved for future use. (Internal use only) */
58 ExpansionData *reserved;
59
60 /*
61 * OSCollectionIterator interfaces.
62 */
63 virtual unsigned int iteratorSize() const;
64 virtual bool initIterator(void *iterator) const;
65 virtual bool getNextObjectForIterator(void *iterator, OSObject **ret) const;
66
67 public:
68 /*!
69 @function withCapacity
70 @abstract A static constructor function to create and initialize a new instance of OSArray with a given capacity.
71 @param capacity The initial capacity (number of refernces) of the OSArray instance.
72 @result Returns a reference to an instance of OSArray or 0 if an error occurred.
73 */
74 static OSArray *withCapacity(unsigned int capacity);
75 /*!
76 @function withObjects
77 @abstract A static constructor function to create and initialize a new instance of OSArray and populates it with a list of objects provided.
78 @param objects A static array of references to OSMetaClassBase derived objects.
79 @param count The number of objects provided.
80 @param capacity The initial storage size of the OSArray instance. If 0, the capacity will be set to the size of count, else the capacity must be greater than or equal to count.
81 @result Returns a reference to a new instance of OSArray or 0 if an error occurred.
82 */
83 static OSArray *withObjects(const OSObject *objects[],
84 unsigned int count,
85 unsigned int capacity = 0);
86 /*!
87 @function withArray
88 @abstract A static constructor function to create and initialize an instance of OSArray of a given capacity and populate it with the contents of the supplied OSArray object.
89 @param array An instance of OSArray from which the new instance will aquire its contents.
90 @param capacity The capacity of the new OSArray. If 0, the capacity will be set to the number of elements in the array, else the capacity must be greater than or equal to the number of elements in the array.
91 @result Returns a reference to an new instance of OSArray or 0 if an error occurred.
92 */
93 static OSArray *withArray(const OSArray *array,
94 unsigned int capacity = 0);
95
96 /*!
97 @function initWithCapacity
98 @abstract A member function which initializes an instance of OSArray.
99 @param capacity The initial capacity of the new instance of OSArray.
100 @result Returns a true if initialization succeeded or false if not.
101 */
102 virtual bool initWithCapacity(unsigned int capacity);
103 /*!
104 @function initWithObjects
105 @abstract A member function which initializes an instance of OSArray and populates it with the given list of objects.
106 @param objects A static array containing references to OSMetaClassBase derived objects.
107 @param count The number of objects to added to the array.
108 @param capacity The initial capacity of the new instance of OSArray. If 0, the capacity will be set to the same value as the 'count' parameter, else capacity must be greater than or equal to the value of 'count'.
109 @result Returns a true if initialization succeeded or false if not.
110 */
111 virtual bool initWithObjects(const OSObject *objects[],
112 unsigned int count,
113 unsigned int capacity = 0);
114 /*!
115 @function initWithArray
116 @abstract A member function which initializes an instance of OSArray and populates it with the contents of the supplied OSArray object.
117 @param anArray An instance of OSArray containing the references to objects which will be copied to the new instances of OSArray.
118 @param capacity The initial capacity of the new instance of OSArray. If 0, the capacity will be set to the number of elements in the array, else the capacity must be greater than or equal to the number of elements in the array.
119 @result Returns a true if initialization succeeded or false if not.
120 */
121 virtual bool initWithArray(const OSArray *anArray,
122 unsigned int theCapacity = 0);
123 /*!
124 @function free
125 @abstract Deallocates and releases all resources used by the OSArray instance. Normally, this is not called directly.
126 @discussion This function should not be called directly, use release() instead.
127 */
128 virtual void free();
129
130 /*!
131 @function getCount
132 @abstract A member function which returns the number of references contained within the OSArray object.
133 @result Returns the number of items within the OSArray object.
134 */
135 virtual unsigned int getCount() const;
136 /*!
137 @function getCapacity
138 @abstract A member function which returns the storage capacity of the OSArray object.
139 @result Returns the storage capacity of the OSArray object.
140 */
141 virtual unsigned int getCapacity() const;
142 /*!
143 @function getCapacityIncrement
144 @abstract A member function which returns the size by which the array will grow.
145 @result Returns the size by which the array will grow.
146 */
147 virtual unsigned int getCapacityIncrement() const;
148 /*!
149 @function setCapacityIncrement
150 @abstract A member function which sets the growth size of the array.
151 @result Returns the new growth size.
152 */
153 virtual unsigned int setCapacityIncrement(unsigned increment);
154
155 /*!
156 @function ensureCapacity
157 @abstract A member function which will expand the size of the collection to a given storage capacity.
158 @param newCapacity The new capacity for the array.
159 @result Returns the new capacity of the array or the previous capacity upon error.
160 */
161 virtual unsigned int ensureCapacity(unsigned int newCapacity);
162
163 /*!
164 @function flushCollection
165 @abstract A member function which removes and releases all items within the array.
166 */
167 virtual void flushCollection();
168
169 /*!
170 @function setObject
171 @abstract A member function which appends an object onto the end of the array.
172 @param anObject The object to add to the OSArray instance. The object will be retained automatically.
173 @result Returns true if the addition of 'anObject' was successful, false if not; failure usually results from failing to allocate the necessary memory.
174 */
175 virtual bool setObject(const OSMetaClassBase *anObject);
176 /*!
177 @function setObject
178 @abstract A member function which inserts an object into the array at a particular index.
179 @param index The index into the array to insert the object.
180 @param anObject The object to add to the OSArray instance. The object will be retained automatically.
181 @result Returns true if the addition of 'anObject' was successful, false if not.
182 */
183 virtual bool setObject(unsigned int index, const OSMetaClassBase *anObject);
184
185 /*!
186 @function merge
187 @abstract A member function which appends the contents of an array onto the receiving array.
188 @param otherArray The array whose contents will be appended to the reveiving array.
189 @result Returns true when merging was successful, false otherwise.
190 */
191 virtual bool merge(const OSArray *otherArray);
192
193 /*!
194 @function replaceObject
195 @abstract A member function which will replace an object in an array at a given index. The original object will be released and the new object will be retained.
196 @param index The index into the array at which the new object will be placed.
197 @param anObject The object to be placed into the array.
198 */
199 virtual void replaceObject(unsigned int index, const OSMetaClassBase *anObject);
200 /*!
201 @function removeObject
202 @abstract A member function which removes an object from the array.
203 @param index The index of the object to be removed.
204 @discussion This function removes an object from the array which is located at a given index. Once removed the contents of the array will shift to fill in the vacated spot. The removed object is automatically released.
205 */
206 virtual void removeObject(unsigned int index);
207
208 /*!
209 @function isEqualTo
210 @abstract A member function which tests the equality of the values of two OSArray objects.
211 @param anArray The array object being compared against the receiver.
212 @result Returns true if the two arrays are equivalent or false otherwise.
213 */
214 virtual bool isEqualTo(const OSArray *anArray) const;
215 /*!
216 @function isEqualTo
217 @abstract A member function which compares the equality of the values of a receiving array to an arbitrary object.
218 @param anObject The object to be compared against the receiver.
219 @result Returns true if the two objects are equivalent, that is they are either the same object or they are both arrays containing the same or equivalent objects, or false otherwise.
220 */
221 virtual bool isEqualTo(const OSMetaClassBase *anObject) const;
222
223 /*!
224 @function getObject
225 @abstract A member function which returns a reference to an object located within the array at a given index. The caller should not release the returned object.
226 @param index The index into the array from which the reference to an object is taken.
227 @result Returns a reference to an object or 0 if the index is beyond the bounds of the array.
228 */
229 virtual OSObject *getObject(unsigned int index) const;
230 /*!
231 @function getLastObject
232 @abstract A member function which returns a reference to the last object in the array. The caller should not release the returned object.
233 @result Returns a reference to the last object in the array or 0 if the array is empty.
234 */
235 virtual OSObject *getLastObject() const;
236
237 /*!
238 @function getNextIndexOfObject
239 @abstract A member function which searches the array for the next instance of a specific object, at or beyond the supplied index.
240 @result Returns the next index of the object in the array or (-1) if none is found.
241 */
242 virtual unsigned int getNextIndexOfObject(const OSMetaClassBase * anObject,
243 unsigned int index) const;
244
245 /*!
246 @function serialize
247 @abstract A member function which archives the receiver.
248 @param s The OSSerialize object.
249 @result Returns true if serialization was successful, false if not.
250 */
251 virtual bool serialize(OSSerialize *s) const;
252
253 /*!
254 @function setOptions
255 @abstract This function is used to recursively set option bits in this array and all child collections.
256 @param options Set the (options & mask) bits.
257 @param mask The mask of bits which need to be set, 0 to get the current value.
258 @result The options before the set operation, NB setOptions(?,0) returns the current value of this collection.
259 */
260 virtual unsigned setOptions(unsigned options, unsigned mask, void * = 0);
261
262 /*!
263 @function copyCollection
264 @abstract Do a deep copy of this array and its collections.
265 @discussion This function copies this array included collections recursively. Objects that don't derive from OSContainter are NOT copied, that is objects like OSString and OSData.
266 @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.
267 @result The newly copied collecton or 0 if insufficient memory
268 */
269 OSCollection *copyCollection(OSDictionary *cycleDict = 0);
270
271 OSMetaClassDeclareReservedUnused(OSArray, 0);
272 OSMetaClassDeclareReservedUnused(OSArray, 1);
273 OSMetaClassDeclareReservedUnused(OSArray, 2);
274 OSMetaClassDeclareReservedUnused(OSArray, 3);
275 OSMetaClassDeclareReservedUnused(OSArray, 4);
276 OSMetaClassDeclareReservedUnused(OSArray, 5);
277 OSMetaClassDeclareReservedUnused(OSArray, 6);
278 OSMetaClassDeclareReservedUnused(OSArray, 7);
279 };
280
281 #endif /* !_OS_OSARRAY_H */