1*0f4c859eSApple OSS Distributions /*
2*0f4c859eSApple OSS Distributions * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3*0f4c859eSApple OSS Distributions *
4*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*0f4c859eSApple OSS Distributions *
6*0f4c859eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*0f4c859eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*0f4c859eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*0f4c859eSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*0f4c859eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*0f4c859eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*0f4c859eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*0f4c859eSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*0f4c859eSApple OSS Distributions *
15*0f4c859eSApple OSS Distributions * Please obtain a copy of the License at
16*0f4c859eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*0f4c859eSApple OSS Distributions *
18*0f4c859eSApple OSS Distributions * The Original Code and all software distributed under the License are
19*0f4c859eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*0f4c859eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*0f4c859eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*0f4c859eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*0f4c859eSApple OSS Distributions * Please see the License for the specific language governing rights and
24*0f4c859eSApple OSS Distributions * limitations under the License.
25*0f4c859eSApple OSS Distributions *
26*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*0f4c859eSApple OSS Distributions */
28*0f4c859eSApple OSS Distributions /* OSBoolean.cpp created by rsulack on Tue Oct 12 1999 */
29*0f4c859eSApple OSS Distributions
30*0f4c859eSApple OSS Distributions #include <libkern/c++/OSBoolean.h>
31*0f4c859eSApple OSS Distributions #include <libkern/c++/OSString.h>
32*0f4c859eSApple OSS Distributions #include <libkern/c++/OSSerialize.h>
33*0f4c859eSApple OSS Distributions #include <libkern/c++/OSLib.h>
34*0f4c859eSApple OSS Distributions
35*0f4c859eSApple OSS Distributions #define super OSObject
36*0f4c859eSApple OSS Distributions
37*0f4c859eSApple OSS Distributions OSDefineMetaClassAndStructorsWithInit(OSBoolean, OSObject, OSBoolean::initialize())
38*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 0);
39*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 1);
40*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 2);
41*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 3);
42*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 4);
43*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 5);
44*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 6);
45*0f4c859eSApple OSS Distributions OSMetaClassDefineReservedUnused(OSBoolean, 7);
46*0f4c859eSApple OSS Distributions
47*0f4c859eSApple OSS Distributions static OSBoolean * gOSBooleanTrue = NULL;
48*0f4c859eSApple OSS Distributions static OSBoolean * gOSBooleanFalse = NULL;
49*0f4c859eSApple OSS Distributions
50*0f4c859eSApple OSS Distributions OSBoolean * const & kOSBooleanTrue = gOSBooleanTrue;
51*0f4c859eSApple OSS Distributions OSBoolean * const & kOSBooleanFalse = gOSBooleanFalse;
52*0f4c859eSApple OSS Distributions
53*0f4c859eSApple OSS Distributions void
initialize()54*0f4c859eSApple OSS Distributions OSBoolean::initialize()
55*0f4c859eSApple OSS Distributions {
56*0f4c859eSApple OSS Distributions gOSBooleanTrue = new OSBoolean;
57*0f4c859eSApple OSS Distributions assert(gOSBooleanTrue);
58*0f4c859eSApple OSS Distributions
59*0f4c859eSApple OSS Distributions if (!gOSBooleanTrue->init()) {
60*0f4c859eSApple OSS Distributions gOSBooleanTrue->OSObject::free();
61*0f4c859eSApple OSS Distributions assert(false);
62*0f4c859eSApple OSS Distributions }
63*0f4c859eSApple OSS Distributions ;
64*0f4c859eSApple OSS Distributions gOSBooleanTrue->value = true;
65*0f4c859eSApple OSS Distributions
66*0f4c859eSApple OSS Distributions gOSBooleanFalse = new OSBoolean;
67*0f4c859eSApple OSS Distributions assert(gOSBooleanFalse);
68*0f4c859eSApple OSS Distributions
69*0f4c859eSApple OSS Distributions if (!gOSBooleanFalse->init()) {
70*0f4c859eSApple OSS Distributions gOSBooleanFalse->OSObject::free();
71*0f4c859eSApple OSS Distributions assert(false);
72*0f4c859eSApple OSS Distributions }
73*0f4c859eSApple OSS Distributions ;
74*0f4c859eSApple OSS Distributions gOSBooleanFalse->value = false;
75*0f4c859eSApple OSS Distributions }
76*0f4c859eSApple OSS Distributions
77*0f4c859eSApple OSS Distributions void
free()78*0f4c859eSApple OSS Distributions OSBoolean::free()
79*0f4c859eSApple OSS Distributions {
80*0f4c859eSApple OSS Distributions /*
81*0f4c859eSApple OSS Distributions * An OSBoolean should never have free() called on it, since it is a shared
82*0f4c859eSApple OSS Distributions * object, with two non-mutable instances: kOSBooleanTrue, kOSBooleanFalse.
83*0f4c859eSApple OSS Distributions * There will be cases where an incorrect number of releases will cause the
84*0f4c859eSApple OSS Distributions * free() method to be called, however, which we must catch and ignore here.
85*0f4c859eSApple OSS Distributions */
86*0f4c859eSApple OSS Distributions assert(false);
87*0f4c859eSApple OSS Distributions }
88*0f4c859eSApple OSS Distributions
89*0f4c859eSApple OSS Distributions void
taggedRetain(__unused const void * tag) const90*0f4c859eSApple OSS Distributions OSBoolean::taggedRetain(__unused const void *tag) const
91*0f4c859eSApple OSS Distributions {
92*0f4c859eSApple OSS Distributions }
93*0f4c859eSApple OSS Distributions void
taggedRelease(__unused const void * tag,__unused const int when) const94*0f4c859eSApple OSS Distributions OSBoolean::taggedRelease(__unused const void *tag, __unused const int when) const
95*0f4c859eSApple OSS Distributions {
96*0f4c859eSApple OSS Distributions }
97*0f4c859eSApple OSS Distributions
98*0f4c859eSApple OSS Distributions OSBoolean *
withBoolean(bool inValue)99*0f4c859eSApple OSS Distributions OSBoolean::withBoolean(bool inValue)
100*0f4c859eSApple OSS Distributions {
101*0f4c859eSApple OSS Distributions return (inValue) ? kOSBooleanTrue : kOSBooleanFalse;
102*0f4c859eSApple OSS Distributions }
103*0f4c859eSApple OSS Distributions
104*0f4c859eSApple OSS Distributions bool
isTrue() const105*0f4c859eSApple OSS Distributions OSBoolean::isTrue() const
106*0f4c859eSApple OSS Distributions {
107*0f4c859eSApple OSS Distributions return value;
108*0f4c859eSApple OSS Distributions }
109*0f4c859eSApple OSS Distributions bool
isFalse() const110*0f4c859eSApple OSS Distributions OSBoolean::isFalse() const
111*0f4c859eSApple OSS Distributions {
112*0f4c859eSApple OSS Distributions return !value;
113*0f4c859eSApple OSS Distributions }
114*0f4c859eSApple OSS Distributions bool
getValue() const115*0f4c859eSApple OSS Distributions OSBoolean::getValue() const
116*0f4c859eSApple OSS Distributions {
117*0f4c859eSApple OSS Distributions return value;
118*0f4c859eSApple OSS Distributions }
119*0f4c859eSApple OSS Distributions
120*0f4c859eSApple OSS Distributions bool
isEqualTo(const OSBoolean * boolean) const121*0f4c859eSApple OSS Distributions OSBoolean::isEqualTo(const OSBoolean *boolean) const
122*0f4c859eSApple OSS Distributions {
123*0f4c859eSApple OSS Distributions return boolean == this;
124*0f4c859eSApple OSS Distributions }
125*0f4c859eSApple OSS Distributions
126*0f4c859eSApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const127*0f4c859eSApple OSS Distributions OSBoolean::isEqualTo(const OSMetaClassBase *obj) const
128*0f4c859eSApple OSS Distributions {
129*0f4c859eSApple OSS Distributions OSBoolean * boolean;
130*0f4c859eSApple OSS Distributions if ((boolean = OSDynamicCast(OSBoolean, obj))) {
131*0f4c859eSApple OSS Distributions return isEqualTo(boolean);
132*0f4c859eSApple OSS Distributions } else {
133*0f4c859eSApple OSS Distributions return false;
134*0f4c859eSApple OSS Distributions }
135*0f4c859eSApple OSS Distributions }
136*0f4c859eSApple OSS Distributions
137*0f4c859eSApple OSS Distributions bool
serialize(OSSerialize * s) const138*0f4c859eSApple OSS Distributions OSBoolean::serialize(OSSerialize *s) const
139*0f4c859eSApple OSS Distributions {
140*0f4c859eSApple OSS Distributions if (s->binary) {
141*0f4c859eSApple OSS Distributions return s->binarySerialize(this);
142*0f4c859eSApple OSS Distributions }
143*0f4c859eSApple OSS Distributions
144*0f4c859eSApple OSS Distributions return s->addString(value ? "<true/>" : "<false/>");
145*0f4c859eSApple OSS Distributions }
146