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