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