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