1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2000, 2014 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions /* IOSet.m created by rsulack on Thu 11-Jun-1998 */
29*c54f35caSApple OSS Distributions
30*c54f35caSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*c54f35caSApple OSS Distributions
32*c54f35caSApple OSS Distributions #include <libkern/c++/OSArray.h>
33*c54f35caSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
34*c54f35caSApple OSS Distributions #include <libkern/c++/OSSerialize.h>
35*c54f35caSApple OSS Distributions #include <libkern/c++/OSSet.h>
36*c54f35caSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
37*c54f35caSApple OSS Distributions #include <os/cpp_util.h>
38*c54f35caSApple OSS Distributions #include <kern/zalloc.h>
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributions #define super OSCollection
41*c54f35caSApple OSS Distributions
42*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSSet, OSCollection,
43*c54f35caSApple OSS Distributions ZC_ZFREE_CLEARMEM)
44*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 0);
45*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 1);
46*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 2);
47*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 3);
48*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 4);
49*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 5);
50*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 6);
51*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSet, 7);
52*c54f35caSApple OSS Distributions
53*c54f35caSApple OSS Distributions #define EXT_CAST(obj) \
54*c54f35caSApple OSS Distributions reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
55*c54f35caSApple OSS Distributions
56*c54f35caSApple OSS Distributions bool
initWithCapacity(unsigned int inCapacity)57*c54f35caSApple OSS Distributions OSSet::initWithCapacity(unsigned int inCapacity)
58*c54f35caSApple OSS Distributions {
59*c54f35caSApple OSS Distributions if (!super::init()) {
60*c54f35caSApple OSS Distributions return false;
61*c54f35caSApple OSS Distributions }
62*c54f35caSApple OSS Distributions
63*c54f35caSApple OSS Distributions members = OSArray::withCapacity(inCapacity);
64*c54f35caSApple OSS Distributions if (!members) {
65*c54f35caSApple OSS Distributions return false;
66*c54f35caSApple OSS Distributions }
67*c54f35caSApple OSS Distributions
68*c54f35caSApple OSS Distributions return true;
69*c54f35caSApple OSS Distributions }
70*c54f35caSApple OSS Distributions
71*c54f35caSApple OSS Distributions bool
initWithObjects(const OSObject * inObjects[],unsigned int inCount,unsigned int inCapacity)72*c54f35caSApple OSS Distributions OSSet::initWithObjects(const OSObject *inObjects[],
73*c54f35caSApple OSS Distributions unsigned int inCount,
74*c54f35caSApple OSS Distributions unsigned int inCapacity)
75*c54f35caSApple OSS Distributions {
76*c54f35caSApple OSS Distributions unsigned int capacity = inCount;
77*c54f35caSApple OSS Distributions
78*c54f35caSApple OSS Distributions if (inCapacity) {
79*c54f35caSApple OSS Distributions if (inCount > inCapacity) {
80*c54f35caSApple OSS Distributions return false;
81*c54f35caSApple OSS Distributions }
82*c54f35caSApple OSS Distributions
83*c54f35caSApple OSS Distributions capacity = inCapacity;
84*c54f35caSApple OSS Distributions }
85*c54f35caSApple OSS Distributions
86*c54f35caSApple OSS Distributions if (!inObjects || !initWithCapacity(capacity)) {
87*c54f35caSApple OSS Distributions return false;
88*c54f35caSApple OSS Distributions }
89*c54f35caSApple OSS Distributions
90*c54f35caSApple OSS Distributions for (unsigned int i = 0; i < inCount; i++) {
91*c54f35caSApple OSS Distributions // xx-review: no test here for failure of setObject()
92*c54f35caSApple OSS Distributions if (members->getCount() < capacity) {
93*c54f35caSApple OSS Distributions setObject(inObjects[i]);
94*c54f35caSApple OSS Distributions } else {
95*c54f35caSApple OSS Distributions return false;
96*c54f35caSApple OSS Distributions }
97*c54f35caSApple OSS Distributions }
98*c54f35caSApple OSS Distributions
99*c54f35caSApple OSS Distributions return true;
100*c54f35caSApple OSS Distributions }
101*c54f35caSApple OSS Distributions
102*c54f35caSApple OSS Distributions bool
initWithArray(const OSArray * inArray,unsigned int inCapacity)103*c54f35caSApple OSS Distributions OSSet::initWithArray(const OSArray *inArray,
104*c54f35caSApple OSS Distributions unsigned int inCapacity)
105*c54f35caSApple OSS Distributions {
106*c54f35caSApple OSS Distributions if (!inArray) {
107*c54f35caSApple OSS Distributions return false;
108*c54f35caSApple OSS Distributions }
109*c54f35caSApple OSS Distributions
110*c54f35caSApple OSS Distributions return initWithObjects((const OSObject **) inArray->array,
111*c54f35caSApple OSS Distributions inArray->count, inCapacity);
112*c54f35caSApple OSS Distributions }
113*c54f35caSApple OSS Distributions
114*c54f35caSApple OSS Distributions bool
initWithSet(const OSSet * inSet,unsigned int inCapacity)115*c54f35caSApple OSS Distributions OSSet::initWithSet(const OSSet *inSet,
116*c54f35caSApple OSS Distributions unsigned int inCapacity)
117*c54f35caSApple OSS Distributions {
118*c54f35caSApple OSS Distributions return initWithArray(inSet->members.get(), inCapacity);
119*c54f35caSApple OSS Distributions }
120*c54f35caSApple OSS Distributions
121*c54f35caSApple OSS Distributions OSSharedPtr<OSSet>
withCapacity(unsigned int capacity)122*c54f35caSApple OSS Distributions OSSet::withCapacity(unsigned int capacity)
123*c54f35caSApple OSS Distributions {
124*c54f35caSApple OSS Distributions OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
125*c54f35caSApple OSS Distributions
126*c54f35caSApple OSS Distributions if (me && !me->initWithCapacity(capacity)) {
127*c54f35caSApple OSS Distributions return nullptr;
128*c54f35caSApple OSS Distributions }
129*c54f35caSApple OSS Distributions
130*c54f35caSApple OSS Distributions return me;
131*c54f35caSApple OSS Distributions }
132*c54f35caSApple OSS Distributions
133*c54f35caSApple OSS Distributions OSSharedPtr<OSSet>
withObjects(const OSObject * objects[],unsigned int count,unsigned int capacity)134*c54f35caSApple OSS Distributions OSSet::withObjects(const OSObject *objects[],
135*c54f35caSApple OSS Distributions unsigned int count,
136*c54f35caSApple OSS Distributions unsigned int capacity)
137*c54f35caSApple OSS Distributions {
138*c54f35caSApple OSS Distributions OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
139*c54f35caSApple OSS Distributions
140*c54f35caSApple OSS Distributions if (me && !me->initWithObjects(objects, count, capacity)) {
141*c54f35caSApple OSS Distributions return nullptr;
142*c54f35caSApple OSS Distributions }
143*c54f35caSApple OSS Distributions
144*c54f35caSApple OSS Distributions return me;
145*c54f35caSApple OSS Distributions }
146*c54f35caSApple OSS Distributions
147*c54f35caSApple OSS Distributions OSSharedPtr<OSSet>
withArray(const OSArray * array,unsigned int capacity)148*c54f35caSApple OSS Distributions OSSet::withArray(const OSArray *array,
149*c54f35caSApple OSS Distributions unsigned int capacity)
150*c54f35caSApple OSS Distributions {
151*c54f35caSApple OSS Distributions OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
152*c54f35caSApple OSS Distributions
153*c54f35caSApple OSS Distributions if (me && !me->initWithArray(array, capacity)) {
154*c54f35caSApple OSS Distributions return nullptr;
155*c54f35caSApple OSS Distributions }
156*c54f35caSApple OSS Distributions
157*c54f35caSApple OSS Distributions return me;
158*c54f35caSApple OSS Distributions }
159*c54f35caSApple OSS Distributions
160*c54f35caSApple OSS Distributions OSSharedPtr<OSSet>
withSet(const OSSet * set,unsigned int capacity)161*c54f35caSApple OSS Distributions OSSet::withSet(const OSSet *set,
162*c54f35caSApple OSS Distributions unsigned int capacity)
163*c54f35caSApple OSS Distributions {
164*c54f35caSApple OSS Distributions OSSharedPtr<OSSet> me = OSMakeShared<OSSet>();
165*c54f35caSApple OSS Distributions
166*c54f35caSApple OSS Distributions if (me && !me->initWithSet(set, capacity)) {
167*c54f35caSApple OSS Distributions return nullptr;
168*c54f35caSApple OSS Distributions }
169*c54f35caSApple OSS Distributions
170*c54f35caSApple OSS Distributions return me;
171*c54f35caSApple OSS Distributions }
172*c54f35caSApple OSS Distributions
173*c54f35caSApple OSS Distributions void
free()174*c54f35caSApple OSS Distributions OSSet::free()
175*c54f35caSApple OSS Distributions {
176*c54f35caSApple OSS Distributions if (members) {
177*c54f35caSApple OSS Distributions (void) members->super::setOptions(0, kImmutable);
178*c54f35caSApple OSS Distributions }
179*c54f35caSApple OSS Distributions
180*c54f35caSApple OSS Distributions super::free();
181*c54f35caSApple OSS Distributions }
182*c54f35caSApple OSS Distributions
183*c54f35caSApple OSS Distributions unsigned int
getCount() const184*c54f35caSApple OSS Distributions OSSet::getCount() const
185*c54f35caSApple OSS Distributions {
186*c54f35caSApple OSS Distributions return members->count;
187*c54f35caSApple OSS Distributions }
188*c54f35caSApple OSS Distributions
189*c54f35caSApple OSS Distributions unsigned int
getCapacity() const190*c54f35caSApple OSS Distributions OSSet::getCapacity() const
191*c54f35caSApple OSS Distributions {
192*c54f35caSApple OSS Distributions return members->capacity;
193*c54f35caSApple OSS Distributions }
194*c54f35caSApple OSS Distributions
195*c54f35caSApple OSS Distributions unsigned int
getCapacityIncrement() const196*c54f35caSApple OSS Distributions OSSet::getCapacityIncrement() const
197*c54f35caSApple OSS Distributions {
198*c54f35caSApple OSS Distributions return members->capacityIncrement;
199*c54f35caSApple OSS Distributions }
200*c54f35caSApple OSS Distributions
201*c54f35caSApple OSS Distributions unsigned int
setCapacityIncrement(unsigned int increment)202*c54f35caSApple OSS Distributions OSSet::setCapacityIncrement(unsigned int increment)
203*c54f35caSApple OSS Distributions {
204*c54f35caSApple OSS Distributions return members->setCapacityIncrement(increment);
205*c54f35caSApple OSS Distributions }
206*c54f35caSApple OSS Distributions
207*c54f35caSApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)208*c54f35caSApple OSS Distributions OSSet::ensureCapacity(unsigned int newCapacity)
209*c54f35caSApple OSS Distributions {
210*c54f35caSApple OSS Distributions return members->ensureCapacity(newCapacity);
211*c54f35caSApple OSS Distributions }
212*c54f35caSApple OSS Distributions
213*c54f35caSApple OSS Distributions void
flushCollection()214*c54f35caSApple OSS Distributions OSSet::flushCollection()
215*c54f35caSApple OSS Distributions {
216*c54f35caSApple OSS Distributions haveUpdated();
217*c54f35caSApple OSS Distributions members->flushCollection();
218*c54f35caSApple OSS Distributions }
219*c54f35caSApple OSS Distributions
220*c54f35caSApple OSS Distributions bool
setObject(const OSMetaClassBase * anObject)221*c54f35caSApple OSS Distributions OSSet::setObject(const OSMetaClassBase *anObject)
222*c54f35caSApple OSS Distributions {
223*c54f35caSApple OSS Distributions if (containsObject(anObject)) {
224*c54f35caSApple OSS Distributions return false;
225*c54f35caSApple OSS Distributions } else {
226*c54f35caSApple OSS Distributions haveUpdated();
227*c54f35caSApple OSS Distributions return members->setObject(anObject);
228*c54f35caSApple OSS Distributions }
229*c54f35caSApple OSS Distributions }
230*c54f35caSApple OSS Distributions
231*c54f35caSApple OSS Distributions bool
setObject(OSSharedPtr<const OSMetaClassBase> const & anObject)232*c54f35caSApple OSS Distributions OSSet::setObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
233*c54f35caSApple OSS Distributions {
234*c54f35caSApple OSS Distributions return setObject(anObject.get());
235*c54f35caSApple OSS Distributions }
236*c54f35caSApple OSS Distributions
237*c54f35caSApple OSS Distributions bool
merge(const OSArray * array)238*c54f35caSApple OSS Distributions OSSet::merge(const OSArray * array)
239*c54f35caSApple OSS Distributions {
240*c54f35caSApple OSS Distributions const OSMetaClassBase * anObject = NULL;
241*c54f35caSApple OSS Distributions bool result = true;
242*c54f35caSApple OSS Distributions
243*c54f35caSApple OSS Distributions for (int i = 0; (anObject = array->getObject(i)); i++) {
244*c54f35caSApple OSS Distributions /* setObject() returns false if the object is already in the set,
245*c54f35caSApple OSS Distributions * so we have to check beforehand here with containsObject().
246*c54f35caSApple OSS Distributions */
247*c54f35caSApple OSS Distributions if (containsObject(anObject)) {
248*c54f35caSApple OSS Distributions continue;
249*c54f35caSApple OSS Distributions }
250*c54f35caSApple OSS Distributions if (!setObject(anObject)) {
251*c54f35caSApple OSS Distributions result = false;
252*c54f35caSApple OSS Distributions }
253*c54f35caSApple OSS Distributions }
254*c54f35caSApple OSS Distributions
255*c54f35caSApple OSS Distributions return result;
256*c54f35caSApple OSS Distributions }
257*c54f35caSApple OSS Distributions
258*c54f35caSApple OSS Distributions bool
merge(const OSSet * set)259*c54f35caSApple OSS Distributions OSSet::merge(const OSSet * set)
260*c54f35caSApple OSS Distributions {
261*c54f35caSApple OSS Distributions return merge(set->members.get());
262*c54f35caSApple OSS Distributions }
263*c54f35caSApple OSS Distributions
264*c54f35caSApple OSS Distributions void
removeObject(const OSMetaClassBase * anObject)265*c54f35caSApple OSS Distributions OSSet::removeObject(const OSMetaClassBase *anObject)
266*c54f35caSApple OSS Distributions {
267*c54f35caSApple OSS Distributions const OSMetaClassBase *probeObject;
268*c54f35caSApple OSS Distributions
269*c54f35caSApple OSS Distributions for (int i = 0; (probeObject = members->getObject(i)); i++) {
270*c54f35caSApple OSS Distributions if (probeObject == anObject) {
271*c54f35caSApple OSS Distributions haveUpdated();
272*c54f35caSApple OSS Distributions members->removeObject(i);
273*c54f35caSApple OSS Distributions return;
274*c54f35caSApple OSS Distributions }
275*c54f35caSApple OSS Distributions }
276*c54f35caSApple OSS Distributions }
277*c54f35caSApple OSS Distributions
278*c54f35caSApple OSS Distributions void
removeObject(OSSharedPtr<const OSMetaClassBase> const & anObject)279*c54f35caSApple OSS Distributions OSSet::removeObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
280*c54f35caSApple OSS Distributions {
281*c54f35caSApple OSS Distributions removeObject(anObject.get());
282*c54f35caSApple OSS Distributions }
283*c54f35caSApple OSS Distributions
284*c54f35caSApple OSS Distributions
285*c54f35caSApple OSS Distributions bool
containsObject(const OSMetaClassBase * anObject) const286*c54f35caSApple OSS Distributions OSSet::containsObject(const OSMetaClassBase *anObject) const
287*c54f35caSApple OSS Distributions {
288*c54f35caSApple OSS Distributions return anObject && member(anObject);
289*c54f35caSApple OSS Distributions }
290*c54f35caSApple OSS Distributions
291*c54f35caSApple OSS Distributions bool
member(const OSMetaClassBase * anObject) const292*c54f35caSApple OSS Distributions OSSet::member(const OSMetaClassBase *anObject) const
293*c54f35caSApple OSS Distributions {
294*c54f35caSApple OSS Distributions OSMetaClassBase *probeObject;
295*c54f35caSApple OSS Distributions
296*c54f35caSApple OSS Distributions for (int i = 0; (probeObject = members->getObject(i)); i++) {
297*c54f35caSApple OSS Distributions if (probeObject == anObject) {
298*c54f35caSApple OSS Distributions return true;
299*c54f35caSApple OSS Distributions }
300*c54f35caSApple OSS Distributions }
301*c54f35caSApple OSS Distributions
302*c54f35caSApple OSS Distributions return false;
303*c54f35caSApple OSS Distributions }
304*c54f35caSApple OSS Distributions
305*c54f35caSApple OSS Distributions OSObject *
getAnyObject() const306*c54f35caSApple OSS Distributions OSSet::getAnyObject() const
307*c54f35caSApple OSS Distributions {
308*c54f35caSApple OSS Distributions return members->getObject(0);
309*c54f35caSApple OSS Distributions }
310*c54f35caSApple OSS Distributions
311*c54f35caSApple OSS Distributions bool
isEqualTo(const OSSet * aSet) const312*c54f35caSApple OSS Distributions OSSet::isEqualTo(const OSSet *aSet) const
313*c54f35caSApple OSS Distributions {
314*c54f35caSApple OSS Distributions unsigned int count;
315*c54f35caSApple OSS Distributions unsigned int i;
316*c54f35caSApple OSS Distributions const OSMetaClassBase *obj1;
317*c54f35caSApple OSS Distributions const OSMetaClassBase *obj2;
318*c54f35caSApple OSS Distributions
319*c54f35caSApple OSS Distributions if (this == aSet) {
320*c54f35caSApple OSS Distributions return true;
321*c54f35caSApple OSS Distributions }
322*c54f35caSApple OSS Distributions
323*c54f35caSApple OSS Distributions count = members->count;
324*c54f35caSApple OSS Distributions if (count != aSet->getCount()) {
325*c54f35caSApple OSS Distributions return false;
326*c54f35caSApple OSS Distributions }
327*c54f35caSApple OSS Distributions
328*c54f35caSApple OSS Distributions for (i = 0; i < count; i++) {
329*c54f35caSApple OSS Distributions obj1 = aSet->members->getObject(i);
330*c54f35caSApple OSS Distributions if (containsObject(obj1)) {
331*c54f35caSApple OSS Distributions continue;
332*c54f35caSApple OSS Distributions }
333*c54f35caSApple OSS Distributions obj2 = members->getObject(i);
334*c54f35caSApple OSS Distributions if (!obj1 || !obj2) {
335*c54f35caSApple OSS Distributions return false;
336*c54f35caSApple OSS Distributions }
337*c54f35caSApple OSS Distributions
338*c54f35caSApple OSS Distributions if (!obj1->isEqualTo(obj2)) {
339*c54f35caSApple OSS Distributions return false;
340*c54f35caSApple OSS Distributions }
341*c54f35caSApple OSS Distributions }
342*c54f35caSApple OSS Distributions
343*c54f35caSApple OSS Distributions return true;
344*c54f35caSApple OSS Distributions }
345*c54f35caSApple OSS Distributions
346*c54f35caSApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * anObject) const347*c54f35caSApple OSS Distributions OSSet::isEqualTo(const OSMetaClassBase *anObject) const
348*c54f35caSApple OSS Distributions {
349*c54f35caSApple OSS Distributions OSSet *otherSet;
350*c54f35caSApple OSS Distributions
351*c54f35caSApple OSS Distributions otherSet = OSDynamicCast(OSSet, anObject);
352*c54f35caSApple OSS Distributions if (otherSet) {
353*c54f35caSApple OSS Distributions return isEqualTo(otherSet);
354*c54f35caSApple OSS Distributions } else {
355*c54f35caSApple OSS Distributions return false;
356*c54f35caSApple OSS Distributions }
357*c54f35caSApple OSS Distributions }
358*c54f35caSApple OSS Distributions
359*c54f35caSApple OSS Distributions unsigned int
iteratorSize() const360*c54f35caSApple OSS Distributions OSSet::iteratorSize() const
361*c54f35caSApple OSS Distributions {
362*c54f35caSApple OSS Distributions return sizeof(unsigned int);
363*c54f35caSApple OSS Distributions }
364*c54f35caSApple OSS Distributions
365*c54f35caSApple OSS Distributions bool
initIterator(void * inIterator) const366*c54f35caSApple OSS Distributions OSSet::initIterator(void *inIterator) const
367*c54f35caSApple OSS Distributions {
368*c54f35caSApple OSS Distributions unsigned int *iteratorP = (unsigned int *) inIterator;
369*c54f35caSApple OSS Distributions
370*c54f35caSApple OSS Distributions *iteratorP = 0;
371*c54f35caSApple OSS Distributions return true;
372*c54f35caSApple OSS Distributions }
373*c54f35caSApple OSS Distributions
374*c54f35caSApple OSS Distributions bool
getNextObjectForIterator(void * inIterator,OSObject ** ret) const375*c54f35caSApple OSS Distributions OSSet::getNextObjectForIterator(void *inIterator, OSObject **ret) const
376*c54f35caSApple OSS Distributions {
377*c54f35caSApple OSS Distributions unsigned int *iteratorP = (unsigned int *) inIterator;
378*c54f35caSApple OSS Distributions unsigned int index = (*iteratorP)++;
379*c54f35caSApple OSS Distributions
380*c54f35caSApple OSS Distributions if (index < members->count) {
381*c54f35caSApple OSS Distributions *ret = members->getObject(index);
382*c54f35caSApple OSS Distributions } else {
383*c54f35caSApple OSS Distributions *ret = NULL;
384*c54f35caSApple OSS Distributions }
385*c54f35caSApple OSS Distributions
386*c54f35caSApple OSS Distributions return *ret != NULL;
387*c54f35caSApple OSS Distributions }
388*c54f35caSApple OSS Distributions
389*c54f35caSApple OSS Distributions bool
serialize(OSSerialize * s) const390*c54f35caSApple OSS Distributions OSSet::serialize(OSSerialize *s) const
391*c54f35caSApple OSS Distributions {
392*c54f35caSApple OSS Distributions const OSMetaClassBase *o;
393*c54f35caSApple OSS Distributions
394*c54f35caSApple OSS Distributions if (s->previouslySerialized(this)) {
395*c54f35caSApple OSS Distributions return true;
396*c54f35caSApple OSS Distributions }
397*c54f35caSApple OSS Distributions
398*c54f35caSApple OSS Distributions if (!s->addXMLStartTag(this, "set")) {
399*c54f35caSApple OSS Distributions return false;
400*c54f35caSApple OSS Distributions }
401*c54f35caSApple OSS Distributions
402*c54f35caSApple OSS Distributions for (int i = 0; (o = members->getObject(i)); i++) {
403*c54f35caSApple OSS Distributions if (!o->serialize(s)) {
404*c54f35caSApple OSS Distributions return false;
405*c54f35caSApple OSS Distributions }
406*c54f35caSApple OSS Distributions }
407*c54f35caSApple OSS Distributions
408*c54f35caSApple OSS Distributions return s->addXMLEndTag("set");
409*c54f35caSApple OSS Distributions }
410*c54f35caSApple OSS Distributions
411*c54f35caSApple OSS Distributions unsigned
setOptions(unsigned options,unsigned mask,void *)412*c54f35caSApple OSS Distributions OSSet::setOptions(unsigned options, unsigned mask, void *)
413*c54f35caSApple OSS Distributions {
414*c54f35caSApple OSS Distributions unsigned old = super::setOptions(options, mask);
415*c54f35caSApple OSS Distributions if ((old ^ options) & mask) {
416*c54f35caSApple OSS Distributions members->setOptions(options, mask);
417*c54f35caSApple OSS Distributions }
418*c54f35caSApple OSS Distributions
419*c54f35caSApple OSS Distributions return old;
420*c54f35caSApple OSS Distributions }
421*c54f35caSApple OSS Distributions
422*c54f35caSApple OSS Distributions OSSharedPtr<OSCollection>
copyCollection(OSDictionary * cycleDict)423*c54f35caSApple OSS Distributions OSSet::copyCollection(OSDictionary *cycleDict)
424*c54f35caSApple OSS Distributions {
425*c54f35caSApple OSS Distributions OSSharedPtr<OSDictionary> ourCycleDict;
426*c54f35caSApple OSS Distributions OSSharedPtr<OSCollection> ret;
427*c54f35caSApple OSS Distributions OSSharedPtr<OSSet> newSet;
428*c54f35caSApple OSS Distributions
429*c54f35caSApple OSS Distributions if (!cycleDict) {
430*c54f35caSApple OSS Distributions ourCycleDict = OSDictionary::withCapacity(16);
431*c54f35caSApple OSS Distributions if (!ourCycleDict) {
432*c54f35caSApple OSS Distributions return nullptr;
433*c54f35caSApple OSS Distributions }
434*c54f35caSApple OSS Distributions cycleDict = ourCycleDict.get();
435*c54f35caSApple OSS Distributions }
436*c54f35caSApple OSS Distributions
437*c54f35caSApple OSS Distributions do {
438*c54f35caSApple OSS Distributions // Check for a cycle
439*c54f35caSApple OSS Distributions ret = super::copyCollection(cycleDict);
440*c54f35caSApple OSS Distributions if (ret) {
441*c54f35caSApple OSS Distributions continue; // Found it
442*c54f35caSApple OSS Distributions }
443*c54f35caSApple OSS Distributions newSet = OSSet::withCapacity(members->capacity);
444*c54f35caSApple OSS Distributions if (!newSet) {
445*c54f35caSApple OSS Distributions continue; // Couldn't create new set abort
446*c54f35caSApple OSS Distributions }
447*c54f35caSApple OSS Distributions // Insert object into cycle Dictionary
448*c54f35caSApple OSS Distributions cycleDict->setObject((const OSSymbol *) this, newSet.get());
449*c54f35caSApple OSS Distributions
450*c54f35caSApple OSS Distributions OSArray *newMembers = newSet->members.get();
451*c54f35caSApple OSS Distributions newMembers->capacityIncrement = members->capacityIncrement;
452*c54f35caSApple OSS Distributions
453*c54f35caSApple OSS Distributions // Now copy over the contents into the new duplicate
454*c54f35caSApple OSS Distributions for (unsigned int i = 0; i < members->count; i++) {
455*c54f35caSApple OSS Distributions OSObject *obj = EXT_CAST(members->array[i].get());
456*c54f35caSApple OSS Distributions OSCollection *coll = OSDynamicCast(OSCollection, obj);
457*c54f35caSApple OSS Distributions if (coll) {
458*c54f35caSApple OSS Distributions OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
459*c54f35caSApple OSS Distributions if (newColl) {
460*c54f35caSApple OSS Distributions obj = newColl.get(); // Rely on cycleDict ref for a bit
461*c54f35caSApple OSS Distributions } else {
462*c54f35caSApple OSS Distributions return ret;
463*c54f35caSApple OSS Distributions }
464*c54f35caSApple OSS Distributions }
465*c54f35caSApple OSS Distributions newMembers->setObject(obj);
466*c54f35caSApple OSS Distributions }
467*c54f35caSApple OSS Distributions
468*c54f35caSApple OSS Distributions ret = os::move(newSet);
469*c54f35caSApple OSS Distributions } while (false);
470*c54f35caSApple OSS Distributions
471*c54f35caSApple OSS Distributions return ret;
472*c54f35caSApple OSS Distributions }
473