]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSBoolean.cpp
cd3ba2ef940c323a871bc1b99afb8f401de7c75b
[apple/xnu.git] / libkern / c++ / OSBoolean.cpp
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* OSBoolean.cpp created by rsulack on Tue Oct 12 1999 */
23
24 #include <libkern/c++/OSBoolean.h>
25 #include <libkern/c++/OSString.h>
26 #include <libkern/c++/OSSerialize.h>
27 #include <libkern/c++/OSLib.h>
28
29 #define super OSObject
30
31 OSDefineMetaClassAndStructors(OSBoolean, OSObject)
32 OSMetaClassDefineReservedUnused(OSBoolean, 0);
33 OSMetaClassDefineReservedUnused(OSBoolean, 1);
34 OSMetaClassDefineReservedUnused(OSBoolean, 2);
35 OSMetaClassDefineReservedUnused(OSBoolean, 3);
36 OSMetaClassDefineReservedUnused(OSBoolean, 4);
37 OSMetaClassDefineReservedUnused(OSBoolean, 5);
38 OSMetaClassDefineReservedUnused(OSBoolean, 6);
39 OSMetaClassDefineReservedUnused(OSBoolean, 7);
40
41 static OSBoolean * gOSBooleanTrue = 0;
42 static OSBoolean * gOSBooleanFalse = 0;
43
44 OSBoolean * const & kOSBooleanTrue = gOSBooleanTrue;
45 OSBoolean * const & kOSBooleanFalse = gOSBooleanFalse;
46
47 void OSBoolean::initialize()
48 {
49 gOSBooleanTrue = new OSBoolean;
50 assert(gOSBooleanTrue);
51
52 if (!gOSBooleanTrue->init()) {
53 gOSBooleanTrue->OSObject::free();
54 assert(false);
55 };
56 gOSBooleanTrue->value = true;
57
58 gOSBooleanFalse = new OSBoolean;
59 assert(gOSBooleanFalse);
60
61 if (!gOSBooleanFalse->init()) {
62 gOSBooleanFalse->OSObject::free();
63 assert(false);
64 };
65 gOSBooleanFalse->value = false;
66 }
67
68 void OSBoolean::free()
69 {
70 /*
71 * An OSBoolean should never have free() called on it, since it is a shared
72 * object, with two non-mutable instances: kOSBooleanTrue, kOSBooleanFalse.
73 * There will be cases where an incorrect number of releases will cause the
74 * free() method to be called, however, which we must catch and ignore here.
75 */
76 assert(false);
77 }
78
79 OSBoolean *OSBoolean::withBoolean(bool inValue)
80 {
81 if (inValue) {
82 kOSBooleanTrue->retain();
83 return kOSBooleanTrue;
84 } else {
85 kOSBooleanFalse->retain();
86 return kOSBooleanFalse;
87 }
88 }
89
90 bool OSBoolean::isTrue() const { return value; }
91 bool OSBoolean::isFalse() const { return !value; }
92 bool OSBoolean::getValue() const { return value; }
93
94 bool OSBoolean::isEqualTo(const OSBoolean *boolean) const
95 {
96 return (boolean == this);
97 }
98
99 bool OSBoolean::isEqualTo(const OSMetaClassBase *obj) const
100 {
101 OSBoolean * boolean;
102 if ((boolean = OSDynamicCast(OSBoolean, obj)))
103 return isEqualTo(boolean);
104 else
105 return false;
106 }
107
108 bool OSBoolean::serialize(OSSerialize *s) const
109 {
110 return s->addString(value ? "<true/>" : "<false/>");
111 }