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