1995-08-30 21:28:28 +00:00
|
|
|
# line 1 "NSCTemplateValue.m" /* So gdb knows which file we are in */
|
|
|
|
/* NSCTemplateValue - Object encapsulation for C types.
|
1995-04-03 20:49:14 +00:00
|
|
|
Copyright (C) 1993,1994 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
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
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSConcreteValue.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSCoder.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.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 4 cooresponding to each value type */
|
|
|
|
#if TYPE_ORDER == 0
|
|
|
|
# define NSCTemplateValue NSNonretainedObjectValue
|
|
|
|
# define TYPE_METHOD nonretainedObjectValue
|
|
|
|
# define TYPE_NAME id
|
|
|
|
#elif TYPE_ORDER == 1
|
|
|
|
# define NSCTemplateValue NSPointValue
|
|
|
|
# define TYPE_METHOD pointValue
|
|
|
|
# define TYPE_NAME NSPoint
|
|
|
|
#elif TYPE_ORDER == 2
|
|
|
|
# define NSCTemplateValue NSPointerValue
|
|
|
|
# define TYPE_METHOD pointerValue
|
|
|
|
# define TYPE_NAME void *
|
|
|
|
#elif TYPE_ORDER == 3
|
|
|
|
# define NSCTemplateValue NSRectValue
|
|
|
|
# define TYPE_METHOD rectValue
|
|
|
|
# define TYPE_NAME NSRect
|
|
|
|
#elif TYPE_ORDER == 4
|
|
|
|
# define NSCTemplateValue NSSizeValue
|
|
|
|
# define TYPE_METHOD sizeValue
|
|
|
|
# define TYPE_NAME NSSize
|
|
|
|
#endif
|
|
|
|
|
|
|
|
@implementation NSCTemplateValue
|
|
|
|
|
|
|
|
// Allocating and Initializing
|
|
|
|
|
|
|
|
- initValue:(const void *)value
|
|
|
|
withObjCType:(const char *)type
|
|
|
|
{
|
|
|
|
typedef _dt = data;
|
|
|
|
self = [super init];
|
|
|
|
data = *(_dt *)value;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessing Data
|
|
|
|
- (void)getValue:(void *)value
|
|
|
|
{
|
|
|
|
if (!value) {
|
|
|
|
[NSException raise:NSInvalidArgumentException
|
|
|
|
format:@"Cannot copy value into NULL buffer"];
|
|
|
|
/* NOT REACHED */
|
|
|
|
}
|
|
|
|
memcpy( value, &data, objc_sizeof_type([self objCType]) );
|
|
|
|
}
|
|
|
|
|
1998-03-12 14:21:20 +00:00
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
{
|
|
|
|
if ([other isKindOfClass: [self class]]) {
|
|
|
|
return [self isEqualToValue: other];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqualToValue: (NSValue*)aValue
|
|
|
|
{
|
|
|
|
typedef _dt = data;
|
|
|
|
if ([aValue isKindOfClass: [self class]]) {
|
|
|
|
_dt val = [aValue TYPE_METHOD];
|
|
|
|
#if TYPE_ORDER == 0
|
|
|
|
return [data isEqual: val];
|
|
|
|
#elif TYPE_ORDER == 1
|
|
|
|
if (data.x == val.x && data.y == val.y)
|
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
#elif TYPE_ORDER == 2
|
|
|
|
if (data == val)
|
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
#elif TYPE_ORDER == 3
|
|
|
|
if (data.origin.x == val.origin.x &&
|
|
|
|
data.origin.y == val.origin.y &&
|
|
|
|
data.size.width == val.size.width &&
|
|
|
|
data.size.height == val.size.height)
|
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
#elif TYPE_ORDER == 4
|
|
|
|
if (data.width == val.width && data.height == val.height)
|
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
- (const char *)objCType
|
|
|
|
{
|
|
|
|
typedef _dt = data;
|
|
|
|
return @encode(_dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (TYPE_NAME)TYPE_METHOD
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
1997-09-01 22:12:12 +00:00
|
|
|
|
|
|
|
- (NSString *) description
|
|
|
|
{
|
|
|
|
#if TYPE_ORDER == 0
|
1997-09-18 14:56:47 +00:00
|
|
|
return [NSString stringWithFormat: @"{object = %@;}", [data description]];
|
1997-09-01 22:12:12 +00:00
|
|
|
#elif TYPE_ORDER == 1
|
1997-12-19 18:13:52 +00:00
|
|
|
return NSStringFromPoint(data);
|
1997-09-01 22:12:12 +00:00
|
|
|
#elif TYPE_ORDER == 2
|
1997-09-18 14:56:47 +00:00
|
|
|
return [NSString stringWithFormat: @"{pointer = %p;}", data];
|
1997-09-01 22:12:12 +00:00
|
|
|
#elif TYPE_ORDER == 3
|
1997-12-19 18:13:52 +00:00
|
|
|
return NSStringFromRect(data);
|
1997-09-01 22:12:12 +00:00
|
|
|
#elif TYPE_ORDER == 4
|
1997-12-19 18:13:52 +00:00
|
|
|
return NSStringFromSize(data);
|
1997-09-01 22:12:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
// NSCoding
|
|
|
|
- (void)encodeWithCoder:(NSCoder *)coder
|
|
|
|
{
|
1995-08-30 21:49:35 +00:00
|
|
|
const char *type;
|
1995-04-20 21:50:04 +00:00
|
|
|
[super encodeWithCoder:coder];
|
1995-08-30 21:49:35 +00:00
|
|
|
type = [self objCType];
|
|
|
|
[coder encodeValueOfObjCType:@encode(char *) at:&type];
|
|
|
|
[coder encodeValueOfObjCType:type at:&data];
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithCoder:(NSCoder *)coder
|
|
|
|
{
|
1995-08-30 21:49:35 +00:00
|
|
|
[NSException raise:NSInconsistentArchiveException
|
|
|
|
format:@"Cannot unarchive class - Need NSValueDecoder."];
|
1995-04-03 20:49:14 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|