Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / NSNumber / NSNumber.m
blob15fe8c6c98cb3126aac8d249898c3fb7b7f4f703
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>
27 #include <string.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();
40    return nil;
43 -initWithChar:(char)value {
44    NSInvalidAbstractInvocation();
45    return nil;
48 -initWithShort:(short)value {
49    NSInvalidAbstractInvocation();
50    return nil;
53 -initWithInt:(int)value {
54    NSInvalidAbstractInvocation();
55    return nil;
58 -initWithLong:(long)value {
59    NSInvalidAbstractInvocation();
60    return nil;
63 -initWithLongLong:(long long)value {
64    NSInvalidAbstractInvocation();
65    return nil;
68 -initWithFloat:(float)value {
69    NSInvalidAbstractInvocation();
70    return nil;
73 -initWithDouble:(double)value {
74    NSInvalidAbstractInvocation();
75    return nil;
78 -initWithInteger:(NSInteger)value {
79    NSInvalidAbstractInvocation();
80    return nil;
83 -initWithUnsignedChar:(unsigned char)value {
84    NSInvalidAbstractInvocation();
85    return nil;
88 -initWithUnsignedShort:(unsigned short)value {
89    NSInvalidAbstractInvocation();
90    return nil;
93 -initWithUnsignedInt:(unsigned int)value {
94    NSInvalidAbstractInvocation();
95    return nil;
98 -initWithUnsignedLong:(unsigned long)value {
99    NSInvalidAbstractInvocation();
100    return nil;
103 -initWithUnsignedLongLong:(unsigned long long)value {
104    NSInvalidAbstractInvocation();
105    return nil;
108 -initWithUnsignedInteger:(NSUInteger)value {
109    NSInvalidAbstractInvocation();
110    return nil;
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 {
127    char *type;
129    [self dealloc];
131    [coder decodeValueOfObjCType:@encode(char *) at:&type];
132    if(strlen(type)!=1)
133     [NSNumber invalidType:type];
135    switch(*type){
137     case 'c':{
138       char value;
140       [coder decodeValueOfObjCType:type at:&value];
141       return NSNumber_charNew(NULL,value);
142      }
143      break;
145     case 'C':{
146       unsigned char value;
148       [coder decodeValueOfObjCType:type at:&value];
149       return NSNumber_unsignedCharNew(NULL,value);
150      }
151      break;
153     case 's':{
154       short value;
156       [coder decodeValueOfObjCType:type at:&value];
157       return NSNumber_shortNew (NULL,value);
158      }
159      break;
161     case 'S':{
162       unsigned short value;
164       [coder decodeValueOfObjCType:type at:&value];
165       return NSNumber_unsignedShortNew(NULL,value);
166      }
167      break;
169     case 'i':{
170       int value;
172       [coder decodeValueOfObjCType:type at:&value];
173       return NSNumber_intNew(NULL,value);
174      }
175      break;
177     case 'I':{
178       unsigned int value;
180       [coder decodeValueOfObjCType:type at:&value];
181       return NSNumber_unsignedIntNew(NULL,value);
182      }
183      break;
185     case 'l':{
186       long value;
188       [coder decodeValueOfObjCType:type at:&value];
189       return NSNumber_longNew(NULL,value);
190      }
191      break;
193     case 'L':{
194       unsigned long value;
196       [coder decodeValueOfObjCType:type at:&value];
197       return NSNumber_unsignedLongNew(NULL,value);
198      }
199      break;
201     case 'q':{
202       long long value;
204       [coder decodeValueOfObjCType:type at:&value];
205       return NSNumber_longLongNew(NULL,value);
206      }
207      break;
209     case 'Q':{
210       unsigned long long value;
212       [coder decodeValueOfObjCType:type at:&value];
213       return NSNumber_unsignedLongLongNew(NULL,value);
214      }
215      break;
217     case 'f':{
218       float value;
220       [coder decodeValueOfObjCType:type at:&value];
221       return NSNumber_floatNew(NULL,value);
222      }
223      break;
225     case 'd':{
226       double value;
228       [coder decodeValueOfObjCType:type at:&value];
229       return NSNumber_doubleNew(NULL,value);
230      }
231      break;
233     default:
234      [NSNumber invalidType:type];
235      break;
236    }
238    return nil;
241 -(void)encodeWithCoder:(NSCoder *)coder {
242    const char *type=[self objCType];
244    [coder encodeValueOfObjCType:@encode(char *) at:&type];
246    switch(*type){
248     case 'c':{
249       char value=[self charValue];;
251       [coder encodeValueOfObjCType:type at:&value];
252      }
253      break;
255     case 'C':{
256       unsigned char value=[self unsignedCharValue];
258       [coder encodeValueOfObjCType:type at:&value];
259      }
260      break;
262     case 's':{
263       short value=[self shortValue];
265       [coder encodeValueOfObjCType:type at:&value];
266      }
267      break;
269     case 'S':{
270       unsigned short value=[self unsignedShortValue];
272       [coder encodeValueOfObjCType:type at:&value];
273      }
274      break;
276     case 'i':{
277       int value=[self intValue];
279       [coder encodeValueOfObjCType:type at:&value];
280      }
281      break;
283     case 'I':{
284       unsigned int value=[self unsignedIntValue];
286       [coder encodeValueOfObjCType:type at:&value];
287      }
288      break;
290     case 'l':{
291       long value=[self longValue];
293       [coder encodeValueOfObjCType:type at:&value];
294      }
295      break;
297     case 'L':{
298       unsigned long value=[self unsignedLongValue];
300       [coder encodeValueOfObjCType:type at:&value];
301      }
302      break;
304     case 'q':{
305       long long value=[self longLongValue];
307       [coder encodeValueOfObjCType:type at:&value];
308      }
309      break;
311     case 'Q':{
312       unsigned long long value=[self unsignedLongLongValue];
314       [coder encodeValueOfObjCType:type at:&value];
315      }
316      break;
318     case 'f':{
319       float value=[self floatValue];
321       [coder encodeValueOfObjCType:type at:&value];
322      }
323      break;
325     case 'd':{
326       double value=[self doubleValue];
328       [coder encodeValueOfObjCType:type at:&value];
329      }
330      break;
331    }
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];
370 #else
371    return [self numberWithInt:value];
372 #endif
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];
398 #else
399    return [self numberWithUnsignedInt:value];
400 #endif
404 -(NSComparisonResult)compare:(NSNumber *)other {
405     double d1 = [self doubleValue];
406     double d2 = [other doubleValue];
408     if (d1 == d2)
409         return NSOrderedSame;
410     else if (d1 > d2)
411         return NSOrderedDescending;
412     else
413         return NSOrderedAscending;
416 -(BOOL)isEqual:other {
417    if(self==other)
418     return YES;
420    if(![other isKindOfClass:objc_lookUpClass("NSNumber")])
421     return NO;
423    return [self isEqualToNumber:other];
426 -(BOOL)isEqualToNumber:(NSNumber *)other {
427    if(self==other)
428     return YES;
430    return ([self compare:other]==NSOrderedSame)?YES:NO;
434 -(NSUInteger)hash {
435    return [self doubleValue];
438 -(BOOL)boolValue {
439    NSInvalidAbstractInvocation();
440    return 0;
443 -(char)charValue {
444    NSInvalidAbstractInvocation();
445    return 0;
448 -(short)shortValue {
449    NSInvalidAbstractInvocation();
450    return 0;
453 -(int)intValue {
454    NSInvalidAbstractInvocation();
455    return 0;
458 -(long)longValue {
459    NSInvalidAbstractInvocation();
460    return 0;
463 -(long long)longLongValue {
464    NSInvalidAbstractInvocation();
465    return 0;
468 -(float)floatValue {
469    NSInvalidAbstractInvocation();
470    return 0;
473 -(double)doubleValue {
474    NSInvalidAbstractInvocation();
475    return 0;
478 -(NSDecimal)decimalValue {
479    NSDecimal result = {0};
480    NSInvalidAbstractInvocation();
481    return result;
484 -(NSInteger)integerValue {
485    NSInvalidAbstractInvocation();
486    return 0;
489 -(unsigned char)unsignedCharValue {
490    NSInvalidAbstractInvocation();
491    return 0;
494 -(unsigned short)unsignedShortValue {
495    NSInvalidAbstractInvocation();
496    return 0;
499 -(unsigned int)unsignedIntValue {
500    NSInvalidAbstractInvocation();
501    return 0;
504 -(unsigned long)unsignedLongValue {
505    NSInvalidAbstractInvocation();
506    return 0;
509 -(unsigned long long)unsignedLongLongValue {
510    NSInvalidAbstractInvocation();
511    return 0;
514 -(NSUInteger)unsignedIntegerValue {
515    NSInvalidAbstractInvocation();
516    return 0;
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];
532 @end
534 #import <Foundation/NSCFTypeID.h>
536 @implementation NSNumber (CFTypeID)
538 - (unsigned) _cfTypeID
540    return kNSCFTypeNumber;
543 @end