]> git.saurik.com Git - apple/xnu.git/blame - libkern/c++/OSNumber.cpp
xnu-792.tar.gz
[apple/xnu.git] / libkern / c++ / OSNumber.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
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.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
23
9bccf70c
A
24#include <sys/cdefs.h>
25
26__BEGIN_DECLS
91447636 27extern unsigned long strtoul(const char *, char **, int);
9bccf70c
A
28__END_DECLS
29
1c79356b
A
30#include <libkern/c++/OSNumber.h>
31#include <libkern/c++/OSString.h>
32#include <libkern/c++/OSSerialize.h>
33#include <libkern/c++/OSLib.h>
34
0b4e3aa0 35#define sizeMask (~0ULL >> (64 - size))
1c79356b
A
36
37#define super OSObject
38
39OSDefineMetaClassAndStructors(OSNumber, OSObject)
9bccf70c 40
1c79356b
A
41OSMetaClassDefineReservedUnused(OSNumber, 0);
42OSMetaClassDefineReservedUnused(OSNumber, 1);
43OSMetaClassDefineReservedUnused(OSNumber, 2);
44OSMetaClassDefineReservedUnused(OSNumber, 3);
45OSMetaClassDefineReservedUnused(OSNumber, 4);
46OSMetaClassDefineReservedUnused(OSNumber, 5);
47OSMetaClassDefineReservedUnused(OSNumber, 6);
48OSMetaClassDefineReservedUnused(OSNumber, 7);
49
50bool OSNumber::init(unsigned long long inValue, unsigned int numberOfBits)
51{
52 if (!super::init())
53 return false;
54
55 size = numberOfBits;
56 value = (inValue & sizeMask);
57
58 return true;
59}
60
61bool OSNumber::init(const char *value, unsigned int numberOfBits)
62{
91447636 63 return init((unsigned long long)strtoul(value, NULL, 0), numberOfBits);
1c79356b
A
64}
65
66void OSNumber::free() { super::free(); }
67
68OSNumber *OSNumber::withNumber(unsigned long long value,
69 unsigned int numberOfBits)
70{
71 OSNumber *me = new OSNumber;
72
73 if (me && !me->init(value, numberOfBits)) {
55e303ae 74 me->release();
1c79356b
A
75 return 0;
76 }
77
78 return me;
79}
80
81OSNumber *OSNumber::withNumber(const char *value, unsigned int numberOfBits)
82{
83 OSNumber *me = new OSNumber;
84
85 if (me && !me->init(value, numberOfBits)) {
55e303ae 86 me->release();
1c79356b
A
87 return 0;
88 }
89
90 return me;
91}
92
93unsigned int OSNumber::numberOfBits() const { return size; }
94
95unsigned int OSNumber::numberOfBytes() const { return (size + 7) / 8; }
96
97
98unsigned char OSNumber::unsigned8BitValue() const
99{
100 return (unsigned char) value;
101}
102
103unsigned short OSNumber::unsigned16BitValue() const
104{
105 return (unsigned short) value;
106}
107
108unsigned int OSNumber::unsigned32BitValue() const
109{
110 return (unsigned int) value;
111}
112
113unsigned long long OSNumber::unsigned64BitValue() const
114{
115 return value;
116}
117
118void OSNumber::addValue(signed long long inValue)
119{
120 value = ((value + inValue) & sizeMask);
121}
122
123void OSNumber::setValue(unsigned long long inValue)
124{
125 value = (inValue & sizeMask);
126}
127
128bool OSNumber::isEqualTo(const OSNumber *integer) const
129{
130 return((value == integer->value));
131}
132
133bool OSNumber::isEqualTo(const OSMetaClassBase *obj) const
134{
135 OSNumber * offset;
136 if ((offset = OSDynamicCast(OSNumber, obj)))
137 return isEqualTo(offset);
138 else
139 return false;
140}
141
142bool OSNumber::serialize(OSSerialize *s) const
143{
144 char temp[32];
145
146 if (s->previouslySerialized(this)) return true;
147
148 sprintf(temp, "integer size=\"%d\"", size);
149 if (!s->addXMLStartTag(this, temp)) return false;
150
151 //XXX sprintf(temp, "0x%qx", value);
152 if ((value >> 32)) {
153 sprintf(temp, "0x%lx%08lx", (unsigned long)(value >> 32),
154 (unsigned long)(value & 0xFFFFFFFF));
155 } else {
156 sprintf(temp, "0x%lx", (unsigned long)value);
157 }
158 if (!s->addString(temp)) return false;
159
160 return s->addXMLEndTag("integer");
161}