]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
2f94543e626828bf1d69b1e03e568728aaa26eab
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
24 #include <libkern/c++/OSNumber.h>
25 #include <libkern/c++/OSString.h>
26 #include <libkern/c++/OSSerialize.h>
27 #include <libkern/c++/OSLib.h>
29 #define sizeMask ((1ULL << (size)) - 1)
31 #define super OSObject
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);
43 bool OSNumber::init(unsigned long long inValue
, unsigned int numberOfBits
)
49 value
= (inValue
& sizeMask
);
54 bool OSNumber::init(const char *value
, unsigned int numberOfBits
)
56 unsigned long long thisOffset
;
59 sscanf(value
, "%qi", thisOffset
);
61 unsigned int smallOffset
;
63 sscanf(value
, "%i", &smallOffset
);
64 thisOffset
= smallOffset
;
67 return init(thisOffset
, numberOfBits
);
70 void OSNumber::free() { super::free(); }
72 OSNumber
*OSNumber::withNumber(unsigned long long value
,
73 unsigned int numberOfBits
)
75 OSNumber
*me
= new OSNumber
;
77 if (me
&& !me
->init(value
, numberOfBits
)) {
85 OSNumber
*OSNumber::withNumber(const char *value
, unsigned int numberOfBits
)
87 OSNumber
*me
= new OSNumber
;
89 if (me
&& !me
->init(value
, numberOfBits
)) {
97 unsigned int OSNumber::numberOfBits() const { return size
; }
99 unsigned int OSNumber::numberOfBytes() const { return (size
+ 7) / 8; }
102 unsigned char OSNumber::unsigned8BitValue() const
104 return (unsigned char) value
;
107 unsigned short OSNumber::unsigned16BitValue() const
109 return (unsigned short) value
;
112 unsigned int OSNumber::unsigned32BitValue() const
114 return (unsigned int) value
;
117 unsigned long long OSNumber::unsigned64BitValue() const
122 void OSNumber::addValue(signed long long inValue
)
124 value
= ((value
+ inValue
) & sizeMask
);
127 void OSNumber::setValue(unsigned long long inValue
)
129 value
= (inValue
& sizeMask
);
132 bool OSNumber::isEqualTo(const OSNumber
*integer
) const
134 return((value
== integer
->value
));
137 bool OSNumber::isEqualTo(const OSMetaClassBase
*obj
) const
140 if ((offset
= OSDynamicCast(OSNumber
, obj
)))
141 return isEqualTo(offset
);
146 bool OSNumber::serialize(OSSerialize
*s
) const
150 if (s
->previouslySerialized(this)) return true;
152 sprintf(temp
, "integer size=\"%d\"", size
);
153 if (!s
->addXMLStartTag(this, temp
)) return false;
155 //XXX sprintf(temp, "0x%qx", value);
157 sprintf(temp
, "0x%lx%08lx", (unsigned long)(value
>> 32),
158 (unsigned long)(value
& 0xFFFFFFFF));
160 sprintf(temp
, "0x%lx", (unsigned long)value
);
162 if (!s
->addString(temp
)) return false;
164 return s
->addXMLEndTag("integer");