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