1995-08-30 21:32:11 +00:00
|
|
|
# line 1 "NSConcreteNumber.m" /* So gdb knows which file we are in */
|
1995-04-03 20:49:14 +00:00
|
|
|
/* NSConcreteNumber - Object encapsulation of numbers
|
|
|
|
|
1996-02-22 16:06:34 +00:00
|
|
|
Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
Date: Mar 1995
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1995-04-03 20:49:14 +00:00
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSConcreteNumber.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSCoder.h>
|
1998-12-18 17:05:44 +00:00
|
|
|
#include <Foundation/NSPortCoder.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/Coder.h>
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
/* This file should be run through a preprocessor with the macro TYPE_ORDER
|
|
|
|
defined to a number from 0 to 12 cooresponding to each number type */
|
|
|
|
#if TYPE_ORDER == 0
|
|
|
|
# define NumberTemplate NSBoolNumber
|
|
|
|
# define TYPE_METHOD boolValue
|
1998-12-07 19:45:08 +00:00
|
|
|
# define TYPE_FORMAT @"%u"
|
1995-04-03 20:49:14 +00:00
|
|
|
#elif TYPE_ORDER == 1
|
|
|
|
# define NumberTemplate NSCharNumber
|
|
|
|
# define TYPE_METHOD charValue
|
|
|
|
# define TYPE_FORMAT @"%c"
|
2000-03-19 20:57:09 +00:00
|
|
|
#elif TYPE_ORDER == 2
|
|
|
|
# define NumberTemplate NSUCharNumber
|
|
|
|
# define TYPE_METHOD unsignedCharValue
|
|
|
|
# define TYPE_FORMAT @"%c"
|
1995-04-03 20:49:14 +00:00
|
|
|
#elif TYPE_ORDER == 3
|
|
|
|
# define NumberTemplate NSShortNumber
|
|
|
|
# define TYPE_METHOD shortValue
|
|
|
|
# define TYPE_FORMAT @"%hd"
|
2000-03-19 20:57:09 +00:00
|
|
|
#elif TYPE_ORDER == 4
|
|
|
|
# define NumberTemplate NSUShortNumber
|
|
|
|
# define TYPE_METHOD unsignedShortValue
|
|
|
|
# define TYPE_FORMAT @"%hu"
|
1995-04-03 20:49:14 +00:00
|
|
|
#elif TYPE_ORDER == 5
|
|
|
|
# define NumberTemplate NSIntNumber
|
|
|
|
# define TYPE_METHOD intValue
|
|
|
|
# define TYPE_FORMAT @"%d"
|
2000-03-19 20:57:09 +00:00
|
|
|
#elif TYPE_ORDER == 6
|
|
|
|
# define NumberTemplate NSUIntNumber
|
|
|
|
# define TYPE_METHOD unsignedIntValue
|
|
|
|
# define TYPE_FORMAT @"%u"
|
1995-04-03 20:49:14 +00:00
|
|
|
#elif TYPE_ORDER == 7
|
|
|
|
# define NumberTemplate NSLongNumber
|
|
|
|
# define TYPE_METHOD longValue
|
|
|
|
# define TYPE_FORMAT @"%ld"
|
2000-03-19 20:57:09 +00:00
|
|
|
#elif TYPE_ORDER == 8
|
|
|
|
# define NumberTemplate NSULongNumber
|
|
|
|
# define TYPE_METHOD unsignedLongValue
|
|
|
|
# define TYPE_FORMAT @"%lu"
|
1995-04-03 20:49:14 +00:00
|
|
|
#elif TYPE_ORDER == 9
|
|
|
|
# define NumberTemplate NSLongLongNumber
|
|
|
|
# define TYPE_METHOD longLongValue
|
|
|
|
# define TYPE_FORMAT @"%lld"
|
2000-03-19 20:57:09 +00:00
|
|
|
#elif TYPE_ORDER == 10
|
|
|
|
# define NumberTemplate NSULongLongNumber
|
|
|
|
# define TYPE_METHOD unsignedLongLongValue
|
|
|
|
# define TYPE_FORMAT @"%llu"
|
1995-04-03 20:49:14 +00:00
|
|
|
#elif TYPE_ORDER == 11
|
|
|
|
# define NumberTemplate NSFloatNumber
|
|
|
|
# define TYPE_METHOD floatValue
|
|
|
|
# define TYPE_FORMAT @"%f"
|
|
|
|
#elif TYPE_ORDER == 12
|
|
|
|
# define NumberTemplate NSDoubleNumber
|
|
|
|
# define TYPE_METHOD doubleValue
|
|
|
|
# define TYPE_FORMAT @"%g"
|
|
|
|
#endif
|
|
|
|
|
1998-02-05 22:06:20 +00:00
|
|
|
@interface NSNumber (Private)
|
2000-03-19 20:57:09 +00:00
|
|
|
- (int) _typeOrder;
|
1998-02-05 22:06:20 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NumberTemplate (Private)
|
2000-03-19 20:57:09 +00:00
|
|
|
- (int) _typeOrder
|
1998-02-05 22:06:20 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return TYPE_ORDER;
|
1998-02-05 22:06:20 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
@implementation NumberTemplate
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (id) initWithBytes: (const void*)value objCType: (const char*)type
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-25 05:57:30 +00:00
|
|
|
typedef __typeof__(data) _dt;
|
2000-03-19 20:57:09 +00:00
|
|
|
data = *(_dt*)value;
|
1999-06-14 09:07:52 +00:00
|
|
|
return self;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
/*
|
|
|
|
* Because of the rule that two numbers which are the same according to
|
|
|
|
* [-isEqual: ] must generate the same hash, we must generate the hash
|
|
|
|
* from the most general representation of the number.
|
|
|
|
* NB. Don't change this without changing the matching function in
|
|
|
|
* NSNumber.m
|
|
|
|
*/
|
|
|
|
- (unsigned) hash
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
unsigned char c[sizeof(double)];
|
|
|
|
} val;
|
|
|
|
unsigned hash = 0;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If possible use a cached hash value for small integers.
|
|
|
|
*/
|
|
|
|
#if TYPE_ORDER < 11
|
|
|
|
#if (TYPE_ORDER & 1)
|
|
|
|
if (data <= GS_SMALL && data >= -GS_SMALL)
|
|
|
|
#else
|
|
|
|
if (data <= GS_SMALL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return GSSmallHash((int)data);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
val.d = [self doubleValue];
|
|
|
|
for (i = 0; i < sizeof(double); i++)
|
|
|
|
{
|
|
|
|
hash += val.c[i];
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) boolValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (char) charValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (double) doubleValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (float) floatValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (int) intValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (long long) longLongValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (long) longValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (short) shortValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (unsigned char) unsignedCharValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (unsigned int) unsignedIntValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (unsigned long long) unsignedLongLongValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (unsigned long) unsignedLongValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (unsigned short) unsignedShortValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
return data;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (NSComparisonResult) compare: (NSNumber*)other
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2000-03-19 20:57:09 +00:00
|
|
|
GSNumberInfo *info;
|
1999-06-14 09:07:52 +00:00
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
if (other == self)
|
|
|
|
{
|
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
|
|
|
info = GSNumberInfoFromObject(other);
|
|
|
|
if (TYPE_ORDER >= info->typeOrder)
|
1999-06-14 09:07:52 +00:00
|
|
|
{
|
1999-06-25 05:57:30 +00:00
|
|
|
typedef __typeof__(data) _dt;
|
2000-03-19 20:57:09 +00:00
|
|
|
_dt other_data = (*(info->TYPE_METHOD))(other, @selector(TYPE_METHOD));
|
1999-06-14 09:07:52 +00:00
|
|
|
|
|
|
|
if (data == other_data)
|
|
|
|
{
|
2000-03-19 20:57:09 +00:00
|
|
|
return NSOrderedSame;
|
1998-02-05 22:06:20 +00:00
|
|
|
}
|
2000-03-19 20:57:09 +00:00
|
|
|
else if (data < other_data)
|
1999-06-14 09:07:52 +00:00
|
|
|
{
|
2000-03-19 20:57:09 +00:00
|
|
|
return NSOrderedAscending;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NSOrderedDescending;
|
1998-02-05 22:06:20 +00:00
|
|
|
}
|
|
|
|
}
|
1999-06-14 09:07:52 +00:00
|
|
|
else
|
|
|
|
{
|
2000-03-19 20:57:09 +00:00
|
|
|
NSComparisonResult r;
|
|
|
|
|
|
|
|
r = (*(info->compValue))(other, @selector(compare:), self);
|
1999-06-14 09:07:52 +00:00
|
|
|
|
|
|
|
if (r == NSOrderedAscending)
|
|
|
|
{
|
|
|
|
return NSOrderedDescending;
|
1998-02-05 22:06:20 +00:00
|
|
|
}
|
1999-06-14 09:07:52 +00:00
|
|
|
if (r == NSOrderedDescending)
|
|
|
|
{
|
|
|
|
return NSOrderedAscending;
|
1998-02-05 22:06:20 +00:00
|
|
|
}
|
1999-06-14 09:07:52 +00:00
|
|
|
return r;
|
1998-02-05 22:06:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (NSString*) descriptionWithLocale: (NSDictionary*)locale
|
1998-02-05 22:06:20 +00:00
|
|
|
{
|
1998-12-07 19:45:08 +00:00
|
|
|
#if TYPE_ORDER == 0
|
1999-06-14 09:07:52 +00:00
|
|
|
return (data) ? @"YES" : @"NO";
|
1998-12-07 19:45:08 +00:00
|
|
|
#else
|
1999-06-14 09:07:52 +00:00
|
|
|
return [NSString stringWithFormat: TYPE_FORMAT, data];
|
1998-12-07 19:45:08 +00:00
|
|
|
#endif
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override these from NSValue
|
2000-03-19 20:57:09 +00:00
|
|
|
- (void) getValue: (void*)value
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2000-03-19 20:57:09 +00:00
|
|
|
if (value == 0)
|
1999-06-14 09:07:52 +00:00
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Cannot copy value into NULL pointer"];
|
|
|
|
/* NOT REACHED */
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2000-03-19 21:04:02 +00:00
|
|
|
memcpy(value, &data, objc_sizeof_type([self objCType]));
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2000-03-19 20:57:09 +00:00
|
|
|
- (const char*) objCType
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-25 05:57:30 +00:00
|
|
|
typedef __typeof__(data) _dt;
|
1996-02-22 16:06:34 +00:00
|
|
|
return @encode(_dt);
|
|
|
|
}
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
// NSCoding
|
2000-03-19 20:57:09 +00:00
|
|
|
- (Class) classForCoder
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1996-02-22 16:06:34 +00:00
|
|
|
return [self class];
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
|
|
|
|
{
|
|
|
|
if ([aCoder isByref] == NO)
|
|
|
|
return self;
|
|
|
|
return [super replacementObjectForPortCoder: aCoder];
|
|
|
|
}
|
|
|
|
|
1996-02-22 16:06:34 +00:00
|
|
|
- (void) encodeWithCoder: coder
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1996-02-22 16:06:34 +00:00
|
|
|
const char *type = [self objCType];
|
1999-06-14 09:07:52 +00:00
|
|
|
[coder encodeValueOfObjCType: type at: &data];
|
1996-02-22 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: coder
|
|
|
|
{
|
|
|
|
const char *type = [self objCType];
|
1999-06-14 09:07:52 +00:00
|
|
|
[coder decodeValueOfObjCType: type at: &data];
|
1996-02-22 16:06:34 +00:00
|
|
|
return self;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|