1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 #import <Foundation/NSValue.h>
9 #import <Foundation/NSCoder.h>
10 #import <Foundation/NSRaise.h>
11 #import <Foundation/NSNumber_char.h>
12 #import <Foundation/NSNumber_double.h>
13 #import <Foundation/NSNumber_float.h>
14 #import <Foundation/NSNumber_int.h>
15 #import <Foundation/NSNumber_longLong.h>
16 #import <Foundation/NSNumber_long.h>
17 #import <Foundation/NSNumber_short.h>
18 #import <Foundation/NSNumber_unsignedChar.h>
19 #import <Foundation/NSNumber_unsignedInt.h>
20 #import <Foundation/NSNumber_unsignedLongLong.h>
21 #import <Foundation/NSNumber_unsignedLong.h>
22 #import <Foundation/NSNumber_unsignedShort.h>
23 #import <Foundation/NSNumber_BOOL.h>
24 #import <Foundation/NSNumber_placeholder.h>
25 #import <Foundation/NSAutoreleasePool-private.h>
29 @implementation NSNumber
31 +allocWithZone:(NSZone *)zone {
32 if(self==objc_lookUpClass("NSNumber"))
33 return [NSNumber_placeholder _sharedInstance];
35 return NSAllocateObject(self,0,zone);
38 -initWithBool:(BOOL)value {
39 NSInvalidAbstractInvocation();
43 -initWithChar:(char)value {
44 NSInvalidAbstractInvocation();
48 -initWithShort:(short)value {
49 NSInvalidAbstractInvocation();
53 -initWithInt:(int)value {
54 NSInvalidAbstractInvocation();
58 -initWithLong:(long)value {
59 NSInvalidAbstractInvocation();
63 -initWithLongLong:(long long)value {
64 NSInvalidAbstractInvocation();
68 -initWithFloat:(float)value {
69 NSInvalidAbstractInvocation();
73 -initWithDouble:(double)value {
74 NSInvalidAbstractInvocation();
78 -initWithInteger:(NSInteger)value {
79 NSInvalidAbstractInvocation();
83 -initWithUnsignedChar:(unsigned char)value {
84 NSInvalidAbstractInvocation();
88 -initWithUnsignedShort:(unsigned short)value {
89 NSInvalidAbstractInvocation();
93 -initWithUnsignedInt:(unsigned int)value {
94 NSInvalidAbstractInvocation();
98 -initWithUnsignedLong:(unsigned long)value {
99 NSInvalidAbstractInvocation();
103 -initWithUnsignedLongLong:(unsigned long long)value {
104 NSInvalidAbstractInvocation();
108 -initWithUnsignedInteger:(NSUInteger)value {
109 NSInvalidAbstractInvocation();
113 -copyWithZone:(NSZone *)zone {
114 return [self retain];
117 +(void)invalidType:(const char *)type {
118 [NSException raise:@"NSNumberCannotInitWithCoderException"
119 format:@"NSNumber cannot initWithCoder: type=%s",type];
122 -(Class)classForCoder {
123 return objc_lookUpClass("NSNumber");
126 -initWithCoder:(NSCoder *)coder {
131 [coder decodeValueOfObjCType:@encode(char *) at:&type];
133 [NSNumber invalidType:type];
140 [coder decodeValueOfObjCType:type at:&value];
141 return NSNumber_charNew(NULL,value);
148 [coder decodeValueOfObjCType:type at:&value];
149 return NSNumber_unsignedCharNew(NULL,value);
156 [coder decodeValueOfObjCType:type at:&value];
157 return NSNumber_shortNew (NULL,value);
162 unsigned short value;
164 [coder decodeValueOfObjCType:type at:&value];
165 return NSNumber_unsignedShortNew(NULL,value);
172 [coder decodeValueOfObjCType:type at:&value];
173 return NSNumber_intNew(NULL,value);
180 [coder decodeValueOfObjCType:type at:&value];
181 return NSNumber_unsignedIntNew(NULL,value);
188 [coder decodeValueOfObjCType:type at:&value];
189 return NSNumber_longNew(NULL,value);
196 [coder decodeValueOfObjCType:type at:&value];
197 return NSNumber_unsignedLongNew(NULL,value);
204 [coder decodeValueOfObjCType:type at:&value];
205 return NSNumber_longLongNew(NULL,value);
210 unsigned long long value;
212 [coder decodeValueOfObjCType:type at:&value];
213 return NSNumber_unsignedLongLongNew(NULL,value);
220 [coder decodeValueOfObjCType:type at:&value];
221 return NSNumber_floatNew(NULL,value);
228 [coder decodeValueOfObjCType:type at:&value];
229 return NSNumber_doubleNew(NULL,value);
234 [NSNumber invalidType:type];
241 -(void)encodeWithCoder:(NSCoder *)coder {
242 const char *type=[self objCType];
244 [coder encodeValueOfObjCType:@encode(char *) at:&type];
249 char value=[self charValue];;
251 [coder encodeValueOfObjCType:type at:&value];
256 unsigned char value=[self unsignedCharValue];
258 [coder encodeValueOfObjCType:type at:&value];
263 short value=[self shortValue];
265 [coder encodeValueOfObjCType:type at:&value];
270 unsigned short value=[self unsignedShortValue];
272 [coder encodeValueOfObjCType:type at:&value];
277 int value=[self intValue];
279 [coder encodeValueOfObjCType:type at:&value];
284 unsigned int value=[self unsignedIntValue];
286 [coder encodeValueOfObjCType:type at:&value];
291 long value=[self longValue];
293 [coder encodeValueOfObjCType:type at:&value];
298 unsigned long value=[self unsignedLongValue];
300 [coder encodeValueOfObjCType:type at:&value];
305 long long value=[self longLongValue];
307 [coder encodeValueOfObjCType:type at:&value];
312 unsigned long long value=[self unsignedLongLongValue];
314 [coder encodeValueOfObjCType:type at:&value];
319 float value=[self floatValue];
321 [coder encodeValueOfObjCType:type at:&value];
326 double value=[self doubleValue];
328 [coder encodeValueOfObjCType:type at:&value];
335 +(NSNumber *)numberWithBool:(BOOL)value {
336 return NSNumber_BOOLNew(NULL,value);
339 +(NSNumber *)numberWithChar:(char)value {
340 return [[[self allocWithZone:NULL] initWithChar:value] autorelease];
343 +(NSNumber *)numberWithShort:(short)value {
344 return [[[self allocWithZone:NULL] initWithShort:value] autorelease];
347 +(NSNumber *)numberWithInt:(int)value {
348 return [[[self allocWithZone:NULL] initWithInt:value] autorelease];
351 +(NSNumber *)numberWithLong:(long)value {
352 return [[[self allocWithZone:NULL] initWithLong:value] autorelease];
355 +(NSNumber *)numberWithLongLong:(long long)value {
356 return [[[self allocWithZone:NULL] initWithLongLong:value] autorelease];
359 +(NSNumber *)numberWithFloat:(float)value {
360 return [[[self allocWithZone:NULL] initWithFloat:value] autorelease];
363 +(NSNumber *)numberWithDouble:(double)value {
364 return [[[self allocWithZone:NULL] initWithDouble:value] autorelease];
367 +(NSNumber *)numberWithInteger:(NSInteger)value {
368 #if defined(__LP64__)
369 return [self numberWithLong:value];
371 return [self numberWithInt:value];
375 +(NSNumber *)numberWithUnsignedChar:(unsigned char)value {
376 return [[[self allocWithZone:NULL] initWithUnsignedChar:value] autorelease];
379 +(NSNumber *)numberWithUnsignedShort:(unsigned short)value {
380 return [[[self allocWithZone:NULL] initWithUnsignedShort:value] autorelease];
383 +(NSNumber *)numberWithUnsignedInt:(unsigned int)value {
384 return [[[self allocWithZone:NULL] initWithUnsignedInt:value] autorelease];
387 +(NSNumber *)numberWithUnsignedLong:(unsigned long)value {
388 return [[[self allocWithZone:NULL] initWithUnsignedLong:value] autorelease];
391 +(NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value {
392 return [[[self allocWithZone:NULL] initWithUnsignedLongLong:value] autorelease];
395 +(NSNumber *)numberWithUnsignedInteger:(NSUInteger)value {
396 #if defined(__LP64__)
397 return [self numberWithUnsignedLong:value];
399 return [self numberWithUnsignedInt:value];
404 -(NSComparisonResult)compare:(NSNumber *)other {
405 double d1 = [self doubleValue];
406 double d2 = [other doubleValue];
409 return NSOrderedSame;
411 return NSOrderedDescending;
413 return NSOrderedAscending;
416 -(BOOL)isEqual:other {
420 if(![other isKindOfClass:objc_lookUpClass("NSNumber")])
423 return [self isEqualToNumber:other];
426 -(BOOL)isEqualToNumber:(NSNumber *)other {
430 return ([self compare:other]==NSOrderedSame)?YES:NO;
435 return [self doubleValue];
439 NSInvalidAbstractInvocation();
444 NSInvalidAbstractInvocation();
449 NSInvalidAbstractInvocation();
454 NSInvalidAbstractInvocation();
459 NSInvalidAbstractInvocation();
463 -(long long)longLongValue {
464 NSInvalidAbstractInvocation();
469 NSInvalidAbstractInvocation();
473 -(double)doubleValue {
474 NSInvalidAbstractInvocation();
478 -(NSDecimal)decimalValue {
479 NSDecimal result = {0};
480 NSInvalidAbstractInvocation();
484 -(NSInteger)integerValue {
485 NSInvalidAbstractInvocation();
489 -(unsigned char)unsignedCharValue {
490 NSInvalidAbstractInvocation();
494 -(unsigned short)unsignedShortValue {
495 NSInvalidAbstractInvocation();
499 -(unsigned int)unsignedIntValue {
500 NSInvalidAbstractInvocation();
504 -(unsigned long)unsignedLongValue {
505 NSInvalidAbstractInvocation();
509 -(unsigned long long)unsignedLongLongValue {
510 NSInvalidAbstractInvocation();
514 -(NSUInteger)unsignedIntegerValue {
515 NSInvalidAbstractInvocation();
519 -(NSString *)stringValue {
520 return [self description];
523 -(NSString *)descriptionWithLocale:(NSDictionary *)locale {
524 NSInvalidAbstractInvocation();
525 return [NSString stringWithFormat:@"<%@ %p>",isa,self];
528 -(NSString *)description {
529 return [self descriptionWithLocale:nil];
534 #import <Foundation/NSCFTypeID.h>
536 @implementation NSNumber (CFTypeID)
538 - (unsigned) _cfTypeID
540 return kNSCFTypeNumber;