~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

« back to all changes in this revision

Viewing changes to Source/NSNumber.m

Tags: upstream-1.20.0
ImportĀ upstreamĀ versionĀ 1.20.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** NSNumber - Object encapsulation of numbers
2
 
 
3
 
   Copyright (C) 1993, 1994, 1996, 2000 Free Software Foundation, Inc.
4
 
 
5
 
   Written by:  Adam Fedor <fedor@boulder.colorado.edu>
6
 
   Created: Mar 1995
7
 
   Rewrite: Richard Frith-Macdonald <rfm@gnu.org>
8
 
   Date: Mar 2000
 
1
/** Implementation of NSNumber for GNUStep
 
2
   Copyright (C) 2010 Free Software Foundation, Inc.
 
3
 
 
4
   Written by:  David Chisnall
 
5
   Partial rewrite:  Richard Frith-Macdonld <rfm@gnu.org>
 
6
    (to compile on gnu/linux and mswindows, to meet coding/style standards)
 
7
   
 
8
   Date: February 2010
9
9
 
10
10
   This file is part of the GNUstep Base Library.
11
11
 
24
24
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25
25
   Boston, MA 02111 USA.
26
26
 
27
 
   <title>NSNumber class reference</title>
28
 
   $Date: 2008-06-08 11:38:33 +0100 (Sun, 08 Jun 2008) $ $Revision: 26606 $
29
 
*/
30
 
 
31
 
#include <string.h>
32
 
#include "config.h"
33
 
#include "GNUstepBase/preface.h"
34
 
#include "Foundation/NSException.h"
35
 
#include "Foundation/NSString.h"
36
 
#include "Foundation/NSNotification.h"
37
 
#include "Foundation/NSMapTable.h"
38
 
#include "Foundation/NSThread.h"
39
 
#include "Foundation/NSCoder.h"
40
 
#include "Foundation/NSPortCoder.h"
41
 
#include "Foundation/NSObjCRuntime.h"
42
 
 
43
 
#include "NSConcreteNumber.h"
44
 
#include "GSPrivate.h"
45
 
 
46
 
@interface GSCachedBool : NSBoolNumber
47
 
@end
48
 
@interface GSCachedInt : NSIntNumber
49
 
@end
50
 
@implementation GSCachedBool
51
 
- (id) copyWithZone: (NSZone*)zone
52
 
{
53
 
  return RETAIN(self);
54
 
}
55
 
- (void) dealloc
56
 
{
57
 
  [NSException raise: NSGenericException
58
 
              format: @"Attempt to deallocate bool number owned by cache"];
59
 
  GSNOSUPERDEALLOC;
60
 
}
61
 
@end
62
 
@implementation GSCachedInt
63
 
- (id) copyWithZone: (NSZone*)zone
64
 
{
65
 
  return RETAIN(self);
66
 
}
67
 
- (void) dealloc
68
 
{
69
 
  [NSException raise: NSGenericException
70
 
              format: @"Attempt to deallocate int number owned by cache"];
71
 
  GSNOSUPERDEALLOC;
72
 
}
 
27
   */
 
28
 
 
29
 
 
30
#import "common.h"
 
31
 
 
32
#if     !defined(LLONG_MAX)
 
33
#  if   defined(__LONG_LONG_MAX__)
 
34
#    define LLONG_MAX __LONG_LONG_MAX__
 
35
#    define LLONG_MIN   (-LLONG_MAX-1)
 
36
#    define ULLONG_MAX  (LLONG_MAX * 2ULL + 1)
 
37
#  else
 
38
#    error Neither LLONG_MAX nor __LONG_LONG_MAX__ found
 
39
#  endif
 
40
#endif
 
41
 
 
42
 
 
43
#import "Foundation/NSCoder.h"
 
44
#import "Foundation/NSDecimalNumber.h"
 
45
#import "Foundation/NSException.h"
 
46
#import "Foundation/NSValue.h"
 
47
#import "GNUstepBase/NSObject+GNUstepBase.h"
 
48
 
 
49
/*
 
50
 * NSNumber implementation.  This matches the behaviour of Apple's
 
51
 * implementation.  Values in the range -1 to 12 inclusive are mapped to
 
52
 * singletons.  All other values are mapped to the smallest signed value that
 
53
 * will store them, unless they are greater than LLONG_MAX, in which case
 
54
 * they are stored in an unsigned long long.
 
55
 */
 
56
 
 
57
@interface NSSignedIntegerNumber : NSNumber
 
58
@end
 
59
 
 
60
@interface NSIntNumber : NSSignedIntegerNumber
 
61
{
 
62
@public
 
63
  int value;
 
64
}
 
65
@end
 
66
 
 
67
@interface NSLongLongNumber : NSSignedIntegerNumber
 
68
{
 
69
@public
 
70
  long long int value;
 
71
}
 
72
@end
 
73
 
 
74
@interface NSUnsignedLongLongNumber : NSNumber
 
75
{
 
76
@public
 
77
  unsigned long long int value;
 
78
}
 
79
@end
 
80
 
 
81
// The value ivar in all of the concrete classes contains the real value.
 
82
#define VALUE value
 
83
#define COMPARE(value, other) \
 
84
if (value < other)\
 
85
  {\
 
86
    return NSOrderedAscending;\
 
87
  }\
 
88
if (value > other)\
 
89
  {\
 
90
    return NSOrderedDescending;\
 
91
  }\
 
92
return NSOrderedSame;
 
93
 
 
94
@implementation NSSignedIntegerNumber
 
95
- (NSComparisonResult) compare: (NSNumber*)aNumber
 
96
{
 
97
  if (aNumber == self)
 
98
    {
 
99
      return NSOrderedSame;
 
100
    }
 
101
  if (aNumber == nil)
 
102
    {
 
103
      [NSException raise: NSInvalidArgumentException
 
104
                  format: @"nil argument for compare:"];
 
105
    }
 
106
 
 
107
  switch ([aNumber objCType][0])
 
108
    {
 
109
      /* For cases smaller than or equal to an int, we could get the int
 
110
       * value and compare.
 
111
       */
 
112
      case 'c':
 
113
      case 'C':
 
114
      case 's':
 
115
      case 'S':
 
116
      case 'i':
 
117
      case 'I':
 
118
      case 'l':
 
119
      case 'L':
 
120
      case 'q':
 
121
        {
 
122
          long long value = [self longLongValue];
 
123
          long long other = [aNumber longLongValue];
 
124
 
 
125
          COMPARE (value, other);
 
126
        }
 
127
      case 'Q':
 
128
        {
 
129
          unsigned long long other;
 
130
          unsigned long long value;
 
131
          long long v;
 
132
 
 
133
          /* According to the C type promotion rules, we should cast this to
 
134
           * an unsigned long long, however Apple's code does not do this.
 
135
           * Instead, it performs a real comparison.
 
136
           */
 
137
          v = [self longLongValue];
 
138
 
 
139
          /* If this value is less than 0, then it is less than any value
 
140
           * that can possibly be stored in an unsigned value.
 
141
           */
 
142
          if (v < 0)
 
143
            {
 
144
              return NSOrderedAscending;
 
145
            }
 
146
 
 
147
          other = [aNumber unsignedLongLongValue];
 
148
          value = (unsigned long long) v;
 
149
          COMPARE (value, other);
 
150
        }
 
151
      case 'f':
 
152
      case 'd':
 
153
        {
 
154
          double other = [aNumber doubleValue];
 
155
          double value = [self doubleValue];
 
156
 
 
157
          COMPARE (value, other);
 
158
        }
 
159
      default:
 
160
        [NSException raise: NSInvalidArgumentException
 
161
                    format: @"unrecognised type for compare:"];
 
162
    }
 
163
  return 0;                     // Not reached.
 
164
}
 
165
@end
 
166
 
 
167
@implementation NSIntNumber
 
168
#define FORMAT @"%i"
 
169
#include "NSNumberMethods.h"
 
170
@end
 
171
 
 
172
@implementation NSLongLongNumber
 
173
#define FORMAT @"%lli"
 
174
#include "NSNumberMethods.h"
 
175
@end
 
176
 
 
177
@implementation NSUnsignedLongLongNumber
 
178
#define FORMAT @"%llu"
 
179
#include "NSNumberMethods.h"
 
180
- (NSComparisonResult) compare: (NSNumber*)aNumber
 
181
{
 
182
  if (aNumber == self)
 
183
    {
 
184
      return NSOrderedSame;
 
185
    }
 
186
  if (aNumber == nil)
 
187
    {
 
188
      [NSException raise: NSInvalidArgumentException
 
189
                  format: @"nil argument for compare:"];
 
190
    }
 
191
 
 
192
  switch ([aNumber objCType][0])
 
193
    {
 
194
      /* For cases smaller than or equal to an int, we could get the int
 
195
       * value and compare.
 
196
       */
 
197
      case 'c':
 
198
      case 'C':
 
199
      case 's':
 
200
      case 'S':
 
201
      case 'i':
 
202
      case 'I':
 
203
      case 'l':
 
204
      case 'L':
 
205
      case 'q':
 
206
        {
 
207
          long long other = [aNumber longLongValue];
 
208
 
 
209
          if (other < 0)
 
210
            {
 
211
              return NSOrderedDescending;
 
212
            }
 
213
          COMPARE (value, ((unsigned long long) other));
 
214
        }
 
215
      case 'Q':
 
216
        {
 
217
          unsigned long long other = [aNumber unsignedLongLongValue];
 
218
 
 
219
          COMPARE (value, other);
 
220
        }
 
221
      case 'f':
 
222
      case 'd':
 
223
        {
 
224
          double other = [aNumber doubleValue];
 
225
 
 
226
          COMPARE (((double) value), other);
 
227
        }
 
228
      default:
 
229
        [NSException raise: NSInvalidArgumentException
 
230
                    format: @"unrecognised type for compare:"];
 
231
    }
 
232
  return 0;                     // Not reached.
 
233
}
 
234
@end
 
235
 
 
236
/*
 
237
 * Abstract superclass for floating point numbers.
 
238
 */
 
239
@interface NSFloatingPointNumber : NSNumber
 
240
@end
 
241
 
 
242
@implementation NSFloatingPointNumber
 
243
/* For floats, the type promotion rules say that we always promote to a
 
244
 * floating point type, even if the other value is really an integer.
 
245
 */
 
246
- (BOOL) isEqualToNumber: (NSNumber*)aNumber
 
247
{
 
248
  return ([self doubleValue] == [aNumber doubleValue]) ? YES : NO;
 
249
}
 
250
 
 
251
- (NSComparisonResult) compare: (NSNumber*)aNumber
 
252
{
 
253
  double other;
 
254
  double value;
 
255
 
 
256
  if (aNumber == self)
 
257
    {
 
258
      return NSOrderedSame;
 
259
    }
 
260
  if (aNumber == nil)
 
261
    {
 
262
      [NSException raise: NSInvalidArgumentException
 
263
                  format: @"nil argument for compare:"];
 
264
    }
 
265
  other = [aNumber doubleValue];
 
266
  value = [self doubleValue];
 
267
  COMPARE (value, other);
 
268
}
 
269
@end
 
270
 
 
271
@interface NSFloatNumber : NSFloatingPointNumber
 
272
{
 
273
@public
 
274
  float value;
 
275
}
 
276
@end
 
277
 
 
278
@implementation NSFloatNumber
 
279
#define FORMAT @"%0.7g"
 
280
#include "NSNumberMethods.h"
 
281
@end
 
282
 
 
283
@interface NSDoubleNumber : NSFloatingPointNumber
 
284
{
 
285
@public
 
286
  double value;
 
287
}
 
288
@end
 
289
 
 
290
@implementation NSDoubleNumber
 
291
#define FORMAT @"%0.16g"
 
292
#include "NSNumberMethods.h"
73
293
@end
74
294
 
75
295
@implementation NSNumber
76
296
 
77
 
static NSMapTable       *numberMap;
78
 
static BOOL             multiThreaded = NO;
79
 
static NSNumber         *boolN;
80
 
static NSNumber         *boolY;
81
 
static NSNumber         *smallIntegers[GS_SMALL * 2 + 1];
82
 
static unsigned int     smallHashes[GS_SMALL * 2 + 1];
83
 
 
84
297
/*
85
 
 * Cache info for each number class.
86
 
 * In a multi-threaded system we may waste some memory in order to get speed.
 
298
 * Numbers from -1 to 12 inclusive that are reused.
87
299
 */
88
 
GSNumberInfo*
89
 
GSNumberInfoFromObject(NSNumber *o)
90
 
{
91
 
  Class         c;
92
 
  GSNumberInfo  *info;
93
 
 
94
 
  if (o == nil)
95
 
    return 0;
96
 
  c = GSObjCClass(o);
97
 
  info = (GSNumberInfo*)NSMapGet (numberMap, (void*)c);
98
 
  if (info == 0)
99
 
    {
100
 
      const char        *t = [o objCType];
101
 
      int               order = -1;
102
 
 
103
 
      if (strlen(t) != 1)
104
 
        {
105
 
          NSLog(@"Invalid return value (%s) from [%@ objCType]", t, c);
106
 
        }
107
 
      else
108
 
        {
109
 
          switch (*t)
110
 
            {
111
 
              case 'c': order = 1;      break;
112
 
              case 'C': order = 2;      break;
113
 
              case 's': order = 3;      break;
114
 
              case 'S': order = 4;      break;
115
 
              case 'i': order = 5;      break;
116
 
              case 'I': order = 6;      break;
117
 
              case 'l': order = 7;      break;
118
 
              case 'L': order = 8;      break;
119
 
              case 'q': order = 9;      break;
120
 
              case 'Q': order = 10;     break;
121
 
              case 'f': order = 11;     break;
122
 
              case 'd': order = 12;     break;
123
 
              default:
124
 
                NSLog(@"Invalid return value (%s) from [%@ objCType]", t, c);
125
 
                break;
126
 
            }
127
 
        }
128
 
      info = (GSNumberInfo*)NSZoneMalloc(NSDefaultMallocZone(),
129
 
        (sizeof(GSNumberInfo)));
130
 
      info->typeLevel = order;
131
 
 
132
 
      info->getValue = (void (*)(NSNumber*, SEL, void*))
133
 
        [o methodForSelector: @selector(getValue:)];
134
 
 
135
 
      if (multiThreaded == YES)
136
 
        {
137
 
          NSMapTable    *table;
138
 
 
139
 
          /*
140
 
           * Memory leak for efficiency - the old map table is never
141
 
           * deallocated, so we don't have to do any locking.
142
 
           */
143
 
          table = NSCopyMapTableWithZone(numberMap, NSDefaultMallocZone());
144
 
          NSMapInsert(table, (void*)c, (void*)info);
145
 
          numberMap = table;
146
 
        }
147
 
      else
148
 
        {
149
 
          NSMapInsert(numberMap, (void*)c, (void*)info);
150
 
        }
151
 
    }
152
 
  return info;
153
 
}
154
 
 
155
 
unsigned int
156
 
GSPrivateSmallHash(int n)
157
 
{
158
 
  return smallHashes[n + GS_SMALL];
159
 
}
160
 
 
161
 
static Class    abstractClass;
162
 
static Class    boolNumberClass;
163
 
static Class    charNumberClass;
164
 
static Class    uCharNumberClass;
165
 
static Class    shortNumberClass;
166
 
static Class    uShortNumberClass;
167
 
static Class    intNumberClass;
168
 
static Class    uIntNumberClass;
169
 
static Class    longNumberClass;
170
 
static Class    uLongNumberClass;
171
 
static Class    longLongNumberClass;
172
 
static Class    uLongLongNumberClass;
173
 
static Class    floatNumberClass;
174
 
static Class    doubleNumberClass;
175
 
 
176
 
+ (void) _becomeThreaded: (NSNotification*)notification
177
 
{
178
 
  multiThreaded = YES;
179
 
}
 
300
static NSNumber *ReusedInstances[14];
 
301
static Class NSNumberClass;
 
302
static Class NSIntNumberClass;
 
303
static Class NSLongLongNumberClass;
 
304
static Class NSUnsignedLongLongNumberClass;
 
305
static Class NSFloatNumberClass;
 
306
static Class NSDoubleNumberClass;
180
307
 
181
308
+ (void) initialize
182
309
{
183
 
  if (self == [NSNumber class])
184
 
    {
185
 
      BOOL      boolean;
186
 
      int       integer;
187
 
      unsigned  (*hasher)(NSNumber*, SEL);
188
 
      GSNumberInfo      *info;
189
 
      CREATE_AUTORELEASE_POOL(pool);
190
 
 
191
 
      abstractClass = self;
192
 
      hasher = (unsigned (*)(NSNumber*, SEL))
193
 
        [self instanceMethodForSelector: @selector(hash)];
194
 
 
195
 
      /*
196
 
       * Create cache for per-subclass method implementations etc.
197
 
       */
198
 
      numberMap = NSCreateMapTable (NSNonOwnedPointerMapKeyCallBacks,
199
 
        NSOwnedPointerMapValueCallBacks, 0);
200
 
 
201
 
      /*
202
 
       * cache standard subclass info.
203
 
       */
204
 
      boolNumberClass = [NSBoolNumber class];
205
 
      info = GSNumberInfoFromObject(AUTORELEASE([boolNumberClass alloc]));
206
 
      /*
207
 
       * Set the typeLevel for a boolean to be '0'
208
 
       */
209
 
      info->typeLevel = 0;
210
 
      charNumberClass = [NSCharNumber class];
211
 
      GSNumberInfoFromObject(AUTORELEASE([charNumberClass alloc]));
212
 
      uCharNumberClass = [NSUCharNumber class];
213
 
      GSNumberInfoFromObject(AUTORELEASE([uCharNumberClass alloc]));
214
 
      shortNumberClass = [NSShortNumber class];
215
 
      GSNumberInfoFromObject(AUTORELEASE([shortNumberClass alloc]));
216
 
      uShortNumberClass = [NSUShortNumber class];
217
 
      GSNumberInfoFromObject(AUTORELEASE([uShortNumberClass alloc]));
218
 
      intNumberClass = [NSIntNumber class];
219
 
      GSNumberInfoFromObject(AUTORELEASE([intNumberClass alloc]));
220
 
      uIntNumberClass = [NSUIntNumber class];
221
 
      GSNumberInfoFromObject(AUTORELEASE([uIntNumberClass alloc]));
222
 
      longNumberClass = [NSLongNumber class];
223
 
      GSNumberInfoFromObject(AUTORELEASE([longNumberClass alloc]));
224
 
      uLongNumberClass = [NSULongNumber class];
225
 
      GSNumberInfoFromObject(AUTORELEASE([uLongNumberClass alloc]));
226
 
      longLongNumberClass = [NSLongLongNumber class];
227
 
      GSNumberInfoFromObject(AUTORELEASE([longLongNumberClass alloc]));
228
 
      uLongLongNumberClass = [NSULongLongNumber class];
229
 
      GSNumberInfoFromObject(AUTORELEASE([uLongLongNumberClass alloc]));
230
 
      floatNumberClass = [NSFloatNumber class];
231
 
      GSNumberInfoFromObject(AUTORELEASE([floatNumberClass alloc]));
232
 
      doubleNumberClass = [NSDoubleNumber class];
233
 
      GSNumberInfoFromObject(AUTORELEASE([doubleNumberClass alloc]));
234
 
 
235
 
      /*
236
 
       * cache bool values.
237
 
       */
238
 
      boolN = (NSNumber*)NSAllocateObject([GSCachedBool class], 0,
239
 
        NSDefaultMallocZone());
240
 
      boolean = NO;
241
 
      boolN = [boolN initWithBytes: &boolean objCType: NULL];
242
 
 
243
 
      boolY = (NSNumber*)NSAllocateObject([GSCachedBool class], 0,
244
 
        NSDefaultMallocZone());
245
 
      boolean = YES;
246
 
      boolY = [boolY initWithBytes: &boolean objCType: NULL];
247
 
 
248
 
      /*
249
 
       * cache small integer values.
250
 
       */
251
 
      for (integer = -GS_SMALL; integer <= GS_SMALL; integer++)
252
 
        {
253
 
          NSNumber      *num;
254
 
 
255
 
          num = (NSNumber*)NSAllocateObject([GSCachedInt class], 0,
256
 
            NSDefaultMallocZone());
257
 
          num = [num initWithBytes: &integer objCType: NULL];
258
 
          smallIntegers[integer + GS_SMALL] = num;
259
 
          smallHashes[integer + GS_SMALL] = (*hasher)(num, @selector(hash));
260
 
        }
261
 
 
262
 
      /*
263
 
       * Make sure we know if we are multi-threaded so that if the caches
264
 
       * need to grow, we do it by copying and replacing without deleting
265
 
       * an old cache that may be in use by another thread.
266
 
       */
267
 
      if ([NSThread isMultiThreaded])
268
 
        {
269
 
          [self _becomeThreaded: nil];
270
 
        }
271
 
      else
272
 
        {
273
 
          [[NSNotificationCenter defaultCenter]
274
 
            addObserver: self
275
 
               selector: @selector(_becomeThreaded:)
276
 
                   name: NSWillBecomeMultiThreadedNotification
277
 
                 object: nil];
278
 
        }
279
 
      RELEASE(pool);
280
 
    }
281
 
}
282
 
 
283
 
/* Returns the concrete class associated with the type encoding. Note
284
 
   that we don't allow NSNumber to instantiate any class but its own
285
 
   concrete subclasses (see check at end of method) */
286
 
+ (Class) valueClassWithObjCType: (const char*)type
287
 
{
288
 
  Class theClass = Nil;
289
 
 
290
 
  switch (*type)
291
 
    {
292
 
      case _C_CHR:      return charNumberClass;
293
 
      case _C_UCHR:     return uCharNumberClass;
294
 
      case _C_SHT:      return shortNumberClass;
295
 
      case _C_USHT:     return uShortNumberClass;
296
 
      case _C_INT:      return intNumberClass;
297
 
      case _C_UINT:     return uIntNumberClass;
298
 
      case _C_LNG:      return longNumberClass;
299
 
      case _C_ULNG:     return uLongNumberClass;
300
 
#ifdef  _C_LNGLNG
301
 
      case _C_LNGLNG:
302
 
#else
 
310
  int i;
 
311
 
 
312
  if ([NSNumber class] != self)
 
313
    {
 
314
      return;
 
315
    }
 
316
 
 
317
  NSNumberClass = self;
 
318
  NSIntNumberClass = [NSIntNumber class];
 
319
  NSLongLongNumberClass = [NSLongLongNumber class];
 
320
  NSUnsignedLongLongNumberClass = [NSUnsignedLongLongNumber class];
 
321
  NSFloatNumberClass = [NSFloatNumber class];
 
322
  NSDoubleNumberClass = [NSDoubleNumber class];
 
323
 
 
324
  for (i = 0; i < 14; i++)
 
325
    {
 
326
      NSIntNumber *n = NSAllocateObject (NSIntNumberClass, 0, 0);
 
327
 
 
328
      n->value = i - 1;
 
329
      ReusedInstances[i] = n;
 
330
    }
 
331
}
 
332
 
 
333
- (const char *) objCType
 
334
{
 
335
  /* All concrete NSNumber types must implement this so we know which oen
 
336
   * they are.
 
337
   */
 
338
  [self subclassResponsibility: _cmd];
 
339
  return NULL;                  // Not reached
 
340
}
 
341
 
 
342
- (BOOL) isEqualToNumber: (NSNumber*)aNumber
 
343
{
 
344
  return ([self compare: aNumber] == NSOrderedSame) ? YES : NO;
 
345
}
 
346
 
 
347
- (BOOL) isEqual: (id)anObject
 
348
{
 
349
  if ([anObject isKindOfClass: NSNumberClass])
 
350
    {
 
351
      return [self isEqualToNumber: anObject];
 
352
    }
 
353
  return [super isEqual: anObject];
 
354
}
 
355
 
 
356
- (BOOL) isEqualToValue: (NSValue*)aValue
 
357
{
 
358
  if ([aValue isKindOfClass: NSNumberClass])
 
359
    {
 
360
      return [self isEqualToNumber: (NSNumber*)aValue];
 
361
    }
 
362
  return NO;
 
363
}
 
364
 
 
365
- (unsigned) hash
 
366
{
 
367
  return (unsigned)[self doubleValue];
 
368
}
 
369
 
 
370
- (NSString*) stringValue
 
371
{
 
372
  return [self descriptionWithLocale: nil];
 
373
}
 
374
 
 
375
- (NSString*) descriptionWithLocale: (id)aLocale
 
376
{
 
377
  [self subclassResponsibility: _cmd];
 
378
  return nil;                   // Not reached
 
379
}
 
380
 
 
381
- (NSComparisonResult) compare: (NSNumber*)aNumber
 
382
{
 
383
  [self subclassResponsibility: _cmd];
 
384
  return 0;                     // Not reached
 
385
}
 
386
 
 
387
#define INTEGER_MACRO(type, ignored, name) \
 
388
- (id) initWith ## name: (type)aValue \
 
389
{\
 
390
  DESTROY(self);\
 
391
  return [[NSNumberClass numberWith ## name: aValue] retain];\
 
392
}
 
393
 
 
394
#include "GSNumberTypes.h"
 
395
 
 
396
- (id) initWithBool: (BOOL)aValue
 
397
{
 
398
  DESTROY(self);
 
399
  return [ReusedInstances[aValue ? 2 : 1] retain];\
 
400
}
 
401
 
 
402
/*
 
403
 * Macro for checking whether this value is the same as one of the singleton
 
404
 * instances.  
 
405
 */
 
406
#define CHECK_SINGLETON(aValue) \
 
407
if (aValue >= -1 && aValue <= 12)\
 
408
{\
 
409
  return ReusedInstances[aValue+1];\
 
410
}
 
411
 
 
412
+ (NSNumber *) numberWithBool: (BOOL)aValue
 
413
{
 
414
  CHECK_SINGLETON (((signed char) aValue));
 
415
  return [self numberWithInt: aValue];
 
416
  // Not reached (BOOL is always 0 or 1)
 
417
}
 
418
 
 
419
+ (NSNumber *) numberWithChar: (signed char)aValue
 
420
{
 
421
  return [self numberWithInt: aValue];
 
422
}
 
423
 
 
424
+ (NSNumber *) numberWithUnsignedChar: (unsigned char)aValue
 
425
{
 
426
  return [self numberWithInt: aValue];
 
427
}
 
428
 
 
429
+ (NSNumber *) numberWithShort: (short)aValue
 
430
{
 
431
  return [self numberWithInt: aValue];
 
432
}
 
433
 
 
434
+ (NSNumber *) numberWithUnsignedShort: (unsigned short)aValue
 
435
{
 
436
  return [self numberWithInt: aValue];
 
437
}
 
438
 
 
439
+ (NSNumber *) numberWithInt: (int)aValue
 
440
{
 
441
  NSIntNumber *n;
 
442
 
 
443
  CHECK_SINGLETON (aValue);
 
444
  n = NSAllocateObject (NSIntNumberClass, 0, 0);
 
445
  n->value = aValue;
 
446
  return AUTORELEASE(n);
 
447
}
 
448
 
 
449
+ (NSNumber *) numberWithUnsignedInt: (unsigned int)aValue
 
450
{
 
451
  CHECK_SINGLETON (aValue);
 
452
 
 
453
  if (aValue < (unsigned int) INT_MAX)
 
454
    {
 
455
      return [self numberWithInt: (int)aValue];
 
456
    }
 
457
  return [self numberWithLongLong: aValue];
 
458
}
 
459
 
 
460
+ (NSNumber *) numberWithLong: (long)aValue
 
461
{
 
462
  return [self numberWithLongLong: aValue];
 
463
}
 
464
 
 
465
+ (NSNumber *) numberWithUnsignedLong: (unsigned long)aValue
 
466
{
 
467
  return [self numberWithUnsignedLongLong: aValue];
 
468
}
 
469
 
 
470
+ (NSNumber *) numberWithLongLong: (long long)aValue
 
471
{
 
472
  NSLongLongNumber *n;
 
473
 
 
474
  CHECK_SINGLETON (aValue);
 
475
  if (aValue < (long long)INT_MAX && aValue > (long long)INT_MIN)
 
476
    {
 
477
      return [self numberWithInt: (int) aValue];
 
478
    }
 
479
  n = NSAllocateObject (NSLongLongNumberClass, 0, 0);
 
480
  n->value = aValue;
 
481
  return AUTORELEASE(n);
 
482
}
 
483
 
 
484
+ (NSNumber *) numberWithUnsignedLongLong: (unsigned long long)aValue
 
485
{
 
486
  NSUnsignedLongLongNumber *n;
 
487
 
 
488
  if (aValue < (unsigned long long) LLONG_MAX)
 
489
    {
 
490
      return [self numberWithLongLong: (long long) aValue];
 
491
    }
 
492
  n = NSAllocateObject (NSUnsignedLongLongNumberClass, 0, 0);
 
493
  n->value = aValue;
 
494
  return AUTORELEASE(n);
 
495
}
 
496
 
 
497
+ (NSNumber *) numberWithFloat: (float)aValue
 
498
{
 
499
  NSFloatNumber *n = NSAllocateObject (NSFloatNumberClass, 0, 0);
 
500
 
 
501
  n->value = aValue;
 
502
  return AUTORELEASE(n);
 
503
}
 
504
 
 
505
+ (NSNumber *) numberWithDouble: (double)aValue
 
506
{
 
507
  NSDoubleNumber *n = NSAllocateObject (NSDoubleNumberClass, 0, 0);
 
508
 
 
509
  n->value = aValue;
 
510
  return AUTORELEASE(n);
 
511
}
 
512
 
 
513
+ (NSNumber *) numberWithInteger: (NSInteger)aValue
 
514
{
 
515
  // Compile time constant; the compiler will remove this conditional
 
516
  if (sizeof (NSInteger) == sizeof (int))
 
517
    {
 
518
      return [self numberWithInt: aValue];
 
519
    }
 
520
  return [self numberWithLongLong: aValue];
 
521
}
 
522
 
 
523
+ (NSNumber *) numberWithUnsignedInteger: (NSUInteger)aValue
 
524
{
 
525
  // Compile time constant; the compiler will remove this conditional
 
526
  if (sizeof (NSUInteger) == sizeof (unsigned int))
 
527
    {
 
528
      return [self numberWithUnsignedInt: aValue];
 
529
    }
 
530
  return [self numberWithUnsignedLongLong: aValue];
 
531
}
 
532
 
 
533
- (id) initWithBytes: (const void *)
 
534
      value objCType: (const char *)type
 
535
{
 
536
  switch (type[0])
 
537
    {
 
538
      case 'c':
 
539
        return [self initWithInteger: *(char *) value];
 
540
      case 'C':
 
541
        return [self initWithInteger: *(unsigned char *) value];
 
542
      case 's':
 
543
        return [self initWithInteger: *(short *) value];
 
544
      case 'S':
 
545
        return [self initWithInteger: *(unsigned short *) value];
 
546
      case 'i':
 
547
        return [self initWithInteger: *(int *) value];
 
548
      case 'I':
 
549
        return [self initWithInteger: *(unsigned int *) value];
 
550
      case 'l':
 
551
        return [self initWithLong: *(long *) value];
 
552
      case 'L':
 
553
        return [self initWithUnsignedLong: *(unsigned long *) value];
303
554
      case 'q':
304
 
#endif
305
 
        return longLongNumberClass;
306
 
#ifdef  _C_ULNGLNG
307
 
      case _C_ULNGLNG:
308
 
#else
 
555
        return [self initWithLongLong: *(long long *) value];
309
556
      case 'Q':
310
 
#endif
311
 
        return uLongLongNumberClass;
312
 
      case _C_FLT:      return floatNumberClass;
313
 
      case _C_DBL:      return doubleNumberClass;
314
 
      default:
315
 
        break;
316
 
    }
317
 
 
318
 
  if (theClass == Nil && self == abstractClass)
319
 
    {
320
 
      [NSException raise: NSInvalidArgumentException
321
 
                  format: @"Invalid number type"];
322
 
        /* NOT REACHED */
323
 
    }
324
 
  else if (theClass == Nil)
325
 
    {
326
 
      theClass = [super valueClassWithObjCType: type];
327
 
    }
328
 
  return theClass;
329
 
}
330
 
 
331
 
+ (NSNumber*) numberWithBool: (BOOL)value
332
 
{
333
 
  // if class is NSNumber, replace by appropriate object
334
 
  if (self == abstractClass)
335
 
    {
336
 
      if (value == NO)
337
 
        {
338
 
          return boolN;
339
 
        }
340
 
      else
341
 
        {
342
 
          return boolY;
343
 
        }
344
 
    }
345
 
  else // alloc class and init with object intWithXX method
346
 
    {
347
 
      return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
348
 
                           initWithBool: value]);
349
 
    }
350
 
}
351
 
 
352
 
+ (NSNumber*) numberWithChar: (signed char)value
353
 
{
354
 
  NSNumber      *theObj = nil;
355
 
 
356
 
  // if class is NSNumber, replace by appropriate object
357
 
  if (self == abstractClass)
358
 
    {
359
 
      if (value <= GS_SMALL && value >= -GS_SMALL)
360
 
        {
361
 
          return smallIntegers[value + GS_SMALL];
362
 
        }
363
 
      theObj = (NSNumber*)NSAllocateObject(charNumberClass, 0,
364
 
                                           NSDefaultMallocZone());
365
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
366
 
    }
367
 
  else // alloc class and init with object intWithXX method
368
 
    {
369
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
370
 
                 initWithChar: value];
371
 
    }
372
 
 
373
 
  return AUTORELEASE(theObj);
374
 
}
375
 
 
376
 
+ (NSNumber*) numberWithDouble: (double)value
377
 
{
378
 
  NSNumber      *theObj = nil;
379
 
 
380
 
  // if class is NSNumber, replace by appropriate object
381
 
  if (self == abstractClass)
382
 
    {
383
 
      theObj = (NSNumber*)NSAllocateObject(doubleNumberClass, 0,
384
 
                                           NSDefaultMallocZone());
385
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
386
 
    }
387
 
  else // alloc class and init with object intWithXX method
388
 
    {
389
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
390
 
                 initWithDouble: value];
391
 
    }
392
 
 
393
 
  return AUTORELEASE(theObj);
394
 
}
395
 
 
396
 
+ (NSNumber*) numberWithFloat: (float)value
397
 
{
398
 
  NSNumber      *theObj = nil;
399
 
 
400
 
  // if class is NSNumber, replace by appropriate object
401
 
  if (self == abstractClass)
402
 
    {
403
 
      theObj = (NSNumber*)NSAllocateObject(floatNumberClass, 0,
404
 
                                           NSDefaultMallocZone());
405
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
406
 
    }
407
 
  else // alloc class and init with object intWithXX method
408
 
    {
409
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
410
 
                 initWithFloat: value];
411
 
    }
412
 
 
413
 
  return AUTORELEASE(theObj);
414
 
}
415
 
 
416
 
+ (NSNumber*) numberWithInt: (signed int)value
417
 
{
418
 
  NSNumber      *theObj = nil;
419
 
 
420
 
  // if class is NSNumber, replace by appropriate object
421
 
  if (self == abstractClass)
422
 
    {
423
 
      if (value <= GS_SMALL && value >= -GS_SMALL)
424
 
        {
425
 
          return smallIntegers[value + GS_SMALL];
426
 
        }
427
 
      theObj = (NSNumber*)NSAllocateObject(intNumberClass, 0,
428
 
                                           NSDefaultMallocZone());
429
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
430
 
    }
431
 
  else // alloc class and init with object intWithXX method
432
 
    {
433
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
434
 
                 initWithInt: value];
435
 
    }
436
 
 
437
 
  return AUTORELEASE(theObj);
438
 
}
439
 
 
440
 
+ (NSNumber*) numberWithLong: (signed long)value
441
 
{
442
 
  NSNumber      *theObj = nil;
443
 
 
444
 
  // if class is NSNumber, replace by appropriate object
445
 
  if (self == abstractClass)
446
 
    {
447
 
      if (value <= GS_SMALL && value >= -GS_SMALL)
448
 
        {
449
 
          return smallIntegers[value + GS_SMALL];
450
 
        }
451
 
      theObj = (NSNumber*)NSAllocateObject(longNumberClass, 0,
452
 
                                           NSDefaultMallocZone());
453
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
454
 
    }
455
 
  else // alloc class and init with object intWithXX method
456
 
    {
457
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
458
 
                 initWithLong: value];
459
 
    }
460
 
 
461
 
  return AUTORELEASE(theObj);
462
 
}
463
 
 
464
 
+ (NSNumber*) numberWithLongLong: (signed long long)value
465
 
{
466
 
  NSNumber      *theObj = nil;
467
 
 
468
 
  // if class is NSNumber, replace by appropriate object
469
 
  if (self == abstractClass)
470
 
    {
471
 
      if (value <= GS_SMALL && value >= -GS_SMALL)
472
 
        {
473
 
          return smallIntegers[value + GS_SMALL];
474
 
        }
475
 
      theObj = (NSNumber*)NSAllocateObject(longLongNumberClass, 0,
476
 
                                           NSDefaultMallocZone());
477
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
478
 
    }
479
 
  else // alloc class and init with object intWithXX method
480
 
    {
481
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
482
 
                 initWithLongLong: value];
483
 
    }
484
 
 
485
 
  return AUTORELEASE(theObj);
486
 
}
487
 
 
488
 
+ (NSNumber*) numberWithShort: (signed short)value
489
 
{
490
 
  NSNumber      *theObj = nil;
491
 
 
492
 
  // if class is NSNumber, replace by appropriate object
493
 
  if (self == abstractClass)
494
 
    {
495
 
      if (value <= GS_SMALL && value >= -GS_SMALL)
496
 
        {
497
 
          return smallIntegers[value + GS_SMALL];
498
 
        }
499
 
      theObj = (NSNumber*)NSAllocateObject(shortNumberClass, 0,
500
 
                                           NSDefaultMallocZone());
501
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
502
 
    }
503
 
  else // alloc class and init with object intWithXX method
504
 
    {
505
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
506
 
                 initWithShort: value];
507
 
    }
508
 
 
509
 
  return AUTORELEASE(theObj);
510
 
}
511
 
 
512
 
+ (NSNumber*) numberWithUnsignedChar: (unsigned char)value
513
 
{
514
 
  NSNumber      *theObj = nil;
515
 
 
516
 
  // if class is NSNumber, replace by appropriate object
517
 
  if (self == abstractClass)
518
 
    {
519
 
      if (value <= GS_SMALL)
520
 
        {
521
 
          return smallIntegers[value + GS_SMALL];
522
 
        }
523
 
      theObj = (NSNumber*)NSAllocateObject(uCharNumberClass, 0,
524
 
                                           NSDefaultMallocZone());
525
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
526
 
    }
527
 
  else // alloc class and init with object intWithXX method
528
 
    {
529
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
530
 
                 initWithUnsignedChar: value];
531
 
    }
532
 
 
533
 
  return AUTORELEASE(theObj);
534
 
}
535
 
 
536
 
+ (NSNumber*) numberWithUnsignedInt: (unsigned int)value
537
 
{
538
 
  NSNumber      *theObj = nil;
539
 
 
540
 
  // if class is NSNumber, replace by appropriate object
541
 
  if (self == abstractClass)
542
 
    {
543
 
      if (value <= GS_SMALL)
544
 
        {
545
 
          return smallIntegers[value + GS_SMALL];
546
 
        }
547
 
      theObj = (NSNumber*)NSAllocateObject(uIntNumberClass, 0,
548
 
                                           NSDefaultMallocZone());
549
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
550
 
    }
551
 
  else // alloc class and init with object intWithXX method
552
 
    {
553
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
554
 
                 initWithUnsignedInt: value];
555
 
    }
556
 
 
557
 
  return AUTORELEASE(theObj);
558
 
}
559
 
 
560
 
+ (NSNumber*) numberWithUnsignedLong: (unsigned long)value
561
 
{
562
 
  NSNumber      *theObj = nil;
563
 
 
564
 
  // if class is NSNumber, replace by appropriate object
565
 
  if (self == abstractClass)
566
 
    {
567
 
      if (value <= GS_SMALL)
568
 
        {
569
 
          return smallIntegers[value + GS_SMALL];
570
 
        }
571
 
      theObj = (NSNumber*)NSAllocateObject(uLongNumberClass, 0,
572
 
                                           NSDefaultMallocZone());
573
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
574
 
    }
575
 
  else // alloc class and init with object intWithXX method
576
 
    {
577
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
578
 
                 initWithUnsignedLong: value];
579
 
    }
580
 
 
581
 
  return AUTORELEASE(theObj);
582
 
}
583
 
 
584
 
+ (NSNumber*) numberWithUnsignedLongLong: (unsigned long long)value
585
 
{
586
 
  NSNumber      *theObj = nil;
587
 
 
588
 
  // if class is NSNumber, replace by appropriate object
589
 
  if (self == abstractClass)
590
 
    {
591
 
      if (value <= GS_SMALL)
592
 
        {
593
 
          return smallIntegers[value + GS_SMALL];
594
 
        }
595
 
      theObj = (NSNumber*)NSAllocateObject(uLongLongNumberClass, 0,
596
 
                                           NSDefaultMallocZone());
597
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
598
 
    }
599
 
  else // alloc class and init with object intWithXX method
600
 
    {
601
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
602
 
                 initWithUnsignedLongLong: value];
603
 
    }
604
 
 
605
 
  return AUTORELEASE(theObj);
606
 
}
607
 
 
608
 
+ (NSNumber*) numberWithUnsignedShort: (unsigned short)value
609
 
{
610
 
  NSNumber      *theObj = nil;
611
 
 
612
 
  // if class is NSNumber, replace by appropriate object
613
 
  if (self == abstractClass)
614
 
    {
615
 
      if (value <= GS_SMALL)
616
 
        {
617
 
          return smallIntegers[value + GS_SMALL];
618
 
        }
619
 
      theObj = (NSNumber*)NSAllocateObject(uShortNumberClass, 0,
620
 
                                           NSDefaultMallocZone());
621
 
      theObj = [theObj initWithBytes: &value objCType: NULL];
622
 
    }
623
 
  else // alloc class and init with object intWithXX method
624
 
    {
625
 
      theObj = [[self allocWithZone: NSDefaultMallocZone()]
626
 
                 initWithUnsignedShort: value];
627
 
    }
628
 
 
629
 
  return AUTORELEASE(theObj);
630
 
}
631
 
 
632
 
/*
633
 
 * A moderately sane default init method - a zero value integer.
 
557
        return [self initWithUnsignedLongLong: *(unsigned long long *) value];
 
558
      case 'f':
 
559
        return [self initWithFloat: *(float *) value];
 
560
      case 'd':
 
561
        return [self initWithDouble: *(double *) value];
 
562
    }
 
563
  return [super initWithBytes: value objCType: type];
 
564
}
 
565
 
 
566
- (void *) pointerValue
 
567
{
 
568
  return (void *)[self unsignedIntegerValue];
 
569
}
 
570
 
 
571
- (id) replacementObjectForPortCoder: (NSPortCoder *) encoder
 
572
{
 
573
  return self;
 
574
}
 
575
 
 
576
- (Class) classForCoder
 
577
{
 
578
  return NSNumberClass;
 
579
}
 
580
 
 
581
- (void) encodeWithCoder: (NSCoder *) coder
 
582
{
 
583
  const char *type = [self objCType];
 
584
  char buffer[16];
 
585
 
 
586
  [coder encodeValueOfObjCType: @encode (char) at: type];
 
587
  /* The most we currently store in an NSNumber is 8 bytes (double or long
 
588
   * long), but we may add support for vectors or long doubles in future, so
 
589
   * make this 16 bytes now so stuff doesn't break in fun and exciting ways
 
590
   * later.
 
591
   */
 
592
  [self getValue: buffer];
 
593
  [coder encodeValueOfObjCType: type at: buffer];
 
594
}
 
595
 
 
596
- (id) copyWithZone: (NSZone *) aZone
 
597
{
 
598
  // OSX just returns the receive with no copy.
 
599
  return RETAIN (self);
 
600
}
 
601
 
 
602
- (id) initWithCoder: (NSCoder *) coder
 
603
{
 
604
  char type[2] = { 0 };
 
605
  char buffer[16];
 
606
 
 
607
  [coder decodeValueOfObjCType: @encode (char) at: type];
 
608
  [coder decodeValueOfObjCType: type at: buffer];
 
609
  return [self initWithBytes: buffer objCType: type];
 
610
}
 
611
 
 
612
- (NSString *) description
 
613
{
 
614
  return [self stringValue];
 
615
}
 
616
 
 
617
/* Return nil for an NSNumber that is allocated and initalized without
 
618
 * providing a real value.  Yes, this seems weird, but it is actually what
 
619
 * happens on OS X.
634
620
 */
635
621
- (id) init
636
622
{
637
 
  return [self initWithInt: 0];
638
 
}
639
 
 
640
 
- (id) initWithBool: (BOOL)value
641
 
{
642
 
  RELEASE(self);
643
 
  if (value == NO)
644
 
    {
645
 
      self = boolN;
646
 
    }
647
 
  else
648
 
    {
649
 
      self = boolY;
650
 
    }
651
 
  return RETAIN(self);
652
 
}
653
 
 
654
 
- (id) initWithChar: (signed char)value
655
 
{
656
 
  RELEASE(self);
657
 
  if (value <= GS_SMALL && value >= -GS_SMALL)
658
 
    {
659
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
660
 
    }
661
 
  self = (NSNumber*)NSAllocateObject(charNumberClass, 0,
662
 
    NSDefaultMallocZone());
663
 
  self = [self initWithBytes: &value objCType: NULL];
664
 
  return self;
665
 
}
666
 
 
667
 
- (id) initWithDouble: (double)value
668
 
{
669
 
  RELEASE(self);
670
 
  self = (NSNumber*)NSAllocateObject(doubleNumberClass, 0,
671
 
    NSDefaultMallocZone());
672
 
  self = [self initWithBytes: &value objCType: NULL];
673
 
  return self;
674
 
}
675
 
 
676
 
- (id) initWithFloat: (float)value
677
 
{
678
 
  RELEASE(self);
679
 
  self = (NSNumber*)NSAllocateObject(floatNumberClass, 0,
680
 
    NSDefaultMallocZone());
681
 
  self = [self initWithBytes: &value objCType: NULL];
682
 
  return self;
683
 
}
684
 
 
685
 
- (id) initWithInt: (signed int)value
686
 
{
687
 
  RELEASE(self);
688
 
  if (value <= GS_SMALL && value >= -GS_SMALL)
689
 
    {
690
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
691
 
    }
692
 
  self = (NSNumber*)NSAllocateObject(intNumberClass, 0,
693
 
    NSDefaultMallocZone());
694
 
  self = [self initWithBytes: &value objCType: NULL];
695
 
  return self;
696
 
}
697
 
 
698
 
- (id) initWithLong: (signed long)value
699
 
{
700
 
  RELEASE(self);
701
 
  if (value <= GS_SMALL && value >= -GS_SMALL)
702
 
    {
703
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
704
 
    }
705
 
  self = (NSNumber*)NSAllocateObject(longNumberClass, 0,
706
 
    NSDefaultMallocZone());
707
 
  self = [self initWithBytes: &value objCType: NULL];
708
 
  return self;
709
 
}
710
 
 
711
 
- (id) initWithLongLong: (signed long long)value
712
 
{
713
 
  RELEASE(self);
714
 
  if (value <= GS_SMALL && value >= -GS_SMALL)
715
 
    {
716
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
717
 
    }
718
 
  self = (NSNumber*)NSAllocateObject(longLongNumberClass, 0,
719
 
    NSDefaultMallocZone());
720
 
  self = [self initWithBytes: &value objCType: NULL];
721
 
  return self;
722
 
}
723
 
 
724
 
- (id) initWithShort: (signed short)value
725
 
{
726
 
  RELEASE(self);
727
 
  if (value <= GS_SMALL && value >= -GS_SMALL)
728
 
    {
729
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
730
 
    }
731
 
  self = (NSNumber*)NSAllocateObject(shortNumberClass, 0,
732
 
    NSDefaultMallocZone());
733
 
  self = [self initWithBytes: &value objCType: NULL];
734
 
  return self;
735
 
}
736
 
 
737
 
- (id) initWithUnsignedChar: (unsigned char)value
738
 
{
739
 
  RELEASE(self);
740
 
  if (value <= GS_SMALL)
741
 
    {
742
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
743
 
    }
744
 
  self = (NSNumber*)NSAllocateObject(uCharNumberClass, 0,
745
 
    NSDefaultMallocZone());
746
 
  self = [self initWithBytes: &value objCType: NULL];
747
 
  return self;
748
 
}
749
 
 
750
 
- (id) initWithUnsignedInt: (unsigned int)value
751
 
{
752
 
  RELEASE(self);
753
 
  if (value <= GS_SMALL)
754
 
    {
755
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
756
 
    }
757
 
  self = (NSNumber*)NSAllocateObject(uIntNumberClass, 0,
758
 
    NSDefaultMallocZone());
759
 
  self = [self initWithBytes: &value objCType: NULL];
760
 
  return self;
761
 
}
762
 
 
763
 
- (id) initWithUnsignedLong: (unsigned long)value
764
 
{
765
 
  RELEASE(self);
766
 
  if (value <= GS_SMALL)
767
 
    {
768
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
769
 
    }
770
 
  self = (NSNumber*)NSAllocateObject(uLongNumberClass, 0,
771
 
    NSDefaultMallocZone());
772
 
  self = [self initWithBytes: &value objCType: NULL];
773
 
  return self;
774
 
}
775
 
 
776
 
- (id) initWithUnsignedLongLong: (unsigned long long)value
777
 
{
778
 
  RELEASE(self);
779
 
  if (value <= GS_SMALL)
780
 
    {
781
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
782
 
    }
783
 
  self = (NSNumber*)NSAllocateObject(uLongLongNumberClass, 0,
784
 
    NSDefaultMallocZone());
785
 
  self = [self initWithBytes: &value objCType: NULL];
786
 
  return self;
787
 
}
788
 
 
789
 
- (id) initWithUnsignedShort: (unsigned short)value
790
 
{
791
 
  RELEASE(self);
792
 
  if (value <= GS_SMALL)
793
 
    {
794
 
      return RETAIN(smallIntegers[value + GS_SMALL]);
795
 
    }
796
 
  self = (NSNumber*)NSAllocateObject(uShortNumberClass, 0,
797
 
    NSDefaultMallocZone());
798
 
  self = [self initWithBytes: &value objCType: NULL];
799
 
  return self;
800
 
}
801
 
 
802
 
- (id) copyWithZone: (NSZone*)zone
803
 
{
804
 
  if (NSShouldRetainWithZone(self, zone))
805
 
    return RETAIN(self);
806
 
  else
807
 
    return NSCopyObject(self, 0, zone);
808
 
}
809
 
 
810
 
- (NSString*) description
811
 
{
812
 
  return [self descriptionWithLocale: nil];
813
 
}
814
 
 
815
 
- (NSString*) descriptionWithLocale: (NSDictionary*)locale
816
 
{
817
 
  NSString      *result = nil;
818
 
 
819
 
  if (GSObjCClass(self) == abstractClass)
820
 
    {
821
 
      [NSException raise: NSInternalInconsistencyException
822
 
                  format: @"descriptionWithLocale: for abstract NSNumber"];
823
 
    }
824
 
  else
825
 
    {
826
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
827
 
 
828
 
      switch (info->typeLevel)
829
 
        {
830
 
          case 0:
831
 
            return [self boolValue] ? @"1" : @"0";
832
 
            break;
833
 
 
834
 
          case 1:
835
 
            result = [[NSString alloc] initWithFormat: @"%i" locale: locale,
836
 
              (int)[self charValue]];
837
 
            break;
838
 
 
839
 
          case 2:
840
 
            result = [[NSString alloc] initWithFormat: @"%u" locale: locale,
841
 
              (unsigned int)[self unsignedCharValue]];
842
 
            break;
843
 
 
844
 
          case 3:
845
 
            result = [[NSString alloc] initWithFormat: @"%hi" locale: locale,
846
 
              [self shortValue]];
847
 
            break;
848
 
 
849
 
          case 4:
850
 
            result = [[NSString alloc] initWithFormat: @"%hu" locale: locale,
851
 
              [self unsignedShortValue]];
852
 
            break;
853
 
 
854
 
          case 5:
855
 
            result = [[NSString alloc] initWithFormat: @"%i" locale: locale,
856
 
              [self intValue]];
857
 
            break;
858
 
 
859
 
          case 6:
860
 
            result = [[NSString alloc] initWithFormat: @"%u" locale: locale,
861
 
              [self unsignedIntValue]];
862
 
            break;
863
 
 
864
 
          case 7:
865
 
            result = [[NSString alloc] initWithFormat: @"%li" locale: locale,
866
 
              [self longValue]];
867
 
            break;
868
 
 
869
 
          case 8:
870
 
            result = [[NSString alloc] initWithFormat: @"%lu" locale: locale,
871
 
              [self unsignedLongValue]];
872
 
            break;
873
 
 
874
 
          case 9:
875
 
            result = [[NSString alloc] initWithFormat: @"%lli" locale: locale,
876
 
              [self longLongValue]];
877
 
            break;
878
 
 
879
 
          case 10:
880
 
            result = [[NSString alloc] initWithFormat: @"%llu" locale: locale,
881
 
              [self unsignedLongLongValue]];
882
 
            break;
883
 
 
884
 
          case 11:
885
 
            result = [[NSString alloc] initWithFormat: @"%0.7g" locale: locale,
886
 
              (double)[self floatValue]];
887
 
            break;
888
 
 
889
 
          case 12:
890
 
            result = [[NSString alloc] initWithFormat: @"%0.16g" locale: locale,
891
 
              [self doubleValue]];
892
 
            break;
893
 
 
894
 
          default:
895
 
            [NSException raise: NSInvalidArgumentException
896
 
                        format: @"unknown number type value for description"];
897
 
        }
898
 
    }
899
 
  return AUTORELEASE(result);
900
 
}
901
 
 
902
 
/* All the rest of these methods must be implemented by a subclass */
 
623
  DESTROY(self);
 
624
  return nil;
 
625
}
 
626
 
 
627
/* Stop the compiler complaining about unimplemented methods.  Throwing an
 
628
 * exception here matches OS X behaviour, although they throw an invalid
 
629
 * argument exception.
 
630
 */
 
631
#define INTEGER_MACRO(type, name, ignored) \
 
632
- (type) name ## Value\
 
633
{\
 
634
  [self subclassResponsibility: _cmd];\
 
635
  return (type)0;\
 
636
}
 
637
 
 
638
#include "GSNumberTypes.h"
 
639
 
903
640
- (BOOL) boolValue
904
641
{
905
 
  if (GSObjCClass(self) == abstractClass)
906
 
    [NSException raise: NSInternalInconsistencyException
907
 
                format: @"get boolValue from abstract NSNumber"];
908
 
  else
909
 
    {
910
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
911
 
 
912
 
      switch (info->typeLevel)
913
 
        {
914
 
          case 0:
915
 
            {
916
 
              BOOL      oData;
917
 
 
918
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
919
 
              return (oData == 0) ? NO : YES;
920
 
            }
921
 
          case 1:
922
 
            {
923
 
              signed char       oData;
924
 
 
925
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
926
 
              return (oData == 0) ? NO : YES;
927
 
            }
928
 
          case 2:
929
 
            {
930
 
              unsigned char     oData;
931
 
 
932
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
933
 
              return (oData == 0) ? NO : YES;
934
 
            }
935
 
          case 3:
936
 
            {
937
 
              signed short      oData;
938
 
 
939
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
940
 
              return (oData == 0) ? NO : YES;
941
 
            }
942
 
          case 4:
943
 
            {
944
 
              unsigned short    oData;
945
 
 
946
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
947
 
              return (oData == 0) ? NO : YES;
948
 
            }
949
 
          case 5:
950
 
            {
951
 
              signed int        oData;
952
 
 
953
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
954
 
              return (oData == 0) ? NO : YES;
955
 
            }
956
 
          case 6:
957
 
            {
958
 
              unsigned int      oData;
959
 
 
960
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
961
 
              return (oData == 0) ? NO : YES;
962
 
            }
963
 
          case 7:
964
 
            {
965
 
              signed long       oData;
966
 
 
967
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
968
 
              return (oData == 0) ? NO : YES;
969
 
            }
970
 
          case 8:
971
 
            {
972
 
              unsigned long     oData;
973
 
 
974
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
975
 
              return (oData == 0) ? NO : YES;
976
 
            }
977
 
          case 9:
978
 
            {
979
 
              signed long long  oData;
980
 
 
981
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
982
 
              return (oData == 0) ? NO : YES;
983
 
            }
984
 
          case 10:
985
 
            {
986
 
              unsigned long long        oData;
987
 
 
988
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
989
 
              return (oData == 0) ? NO : YES;
990
 
            }
991
 
          case 11:
992
 
            {
993
 
              float     oData;
994
 
 
995
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
996
 
              return (oData == 0) ? NO : YES;
997
 
            }
998
 
          case 12:
999
 
            {
1000
 
              double    oData;
1001
 
 
1002
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1003
 
              return (oData == 0) ? NO : YES;
1004
 
            }
1005
 
          default:
1006
 
            [NSException raise: NSInvalidArgumentException
1007
 
                        format: @"unknown number type value for get"];
1008
 
        }
1009
 
    }
1010
 
  return NO;
1011
 
}
1012
 
 
1013
 
- (signed char) charValue
1014
 
{
1015
 
  if (GSObjCClass(self) == abstractClass)
1016
 
    [NSException raise: NSInternalInconsistencyException
1017
 
                format: @"get charValue from abstract NSNumber"];
1018
 
  else
1019
 
    {
1020
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1021
 
 
1022
 
      switch (info->typeLevel)
1023
 
        {
1024
 
          case 0:
1025
 
            {
1026
 
              BOOL      oData;
1027
 
 
1028
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1029
 
              return oData;
1030
 
            }
1031
 
          case 1:
1032
 
            {
1033
 
              signed char       oData;
1034
 
 
1035
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1036
 
              return oData;
1037
 
            }
1038
 
          case 2:
1039
 
            {
1040
 
              unsigned char     oData;
1041
 
 
1042
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1043
 
              return oData;
1044
 
            }
1045
 
          case 3:
1046
 
            {
1047
 
              signed short      oData;
1048
 
 
1049
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1050
 
              return oData;
1051
 
            }
1052
 
          case 4:
1053
 
            {
1054
 
              unsigned short    oData;
1055
 
 
1056
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1057
 
              return oData;
1058
 
            }
1059
 
          case 5:
1060
 
            {
1061
 
              signed int        oData;
1062
 
 
1063
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1064
 
              return oData;
1065
 
            }
1066
 
          case 6:
1067
 
            {
1068
 
              unsigned int      oData;
1069
 
 
1070
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1071
 
              return oData;
1072
 
            }
1073
 
          case 7:
1074
 
            {
1075
 
              signed long       oData;
1076
 
 
1077
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1078
 
              return oData;
1079
 
            }
1080
 
          case 8:
1081
 
            {
1082
 
              unsigned long     oData;
1083
 
 
1084
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1085
 
              return oData;
1086
 
            }
1087
 
          case 9:
1088
 
            {
1089
 
              signed long long  oData;
1090
 
 
1091
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1092
 
              return oData;
1093
 
            }
1094
 
          case 10:
1095
 
            {
1096
 
              unsigned long long        oData;
1097
 
 
1098
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1099
 
              return oData;
1100
 
            }
1101
 
          case 11:
1102
 
            {
1103
 
              float     oData;
1104
 
 
1105
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1106
 
              return oData;
1107
 
            }
1108
 
          case 12:
1109
 
            {
1110
 
              double    oData;
1111
 
 
1112
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1113
 
              return oData;
1114
 
            }
1115
 
          default:
1116
 
            [NSException raise: NSInvalidArgumentException
1117
 
                        format: @"unknown number type value for get"];
1118
 
        }
1119
 
    }
1120
 
  return 0;
1121
 
}
1122
 
 
1123
 
- (double) doubleValue
1124
 
{
1125
 
  if (GSObjCClass(self) == abstractClass)
1126
 
    [NSException raise: NSInternalInconsistencyException
1127
 
                format: @"get doubleValue from abstract NSNumber"];
1128
 
  else
1129
 
    {
1130
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1131
 
 
1132
 
      switch (info->typeLevel)
1133
 
        {
1134
 
          case 0:
1135
 
            {
1136
 
              BOOL      oData;
1137
 
 
1138
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1139
 
              return oData;
1140
 
            }
1141
 
          case 1:
1142
 
            {
1143
 
              signed char       oData;
1144
 
 
1145
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1146
 
              return oData;
1147
 
            }
1148
 
          case 2:
1149
 
            {
1150
 
              unsigned char     oData;
1151
 
 
1152
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1153
 
              return oData;
1154
 
            }
1155
 
          case 3:
1156
 
            {
1157
 
              signed short      oData;
1158
 
 
1159
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1160
 
              return oData;
1161
 
            }
1162
 
          case 4:
1163
 
            {
1164
 
              unsigned short    oData;
1165
 
 
1166
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1167
 
              return oData;
1168
 
            }
1169
 
          case 5:
1170
 
            {
1171
 
              signed int        oData;
1172
 
 
1173
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1174
 
              return oData;
1175
 
            }
1176
 
          case 6:
1177
 
            {
1178
 
              unsigned int      oData;
1179
 
 
1180
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1181
 
              return oData;
1182
 
            }
1183
 
          case 7:
1184
 
            {
1185
 
              signed long       oData;
1186
 
 
1187
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1188
 
              return oData;
1189
 
            }
1190
 
          case 8:
1191
 
            {
1192
 
              unsigned long     oData;
1193
 
 
1194
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1195
 
              return oData;
1196
 
            }
1197
 
          case 9:
1198
 
            {
1199
 
              signed long long  oData;
1200
 
 
1201
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1202
 
              return oData;
1203
 
            }
1204
 
          case 10:
1205
 
            {
1206
 
              unsigned long long        oData;
1207
 
 
1208
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1209
 
              return oData;
1210
 
            }
1211
 
          case 11:
1212
 
            {
1213
 
              float     oData;
1214
 
 
1215
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1216
 
              return oData;
1217
 
            }
1218
 
          case 12:
1219
 
            {
1220
 
              double    oData;
1221
 
 
1222
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1223
 
              return oData;
1224
 
            }
1225
 
          default:
1226
 
            [NSException raise: NSInvalidArgumentException
1227
 
                        format: @"unknown number type value for get"];
1228
 
        }
1229
 
    }
1230
 
  return 0;
1231
 
}
1232
 
 
1233
 
- (float) floatValue
1234
 
{
1235
 
  if (GSObjCClass(self) == abstractClass)
1236
 
    [NSException raise: NSInternalInconsistencyException
1237
 
                format: @"get floatValue from abstract NSNumber"];
1238
 
  else
1239
 
    {
1240
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1241
 
 
1242
 
      switch (info->typeLevel)
1243
 
        {
1244
 
          case 0:
1245
 
            {
1246
 
              BOOL      oData;
1247
 
 
1248
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1249
 
              return oData;
1250
 
            }
1251
 
          case 1:
1252
 
            {
1253
 
              signed char       oData;
1254
 
 
1255
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1256
 
              return oData;
1257
 
            }
1258
 
          case 2:
1259
 
            {
1260
 
              unsigned char     oData;
1261
 
 
1262
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1263
 
              return oData;
1264
 
            }
1265
 
          case 3:
1266
 
            {
1267
 
              signed short      oData;
1268
 
 
1269
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1270
 
              return oData;
1271
 
            }
1272
 
          case 4:
1273
 
            {
1274
 
              unsigned short    oData;
1275
 
 
1276
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1277
 
              return oData;
1278
 
            }
1279
 
          case 5:
1280
 
            {
1281
 
              signed int        oData;
1282
 
 
1283
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1284
 
              return oData;
1285
 
            }
1286
 
          case 6:
1287
 
            {
1288
 
              unsigned int      oData;
1289
 
 
1290
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1291
 
              return oData;
1292
 
            }
1293
 
          case 7:
1294
 
            {
1295
 
              signed long       oData;
1296
 
 
1297
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1298
 
              return oData;
1299
 
            }
1300
 
          case 8:
1301
 
            {
1302
 
              unsigned long     oData;
1303
 
 
1304
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1305
 
              return oData;
1306
 
            }
1307
 
          case 9:
1308
 
            {
1309
 
              signed long long  oData;
1310
 
 
1311
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1312
 
              return oData;
1313
 
            }
1314
 
          case 10:
1315
 
            {
1316
 
              unsigned long long        oData;
1317
 
 
1318
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1319
 
              return oData;
1320
 
            }
1321
 
          case 11:
1322
 
            {
1323
 
              float     oData;
1324
 
 
1325
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1326
 
              return oData;
1327
 
            }
1328
 
          case 12:
1329
 
            {
1330
 
              double    oData;
1331
 
 
1332
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1333
 
              return oData;
1334
 
            }
1335
 
          default:
1336
 
            [NSException raise: NSInvalidArgumentException
1337
 
                        format: @"unknown number type value for get"];
1338
 
        }
1339
 
    }
1340
 
  return 0;
1341
 
}
1342
 
 
1343
 
- (signed int) intValue
1344
 
{
1345
 
  if (GSObjCClass(self) == abstractClass)
1346
 
    [NSException raise: NSInternalInconsistencyException
1347
 
                format: @"get intValue from abstract NSNumber"];
1348
 
  else
1349
 
    {
1350
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1351
 
 
1352
 
      switch (info->typeLevel)
1353
 
        {
1354
 
          case 0:
1355
 
            {
1356
 
              BOOL      oData;
1357
 
 
1358
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1359
 
              return oData;
1360
 
            }
1361
 
          case 1:
1362
 
            {
1363
 
              signed char       oData;
1364
 
 
1365
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1366
 
              return oData;
1367
 
            }
1368
 
          case 2:
1369
 
            {
1370
 
              unsigned char     oData;
1371
 
 
1372
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1373
 
              return oData;
1374
 
            }
1375
 
          case 3:
1376
 
            {
1377
 
              signed short      oData;
1378
 
 
1379
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1380
 
              return oData;
1381
 
            }
1382
 
          case 4:
1383
 
            {
1384
 
              unsigned short    oData;
1385
 
 
1386
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1387
 
              return oData;
1388
 
            }
1389
 
          case 5:
1390
 
            {
1391
 
              signed int        oData;
1392
 
 
1393
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1394
 
              return oData;
1395
 
            }
1396
 
          case 6:
1397
 
            {
1398
 
              unsigned int      oData;
1399
 
 
1400
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1401
 
              return oData;
1402
 
            }
1403
 
          case 7:
1404
 
            {
1405
 
              signed long       oData;
1406
 
 
1407
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1408
 
              return oData;
1409
 
            }
1410
 
          case 8:
1411
 
            {
1412
 
              unsigned long     oData;
1413
 
 
1414
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1415
 
              return oData;
1416
 
            }
1417
 
          case 9:
1418
 
            {
1419
 
              signed long long  oData;
1420
 
 
1421
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1422
 
              return oData;
1423
 
            }
1424
 
          case 10:
1425
 
            {
1426
 
              unsigned long long        oData;
1427
 
 
1428
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1429
 
              return oData;
1430
 
            }
1431
 
          case 11:
1432
 
            {
1433
 
              float     oData;
1434
 
 
1435
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1436
 
              return oData;
1437
 
            }
1438
 
          case 12:
1439
 
            {
1440
 
              double    oData;
1441
 
 
1442
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1443
 
              return oData;
1444
 
            }
1445
 
          default:
1446
 
            [NSException raise: NSInvalidArgumentException
1447
 
                        format: @"unknown number type value for get"];
1448
 
        }
1449
 
    }
1450
 
  return 0;
1451
 
}
1452
 
 
1453
 
- (signed long long) longLongValue
1454
 
{
1455
 
  if (GSObjCClass(self) == abstractClass)
1456
 
    [NSException raise: NSInternalInconsistencyException
1457
 
                format: @"get longLongValue from abstract NSNumber"];
1458
 
  else
1459
 
    {
1460
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1461
 
 
1462
 
      switch (info->typeLevel)
1463
 
        {
1464
 
          case 0:
1465
 
            {
1466
 
              BOOL      oData;
1467
 
 
1468
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1469
 
              return oData;
1470
 
            }
1471
 
          case 1:
1472
 
            {
1473
 
              signed char       oData;
1474
 
 
1475
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1476
 
              return oData;
1477
 
            }
1478
 
          case 2:
1479
 
            {
1480
 
              unsigned char     oData;
1481
 
 
1482
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1483
 
              return oData;
1484
 
            }
1485
 
          case 3:
1486
 
            {
1487
 
              signed short      oData;
1488
 
 
1489
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1490
 
              return oData;
1491
 
            }
1492
 
          case 4:
1493
 
            {
1494
 
              unsigned short    oData;
1495
 
 
1496
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1497
 
              return oData;
1498
 
            }
1499
 
          case 5:
1500
 
            {
1501
 
              signed int        oData;
1502
 
 
1503
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1504
 
              return oData;
1505
 
            }
1506
 
          case 6:
1507
 
            {
1508
 
              unsigned int      oData;
1509
 
 
1510
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1511
 
              return oData;
1512
 
            }
1513
 
          case 7:
1514
 
            {
1515
 
              signed long       oData;
1516
 
 
1517
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1518
 
              return oData;
1519
 
            }
1520
 
          case 8:
1521
 
            {
1522
 
              unsigned long     oData;
1523
 
 
1524
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1525
 
              return oData;
1526
 
            }
1527
 
          case 9:
1528
 
            {
1529
 
              signed long long  oData;
1530
 
 
1531
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1532
 
              return oData;
1533
 
            }
1534
 
          case 10:
1535
 
            {
1536
 
              unsigned long long        oData;
1537
 
 
1538
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1539
 
              return oData;
1540
 
            }
1541
 
          case 11:
1542
 
            {
1543
 
              float     oData;
1544
 
 
1545
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1546
 
              return oData;
1547
 
            }
1548
 
          case 12:
1549
 
            {
1550
 
              double    oData;
1551
 
 
1552
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1553
 
              return oData;
1554
 
            }
1555
 
          default:
1556
 
            [NSException raise: NSInvalidArgumentException
1557
 
                        format: @"unknown number type value for get"];
1558
 
        }
1559
 
    }
1560
 
  return 0;
1561
 
}
1562
 
 
1563
 
- (signed long) longValue
1564
 
{
1565
 
  if (GSObjCClass(self) == abstractClass)
1566
 
    [NSException raise: NSInternalInconsistencyException
1567
 
                format: @"get longValue from abstract NSNumber"];
1568
 
  else
1569
 
    {
1570
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1571
 
 
1572
 
      switch (info->typeLevel)
1573
 
        {
1574
 
          case 0:
1575
 
            {
1576
 
              BOOL      oData;
1577
 
 
1578
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1579
 
              return oData;
1580
 
            }
1581
 
          case 1:
1582
 
            {
1583
 
              signed char       oData;
1584
 
 
1585
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1586
 
              return oData;
1587
 
            }
1588
 
          case 2:
1589
 
            {
1590
 
              unsigned char     oData;
1591
 
 
1592
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1593
 
              return oData;
1594
 
            }
1595
 
          case 3:
1596
 
            {
1597
 
              signed short      oData;
1598
 
 
1599
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1600
 
              return oData;
1601
 
            }
1602
 
          case 4:
1603
 
            {
1604
 
              unsigned short    oData;
1605
 
 
1606
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1607
 
              return oData;
1608
 
            }
1609
 
          case 5:
1610
 
            {
1611
 
              signed int        oData;
1612
 
 
1613
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1614
 
              return oData;
1615
 
            }
1616
 
          case 6:
1617
 
            {
1618
 
              unsigned int      oData;
1619
 
 
1620
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1621
 
              return oData;
1622
 
            }
1623
 
          case 7:
1624
 
            {
1625
 
              signed long       oData;
1626
 
 
1627
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1628
 
              return oData;
1629
 
            }
1630
 
          case 8:
1631
 
            {
1632
 
              unsigned long     oData;
1633
 
 
1634
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1635
 
              return oData;
1636
 
            }
1637
 
          case 9:
1638
 
            {
1639
 
              signed long long  oData;
1640
 
 
1641
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1642
 
              return oData;
1643
 
            }
1644
 
          case 10:
1645
 
            {
1646
 
              unsigned long long        oData;
1647
 
 
1648
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1649
 
              return oData;
1650
 
            }
1651
 
          case 11:
1652
 
            {
1653
 
              float     oData;
1654
 
 
1655
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1656
 
              return oData;
1657
 
            }
1658
 
          case 12:
1659
 
            {
1660
 
              double    oData;
1661
 
 
1662
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1663
 
              return oData;
1664
 
            }
1665
 
          default:
1666
 
            [NSException raise: NSInvalidArgumentException
1667
 
                        format: @"unknown number type value for get"];
1668
 
        }
1669
 
    }
1670
 
  return 0;
1671
 
}
1672
 
 
1673
 
- (signed short) shortValue
1674
 
{
1675
 
  if (GSObjCClass(self) == abstractClass)
1676
 
    [NSException raise: NSInternalInconsistencyException
1677
 
                format: @"get shortValue from abstract NSNumber"];
1678
 
  else
1679
 
    {
1680
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1681
 
 
1682
 
      switch (info->typeLevel)
1683
 
        {
1684
 
          case 0:
1685
 
            {
1686
 
              BOOL      oData;
1687
 
 
1688
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1689
 
              return oData;
1690
 
            }
1691
 
          case 1:
1692
 
            {
1693
 
              signed char       oData;
1694
 
 
1695
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1696
 
              return oData;
1697
 
            }
1698
 
          case 2:
1699
 
            {
1700
 
              unsigned char     oData;
1701
 
 
1702
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1703
 
              return oData;
1704
 
            }
1705
 
          case 3:
1706
 
            {
1707
 
              signed short      oData;
1708
 
 
1709
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1710
 
              return oData;
1711
 
            }
1712
 
          case 4:
1713
 
            {
1714
 
              unsigned short    oData;
1715
 
 
1716
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1717
 
              return oData;
1718
 
            }
1719
 
          case 5:
1720
 
            {
1721
 
              signed int        oData;
1722
 
 
1723
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1724
 
              return oData;
1725
 
            }
1726
 
          case 6:
1727
 
            {
1728
 
              unsigned int      oData;
1729
 
 
1730
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1731
 
              return oData;
1732
 
            }
1733
 
          case 7:
1734
 
            {
1735
 
              signed long       oData;
1736
 
 
1737
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1738
 
              return oData;
1739
 
            }
1740
 
          case 8:
1741
 
            {
1742
 
              unsigned long     oData;
1743
 
 
1744
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1745
 
              return oData;
1746
 
            }
1747
 
          case 9:
1748
 
            {
1749
 
              signed long long  oData;
1750
 
 
1751
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1752
 
              return oData;
1753
 
            }
1754
 
          case 10:
1755
 
            {
1756
 
              unsigned long long        oData;
1757
 
 
1758
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1759
 
              return oData;
1760
 
            }
1761
 
          case 11:
1762
 
            {
1763
 
              float     oData;
1764
 
 
1765
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1766
 
              return oData;
1767
 
            }
1768
 
          case 12:
1769
 
            {
1770
 
              double    oData;
1771
 
 
1772
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1773
 
              return oData;
1774
 
            }
1775
 
          default:
1776
 
            [NSException raise: NSInvalidArgumentException
1777
 
                        format: @"unknown number type value for get"];
1778
 
        }
1779
 
    }
1780
 
  return 0;
1781
 
}
1782
 
 
1783
 
- (NSString*) stringValue
1784
 
{
1785
 
  return [self descriptionWithLocale: nil];
1786
 
}
1787
 
 
1788
 
- (unsigned char) unsignedCharValue
1789
 
{
1790
 
  if (GSObjCClass(self) == abstractClass)
1791
 
    [NSException raise: NSInternalInconsistencyException
1792
 
                format: @"get unsignedCharrValue from abstract NSNumber"];
1793
 
  else
1794
 
    {
1795
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1796
 
 
1797
 
      switch (info->typeLevel)
1798
 
        {
1799
 
          case 0:
1800
 
            {
1801
 
              BOOL      oData;
1802
 
 
1803
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1804
 
              return oData;
1805
 
            }
1806
 
          case 1:
1807
 
            {
1808
 
              signed char       oData;
1809
 
 
1810
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1811
 
              return oData;
1812
 
            }
1813
 
          case 2:
1814
 
            {
1815
 
              unsigned char     oData;
1816
 
 
1817
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1818
 
              return oData;
1819
 
            }
1820
 
          case 3:
1821
 
            {
1822
 
              signed short      oData;
1823
 
 
1824
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1825
 
              return oData;
1826
 
            }
1827
 
          case 4:
1828
 
            {
1829
 
              unsigned short    oData;
1830
 
 
1831
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1832
 
              return oData;
1833
 
            }
1834
 
          case 5:
1835
 
            {
1836
 
              signed int        oData;
1837
 
 
1838
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1839
 
              return oData;
1840
 
            }
1841
 
          case 6:
1842
 
            {
1843
 
              unsigned int      oData;
1844
 
 
1845
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1846
 
              return oData;
1847
 
            }
1848
 
          case 7:
1849
 
            {
1850
 
              signed long       oData;
1851
 
 
1852
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1853
 
              return oData;
1854
 
            }
1855
 
          case 8:
1856
 
            {
1857
 
              unsigned long     oData;
1858
 
 
1859
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1860
 
              return oData;
1861
 
            }
1862
 
          case 9:
1863
 
            {
1864
 
              signed long long  oData;
1865
 
 
1866
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1867
 
              return oData;
1868
 
            }
1869
 
          case 10:
1870
 
            {
1871
 
              unsigned long long        oData;
1872
 
 
1873
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1874
 
              return oData;
1875
 
            }
1876
 
          case 11:
1877
 
            {
1878
 
              float     oData;
1879
 
 
1880
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1881
 
              return oData;
1882
 
            }
1883
 
          case 12:
1884
 
            {
1885
 
              double    oData;
1886
 
 
1887
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1888
 
              return oData;
1889
 
            }
1890
 
          default:
1891
 
            [NSException raise: NSInvalidArgumentException
1892
 
                        format: @"unknown number type value for get"];
1893
 
        }
1894
 
    }
1895
 
  return 0;
1896
 
}
1897
 
 
1898
 
- (unsigned int) unsignedIntValue
1899
 
{
1900
 
  if (GSObjCClass(self) == abstractClass)
1901
 
    [NSException raise: NSInternalInconsistencyException
1902
 
                format: @"get unsignedIntValue from abstract NSNumber"];
1903
 
  else
1904
 
    {
1905
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
1906
 
 
1907
 
      switch (info->typeLevel)
1908
 
        {
1909
 
          case 0:
1910
 
            {
1911
 
              BOOL      oData;
1912
 
 
1913
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1914
 
              return oData;
1915
 
            }
1916
 
          case 1:
1917
 
            {
1918
 
              signed char       oData;
1919
 
 
1920
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1921
 
              return oData;
1922
 
            }
1923
 
          case 2:
1924
 
            {
1925
 
              unsigned char     oData;
1926
 
 
1927
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1928
 
              return oData;
1929
 
            }
1930
 
          case 3:
1931
 
            {
1932
 
              signed short      oData;
1933
 
 
1934
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1935
 
              return oData;
1936
 
            }
1937
 
          case 4:
1938
 
            {
1939
 
              unsigned short    oData;
1940
 
 
1941
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1942
 
              return oData;
1943
 
            }
1944
 
          case 5:
1945
 
            {
1946
 
              signed int        oData;
1947
 
 
1948
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1949
 
              return oData;
1950
 
            }
1951
 
          case 6:
1952
 
            {
1953
 
              unsigned int      oData;
1954
 
 
1955
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1956
 
              return oData;
1957
 
            }
1958
 
          case 7:
1959
 
            {
1960
 
              signed long       oData;
1961
 
 
1962
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1963
 
              return oData;
1964
 
            }
1965
 
          case 8:
1966
 
            {
1967
 
              unsigned long     oData;
1968
 
 
1969
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1970
 
              return oData;
1971
 
            }
1972
 
          case 9:
1973
 
            {
1974
 
              signed long long  oData;
1975
 
 
1976
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1977
 
              return oData;
1978
 
            }
1979
 
          case 10:
1980
 
            {
1981
 
              unsigned long long        oData;
1982
 
 
1983
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1984
 
              return oData;
1985
 
            }
1986
 
          case 11:
1987
 
            {
1988
 
              float     oData;
1989
 
 
1990
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1991
 
              return oData;
1992
 
            }
1993
 
          case 12:
1994
 
            {
1995
 
              double    oData;
1996
 
 
1997
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
1998
 
              return oData;
1999
 
            }
2000
 
          default:
2001
 
            [NSException raise: NSInvalidArgumentException
2002
 
                        format: @"unknown number type value for get"];
2003
 
        }
2004
 
    }
2005
 
  return 0;
2006
 
}
2007
 
 
2008
 
- (unsigned long long) unsignedLongLongValue
2009
 
{
2010
 
  if (GSObjCClass(self) == abstractClass)
2011
 
    [NSException raise: NSInternalInconsistencyException
2012
 
                format: @"get unsignedLongLongValue from abstract NSNumber"];
2013
 
  else
2014
 
    {
2015
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
2016
 
 
2017
 
      switch (info->typeLevel)
2018
 
        {
2019
 
          case 0:
2020
 
            {
2021
 
              BOOL      oData;
2022
 
 
2023
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2024
 
              return oData;
2025
 
            }
2026
 
          case 1:
2027
 
            {
2028
 
              signed char       oData;
2029
 
 
2030
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2031
 
              return oData;
2032
 
            }
2033
 
          case 2:
2034
 
            {
2035
 
              unsigned char     oData;
2036
 
 
2037
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2038
 
              return oData;
2039
 
            }
2040
 
          case 3:
2041
 
            {
2042
 
              signed short      oData;
2043
 
 
2044
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2045
 
              return oData;
2046
 
            }
2047
 
          case 4:
2048
 
            {
2049
 
              unsigned short    oData;
2050
 
 
2051
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2052
 
              return oData;
2053
 
            }
2054
 
          case 5:
2055
 
            {
2056
 
              signed int        oData;
2057
 
 
2058
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2059
 
              return oData;
2060
 
            }
2061
 
          case 6:
2062
 
            {
2063
 
              unsigned int      oData;
2064
 
 
2065
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2066
 
              return oData;
2067
 
            }
2068
 
          case 7:
2069
 
            {
2070
 
              signed long       oData;
2071
 
 
2072
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2073
 
              return oData;
2074
 
            }
2075
 
          case 8:
2076
 
            {
2077
 
              unsigned long     oData;
2078
 
 
2079
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2080
 
              return oData;
2081
 
            }
2082
 
          case 9:
2083
 
            {
2084
 
              signed long long  oData;
2085
 
 
2086
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2087
 
              return oData;
2088
 
            }
2089
 
          case 10:
2090
 
            {
2091
 
              unsigned long long        oData;
2092
 
 
2093
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2094
 
              return oData;
2095
 
            }
2096
 
          case 11:
2097
 
            {
2098
 
              float     oData;
2099
 
 
2100
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2101
 
              return oData;
2102
 
            }
2103
 
          case 12:
2104
 
            {
2105
 
              double    oData;
2106
 
 
2107
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2108
 
              return oData;
2109
 
            }
2110
 
          default:
2111
 
            [NSException raise: NSInvalidArgumentException
2112
 
                        format: @"unknown number type value for get"];
2113
 
        }
2114
 
    }
2115
 
  return 0;
2116
 
}
2117
 
 
2118
 
- (unsigned long) unsignedLongValue
2119
 
{
2120
 
  if (GSObjCClass(self) == abstractClass)
2121
 
    [NSException raise: NSInternalInconsistencyException
2122
 
                format: @"get unsignedLongValue from abstract NSNumber"];
2123
 
  else
2124
 
    {
2125
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
2126
 
 
2127
 
      switch (info->typeLevel)
2128
 
        {
2129
 
          case 0:
2130
 
            {
2131
 
              BOOL      oData;
2132
 
 
2133
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2134
 
              return oData;
2135
 
            }
2136
 
          case 1:
2137
 
            {
2138
 
              signed char       oData;
2139
 
 
2140
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2141
 
              return oData;
2142
 
            }
2143
 
          case 2:
2144
 
            {
2145
 
              unsigned char     oData;
2146
 
 
2147
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2148
 
              return oData;
2149
 
            }
2150
 
          case 3:
2151
 
            {
2152
 
              signed short      oData;
2153
 
 
2154
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2155
 
              return oData;
2156
 
            }
2157
 
          case 4:
2158
 
            {
2159
 
              unsigned short    oData;
2160
 
 
2161
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2162
 
              return oData;
2163
 
            }
2164
 
          case 5:
2165
 
            {
2166
 
              signed int        oData;
2167
 
 
2168
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2169
 
              return oData;
2170
 
            }
2171
 
          case 6:
2172
 
            {
2173
 
              unsigned int      oData;
2174
 
 
2175
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2176
 
              return oData;
2177
 
            }
2178
 
          case 7:
2179
 
            {
2180
 
              signed long       oData;
2181
 
 
2182
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2183
 
              return oData;
2184
 
            }
2185
 
          case 8:
2186
 
            {
2187
 
              unsigned long     oData;
2188
 
 
2189
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2190
 
              return oData;
2191
 
            }
2192
 
          case 9:
2193
 
            {
2194
 
              signed long long  oData;
2195
 
 
2196
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2197
 
              return oData;
2198
 
            }
2199
 
          case 10:
2200
 
            {
2201
 
              unsigned long long        oData;
2202
 
 
2203
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2204
 
              return oData;
2205
 
            }
2206
 
          case 11:
2207
 
            {
2208
 
              float     oData;
2209
 
 
2210
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2211
 
              return oData;
2212
 
            }
2213
 
          case 12:
2214
 
            {
2215
 
              double    oData;
2216
 
 
2217
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2218
 
              return oData;
2219
 
            }
2220
 
          default:
2221
 
            [NSException raise: NSInvalidArgumentException
2222
 
                        format: @"unknown number type value for get"];
2223
 
        }
2224
 
    }
2225
 
  return 0;
2226
 
}
2227
 
 
2228
 
- (unsigned short) unsignedShortValue
2229
 
{
2230
 
  if (GSObjCClass(self) == abstractClass)
2231
 
    [NSException raise: NSInternalInconsistencyException
2232
 
                format: @"get unsignedShortValue from abstract NSNumber"];
2233
 
  else
2234
 
    {
2235
 
      GSNumberInfo      *info = GSNumberInfoFromObject(self);
2236
 
 
2237
 
      switch (info->typeLevel)
2238
 
        {
2239
 
          case 0:
2240
 
            {
2241
 
              BOOL      oData;
2242
 
 
2243
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2244
 
              return oData;
2245
 
            }
2246
 
          case 1:
2247
 
            {
2248
 
              signed char       oData;
2249
 
 
2250
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2251
 
              return oData;
2252
 
            }
2253
 
          case 2:
2254
 
            {
2255
 
              unsigned char     oData;
2256
 
 
2257
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2258
 
              return oData;
2259
 
            }
2260
 
          case 3:
2261
 
            {
2262
 
              signed short      oData;
2263
 
 
2264
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2265
 
              return oData;
2266
 
            }
2267
 
          case 4:
2268
 
            {
2269
 
              unsigned short    oData;
2270
 
 
2271
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2272
 
              return oData;
2273
 
            }
2274
 
          case 5:
2275
 
            {
2276
 
              signed int        oData;
2277
 
 
2278
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2279
 
              return oData;
2280
 
            }
2281
 
          case 6:
2282
 
            {
2283
 
              unsigned int      oData;
2284
 
 
2285
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2286
 
              return oData;
2287
 
            }
2288
 
          case 7:
2289
 
            {
2290
 
              signed long       oData;
2291
 
 
2292
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2293
 
              return oData;
2294
 
            }
2295
 
          case 8:
2296
 
            {
2297
 
              unsigned long     oData;
2298
 
 
2299
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2300
 
              return oData;
2301
 
            }
2302
 
          case 9:
2303
 
            {
2304
 
              signed long long  oData;
2305
 
 
2306
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2307
 
              return oData;
2308
 
            }
2309
 
          case 10:
2310
 
            {
2311
 
              unsigned long long        oData;
2312
 
 
2313
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2314
 
              return oData;
2315
 
            }
2316
 
          case 11:
2317
 
            {
2318
 
              float     oData;
2319
 
 
2320
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2321
 
              return oData;
2322
 
            }
2323
 
          case 12:
2324
 
            {
2325
 
              double    oData;
2326
 
 
2327
 
              (*(info->getValue))(self, @selector(getValue:), &oData);
2328
 
              return oData;
2329
 
            }
2330
 
          default:
2331
 
            [NSException raise: NSInvalidArgumentException
2332
 
                        format: @"unknown number type value for get"];
2333
 
        }
2334
 
    }
2335
 
  return 0;
2336
 
}
2337
 
 
2338
 
- (NSComparisonResult) compare: (NSNumber*)other
2339
 
{
2340
 
  double        otherValue;
2341
 
  double        myValue;
2342
 
 
2343
 
  if (other == self)
2344
 
    {
2345
 
      return NSOrderedSame;
2346
 
    }
2347
 
  else if (other == nil)
2348
 
    {
2349
 
      [NSException raise: NSInvalidArgumentException
2350
 
                  format: @"nil argument for compare:"];
2351
 
    }
2352
 
 
2353
 
  myValue = [self doubleValue];
2354
 
  otherValue = [other doubleValue];
2355
 
 
2356
 
  if (myValue == otherValue)
2357
 
    {
2358
 
      return NSOrderedSame;
2359
 
    }
2360
 
  else if (myValue < otherValue)
2361
 
    {
2362
 
      return  NSOrderedAscending;
2363
 
    }
2364
 
  else
2365
 
    {
2366
 
      return NSOrderedDescending;
2367
 
    }
2368
 
}
2369
 
 
2370
 
/*
2371
 
 * Because of the rule that two numbers which are the same according to
2372
 
 * [-isEqual: ] must generate the same hash, we must generate the hash
2373
 
 * from the most general representation of the number.
2374
 
 * NB. Don't change this without changing the matching function in
2375
 
 * NSConcreteNumber.m
2376
 
 */
2377
 
- (unsigned) hash
2378
 
{
2379
 
  union {
2380
 
    double d;
2381
 
    unsigned char c[sizeof(double)];
2382
 
  } val;
2383
 
  unsigned      hash = 0;
2384
 
  unsigned      i;
2385
 
 
2386
 
  val.d = [self doubleValue];
2387
 
  for (i = 0; i < sizeof(double); i++)
2388
 
    {
2389
 
      hash = (hash << 5) + hash + val.c[i];
2390
 
    }
2391
 
  return hash;
2392
 
}
2393
 
 
2394
 
- (BOOL) isEqual: (id)o
2395
 
{
2396
 
  if (o == self)
2397
 
    {
2398
 
      return YES;
2399
 
    }
2400
 
  else if (o == nil)
2401
 
    {
2402
 
      return NO;
2403
 
    }
2404
 
  else if (GSObjCIsInstance(o) == YES
2405
 
    && GSObjCIsKindOf(GSObjCClass(o), abstractClass))
2406
 
    {
2407
 
      return [self isEqualToNumber: (NSNumber*)o];
2408
 
    }
2409
 
  else
2410
 
    {
2411
 
      return [super isEqual: o];
2412
 
    }
2413
 
}
2414
 
 
2415
 
- (BOOL) isEqualToNumber: (NSNumber*)o
2416
 
{
2417
 
  if (o == self)
2418
 
    {
2419
 
      return YES;
2420
 
    }
2421
 
  else if (o == nil)
2422
 
    {
2423
 
      return NO;
2424
 
    }
2425
 
  else if ([self compare: o] == NSOrderedSame)
2426
 
    {
2427
 
      return YES;
2428
 
    }
2429
 
  return NO;
2430
 
}
2431
 
 
2432
 
- (BOOL) isEqualToValue: (NSValue*)o
2433
 
{
2434
 
  if (o == self)
2435
 
    {
2436
 
      return YES;
2437
 
    }
2438
 
  else if (o == nil)
2439
 
    {
2440
 
      return NO;
2441
 
    }
2442
 
  else if (GSObjCIsInstance(o) == YES
2443
 
    && GSObjCIsKindOf(GSObjCClass(o), abstractClass))
2444
 
    {
2445
 
      return [self isEqualToNumber: (NSNumber*)o];
2446
 
    }
2447
 
  return NO;
2448
 
}
2449
 
 
2450
 
/*
2451
 
 * NSCoding
2452
 
 */
2453
 
 
2454
 
- (Class) classForCoder
2455
 
{
2456
 
  return abstractClass;
2457
 
}
2458
 
 
2459
 
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
2460
 
{
2461
 
  if ([aCoder isByref] == NO)
2462
 
    return self;
2463
 
  return [super replacementObjectForPortCoder: aCoder];
2464
 
}
2465
 
 
2466
 
- (void) encodeWithCoder: (NSCoder*)coder
2467
 
{
2468
 
  const char    *t = [self objCType];
2469
 
 
2470
 
  [coder encodeValueOfObjCType: @encode(signed char) at: t];
2471
 
  [coder encodeValueOfObjCType: t at: [self pointerValue]];
2472
 
}
2473
 
 
2474
 
- (id) initWithCoder: (NSCoder*)coder
2475
 
{
2476
 
  char  t[2];
2477
 
  union {
2478
 
    signed char c;
2479
 
    unsigned char C;
2480
 
    signed short s;
2481
 
    unsigned short S;
2482
 
    signed int i;
2483
 
    unsigned int I;
2484
 
    signed long l;
2485
 
    unsigned long L;
2486
 
    signed long long q;
2487
 
    unsigned long long Q;
2488
 
    float f;
2489
 
    double d;
2490
 
  } data;
2491
 
 
2492
 
  [coder decodeValueOfObjCType: @encode(signed char) at: t];
2493
 
  t[1] = '\0';
2494
 
  [coder decodeValueOfObjCType: t at: &data];
2495
 
  switch (*t)
2496
 
    {
2497
 
      case 'c': self = [self initWithChar: data.c];     break;
2498
 
      case 'C': self = [self initWithUnsignedChar: data.C]; break;
2499
 
      case 's': self = [self initWithShort: data.s]; break;
2500
 
      case 'S': self = [self initWithUnsignedShort: data.S]; break;
2501
 
      case 'i': self = [self initWithInt: data.i]; break;
2502
 
      case 'I': self = [self initWithUnsignedInt: data.I]; break;
2503
 
      case 'l': self = [self initWithLong: data.l]; break;
2504
 
      case 'L': self = [self initWithUnsignedLong: data.L]; break;
2505
 
      case 'q': self = [self initWithLongLong: data.q]; break;
2506
 
      case 'Q': self = [self initWithUnsignedLongLong: data.Q]; break;
2507
 
      case 'f': self = [self initWithFloat: data.f]; break;
2508
 
      case 'd': self = [self initWithDouble: data.d]; break;
2509
 
      default:
2510
 
        DESTROY(self);
2511
 
        NSLog(@"Attempt to decode number with unknown ObjC type");
2512
 
    }
2513
 
  return self;
2514
 
}
 
642
  [self subclassResponsibility: _cmd];
 
643
  return NO;
 
644
}
 
645
 
 
646
- (NSDecimal) decimalValue
 
647
{
 
648
  NSDecimalNumber *dn;
 
649
  NSDecimal decimal;
 
650
 
 
651
  dn = [[NSDecimalNumber alloc] initWithString: [self stringValue]];
 
652
  decimal = [dn decimalValue];
 
653
  [dn release];
 
654
  return decimal;
 
655
}
 
656
 
2515
657
@end