]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
ffbc9e79359d00bfee6eb82d524769103ff8cca0
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
30 #include <sys/cdefs.h>
32 #include <libkern/c++/OSNumber.h>
33 #include <libkern/c++/OSString.h>
34 #include <libkern/c++/OSSerialize.h>
35 #include <libkern/c++/OSLib.h>
37 #define sizeMask (~0ULL >> (64 - size))
39 #define super OSObject
41 OSDefineMetaClassAndStructors(OSNumber
, OSObject
)
43 OSMetaClassDefineReservedUnused(OSNumber
, 0);
44 OSMetaClassDefineReservedUnused(OSNumber
, 1);
45 OSMetaClassDefineReservedUnused(OSNumber
, 2);
46 OSMetaClassDefineReservedUnused(OSNumber
, 3);
47 OSMetaClassDefineReservedUnused(OSNumber
, 4);
48 OSMetaClassDefineReservedUnused(OSNumber
, 5);
49 OSMetaClassDefineReservedUnused(OSNumber
, 6);
50 OSMetaClassDefineReservedUnused(OSNumber
, 7);
53 OSNumber::init(unsigned long long inValue
, unsigned int newNumberOfBits
)
58 if (newNumberOfBits
> 64) {
62 size
= newNumberOfBits
;
63 value
= (inValue
& sizeMask
);
69 OSNumber::init(const char *newValue
, unsigned int newNumberOfBits
)
71 return init((unsigned long long)strtoul(newValue
, NULL
, 0), newNumberOfBits
);
81 OSNumber::withNumber(unsigned long long value
,
82 unsigned int newNumberOfBits
)
84 OSNumber
*me
= new OSNumber
;
86 if (me
&& !me
->init(value
, newNumberOfBits
)) {
95 OSNumber::withNumber(const char *value
, unsigned int newNumberOfBits
)
97 OSNumber
*me
= new OSNumber
;
99 if (me
&& !me
->init(value
, newNumberOfBits
)) {
108 OSNumber::numberOfBits() const
114 OSNumber::numberOfBytes() const
116 return (size
+ 7) / 8;
121 OSNumber::unsigned8BitValue() const
123 return (unsigned char) value
;
127 OSNumber::unsigned16BitValue() const
129 return (unsigned short) value
;
133 OSNumber::unsigned32BitValue() const
135 return (unsigned int) value
;
139 OSNumber::unsigned64BitValue() const
145 OSNumber::addValue(signed long long inValue
)
147 value
= ((value
+ inValue
) & sizeMask
);
151 OSNumber::setValue(unsigned long long inValue
)
153 value
= (inValue
& sizeMask
);
157 OSNumber::isEqualTo(const OSNumber
*integer
) const
159 return value
== integer
->value
;
163 OSNumber::isEqualTo(const OSMetaClassBase
*obj
) const
166 if ((offset
= OSDynamicCast(OSNumber
, obj
))) {
167 return isEqualTo(offset
);
174 OSNumber::serialize(OSSerialize
*s
) const
178 if (s
->previouslySerialized(this)) {
182 snprintf(temp
, sizeof(temp
), "integer size=\"%d\"", size
);
183 if (!s
->addXMLStartTag(this, temp
)) {
187 //XXX sprintf(temp, "0x%qx", value);
189 snprintf(temp
, sizeof(temp
), "0x%lx%08lx", (unsigned long)(value
>> 32),
190 (unsigned long)(value
& 0xFFFFFFFF));
192 snprintf(temp
, sizeof(temp
), "0x%lx", (unsigned long)value
);
194 if (!s
->addString(temp
)) {
198 return s
->addXMLEndTag("integer");