xref: /xnu-11215/libkern/c++/OSBoolean.cpp (revision 368ad365)
1 /*
2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
7  *
8  * This file contains Original Code and/or Modifications of Original Code
9  * as defined in and that are subject to the Apple Public Source License
10  * Version 2.0 (the 'License'). You may not use this file except in
11  * compliance with the License. Please obtain a copy of the License at
12  * http://www.opensource.apple.com/apsl/ and read it before using this
13  * file.
14  *
15  * The Original Code and all software distributed under the License are
16  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20  * Please see the License for the specific language governing rights and
21  * limitations under the License.
22  *
23  * @APPLE_LICENSE_HEADER_END@
24  */
25 /* OSBoolean.cpp created by rsulack on Tue Oct 12 1999 */
26 
27 #include <libkern/c++/OSBoolean.h>
28 #include <libkern/c++/OSString.h>
29 #include <libkern/c++/OSSerialize.h>
30 #include <libkern/c++/OSLib.h>
31 
32 #define super OSObject
33 
34 OSDefineMetaClassAndStructors(OSBoolean, OSObject)
35 OSMetaClassDefineReservedUnused(OSBoolean, 0);
36 OSMetaClassDefineReservedUnused(OSBoolean, 1);
37 OSMetaClassDefineReservedUnused(OSBoolean, 2);
38 OSMetaClassDefineReservedUnused(OSBoolean, 3);
39 OSMetaClassDefineReservedUnused(OSBoolean, 4);
40 OSMetaClassDefineReservedUnused(OSBoolean, 5);
41 OSMetaClassDefineReservedUnused(OSBoolean, 6);
42 OSMetaClassDefineReservedUnused(OSBoolean, 7);
43 
44 static OSBoolean * gOSBooleanTrue  = 0;
45 static OSBoolean * gOSBooleanFalse = 0;
46 
47 OSBoolean * const & kOSBooleanTrue  = gOSBooleanTrue;
48 OSBoolean * const & kOSBooleanFalse = gOSBooleanFalse;
49 
50 void OSBoolean::initialize()
51 {
52     gOSBooleanTrue = new OSBoolean;
53     assert(gOSBooleanTrue);
54 
55     if (!gOSBooleanTrue->init()) {
56         gOSBooleanTrue->OSObject::free();
57         assert(false);
58     };
59     gOSBooleanTrue->value = true;
60 
61     gOSBooleanFalse = new OSBoolean;
62     assert(gOSBooleanFalse);
63 
64     if (!gOSBooleanFalse->init()) {
65         gOSBooleanFalse->OSObject::free();
66         assert(false);
67     };
68     gOSBooleanFalse->value = false;
69 }
70 
71 void OSBoolean::free()
72 {
73     /*
74      * An OSBoolean should never have free() called on it, since it is a shared
75      * object, with two non-mutable instances: kOSBooleanTrue, kOSBooleanFalse.
76      * There will be cases where an incorrect number of releases will cause the
77      * free() method to be called, however, which we must catch and ignore here.
78      */
79     assert(false);
80 }
81 
82 void OSBoolean::taggedRetain(const void *tag) const { }
83 void OSBoolean::taggedRelease(const void *tag, const int when) const { }
84 
85 OSBoolean *OSBoolean::withBoolean(bool inValue)
86 {
87     return (inValue) ? kOSBooleanTrue : kOSBooleanFalse;
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 }
112