]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
3a1306a823f1b4f1b6f23623d4cbf0e30d72a7ce
[apple/xnu.git] / libkern / c++ / OSNumber.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 /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
23
24 #include <libkern/c++/OSNumber.h>
25 #include <libkern/c++/OSString.h>
26 #include <libkern/c++/OSSerialize.h>
27 #include <libkern/c++/OSLib.h>
28
29 #define sizeMask (~0ULL >> (64 - size))
30
31 #define super OSObject
32
33 OSDefineMetaClassAndStructors(OSNumber, OSObject)
34 OSMetaClassDefineReservedUnused(OSNumber, 0);
35 OSMetaClassDefineReservedUnused(OSNumber, 1);
36 OSMetaClassDefineReservedUnused(OSNumber, 2);
37 OSMetaClassDefineReservedUnused(OSNumber, 3);
38 OSMetaClassDefineReservedUnused(OSNumber, 4);
39 OSMetaClassDefineReservedUnused(OSNumber, 5);
40 OSMetaClassDefineReservedUnused(OSNumber, 6);
41 OSMetaClassDefineReservedUnused(OSNumber, 7);
42
43 bool OSNumber::init(unsigned long long inValue, unsigned int numberOfBits)
44 {
45 if (!super::init())
46 return false;
47
48 size = numberOfBits;
49 value = (inValue & sizeMask);
50
51 return true;
52 }
53
54 bool OSNumber::init(const char *value, unsigned int numberOfBits)
55 {
56 unsigned long long thisOffset;
57
58 #ifdef q_works
59 sscanf(value, "%qi", thisOffset);
60 #else
61 unsigned int smallOffset;
62
63 sscanf(value, "%i", &smallOffset);
64 thisOffset = smallOffset;
65 #endif
66
67 return init(thisOffset, numberOfBits);
68 }
69
70 void OSNumber::free() { super::free(); }
71
72 OSNumber *OSNumber::withNumber(unsigned long long value,
73 unsigned int numberOfBits)
74 {
75 OSNumber *me = new OSNumber;
76
77 if (me && !me->init(value, numberOfBits)) {
78 me->free();
79 return 0;
80 }
81
82 return me;
83 }
84
85 OSNumber *OSNumber::withNumber(const char *value, unsigned int numberOfBits)
86 {
87 OSNumber *me = new OSNumber;
88
89 if (me && !me->init(value, numberOfBits)) {
90 me->free();
91 return 0;
92 }
93
94 return me;
95 }
96
97 unsigned int OSNumber::numberOfBits() const { return size; }
98
99 unsigned int OSNumber::numberOfBytes() const { return (size + 7) / 8; }
100
101
102 unsigned char OSNumber::unsigned8BitValue() const
103 {
104 return (unsigned char) value;
105 }
106
107 unsigned short OSNumber::unsigned16BitValue() const
108 {
109 return (unsigned short) value;
110 }
111
112 unsigned int OSNumber::unsigned32BitValue() const
113 {
114 return (unsigned int) value;
115 }
116
117 unsigned long long OSNumber::unsigned64BitValue() const
118 {
119 return value;
120 }
121
122 void OSNumber::addValue(signed long long inValue)
123 {
124 value = ((value + inValue) & sizeMask);
125 }
126
127 void OSNumber::setValue(unsigned long long inValue)
128 {
129 value = (inValue & sizeMask);
130 }
131
132 bool OSNumber::isEqualTo(const OSNumber *integer) const
133 {
134 return((value == integer->value));
135 }
136
137 bool OSNumber::isEqualTo(const OSMetaClassBase *obj) const
138 {
139 OSNumber * offset;
140 if ((offset = OSDynamicCast(OSNumber, obj)))
141 return isEqualTo(offset);
142 else
143 return false;
144 }
145
146 bool OSNumber::serialize(OSSerialize *s) const
147 {
148 char temp[32];
149
150 if (s->previouslySerialized(this)) return true;
151
152 sprintf(temp, "integer size=\"%d\"", size);
153 if (!s->addXMLStartTag(this, temp)) return false;
154
155 //XXX sprintf(temp, "0x%qx", value);
156 if ((value >> 32)) {
157 sprintf(temp, "0x%lx%08lx", (unsigned long)(value >> 32),
158 (unsigned long)(value & 0xFFFFFFFF));
159 } else {
160 sprintf(temp, "0x%lx", (unsigned long)value);
161 }
162 if (!s->addString(temp)) return false;
163
164 return s->addXMLEndTag("integer");
165 }