2001-01-12 14:29:34 +00:00
|
|
|
/* GSValue - Object encapsulation for C types.
|
1999-06-12 14:37:58 +00:00
|
|
|
Copyright (C) 1993,1994,1995,1999 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>
|
2001-01-12 14:29:34 +00:00
|
|
|
#include <Foundation/NSValue.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSString.h>
|
1997-09-01 22:12:12 +00:00
|
|
|
#include <Foundation/NSData.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSCoder.h>
|
|
|
|
#include <Foundation/NSZone.h>
|
2000-10-31 11:05:23 +00:00
|
|
|
#include <Foundation/NSObjCRuntime.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.h>
|
1995-04-03 20:49:14 +00:00
|
|
|
|
2001-01-12 14:29:34 +00:00
|
|
|
@interface GSValue : NSValue
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
char *objctype;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
/* This is the real, general purpose value object. I've implemented all the
|
|
|
|
methods here (like pointValue) even though most likely, other concrete
|
|
|
|
subclasses were created to handle these types */
|
|
|
|
|
2001-01-12 14:29:34 +00:00
|
|
|
@implementation GSValue
|
1995-04-03 20:49:14 +00:00
|
|
|
|
|
|
|
// Allocating and Initializing
|
|
|
|
|
1999-06-14 09:07:52 +00:00
|
|
|
- (id) initWithBytes: (const void *)value
|
|
|
|
objCType: (const char *)type
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
int size;
|
|
|
|
|
|
|
|
if (!value || !type)
|
|
|
|
{
|
|
|
|
NSLog(@"Tried to create NSValue with NULL value or NULL type");
|
1999-06-14 09:07:52 +00:00
|
|
|
RELEASE(self);
|
1999-06-12 14:37:58 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
// FIXME: objc_sizeof_type will abort when it finds an invalid type, when
|
|
|
|
// we really want to just raise an exception
|
|
|
|
size = objc_sizeof_type(type);
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
|
|
|
NSLog(@"Tried to create NSValue with invalid Objective-C type");
|
1999-06-14 09:07:52 +00:00
|
|
|
RELEASE(self);
|
1999-06-12 14:37:58 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
data = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
1999-06-12 14:37:58 +00:00
|
|
|
memcpy(data, value, size);
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
objctype = (char *)NSZoneMalloc(GSObjCZone(self), strlen(type)+1);
|
1999-06-12 14:37:58 +00:00
|
|
|
strcpy(objctype, type);
|
|
|
|
return self;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (void) dealloc
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1997-09-23 14:03:36 +00:00
|
|
|
if (objctype)
|
2000-10-31 16:17:33 +00:00
|
|
|
NSZoneFree(GSObjCZone(self), objctype);
|
1997-09-23 14:03:36 +00:00
|
|
|
if (data)
|
2000-10-31 16:17:33 +00:00
|
|
|
NSZoneFree(GSObjCZone(self), data);
|
1997-09-23 14:03:36 +00:00
|
|
|
[super dealloc];
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accessing Data
|
1999-06-12 14:37:58 +00:00
|
|
|
- (void) getValue: (void *)value
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Cannot copy value into NULL buffer"];
|
|
|
|
/* NOT REACHED */
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
1999-06-12 14:37:58 +00:00
|
|
|
memcpy(value, data, objc_sizeof_type(objctype));
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 09:07:50 +00:00
|
|
|
- (unsigned) hash
|
1998-03-12 14:21:20 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
unsigned size = objc_sizeof_type(objctype);
|
1999-06-12 09:07:50 +00:00
|
|
|
unsigned hash = 0;
|
|
|
|
|
|
|
|
while (size-- > 0)
|
|
|
|
hash += ((unsigned char*)data)[size];
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqualToValue: (NSValue*)aValue
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
if (aValue == nil)
|
|
|
|
return NO;
|
|
|
|
if (GSObjCClass(aValue) != GSObjCClass(self))
|
1999-06-12 09:07:50 +00:00
|
|
|
return NO;
|
2001-01-12 14:29:34 +00:00
|
|
|
if (strcmp(objctype, ((GSValue*)aValue)->objctype) != 0)
|
1998-03-12 14:21:20 +00:00
|
|
|
return NO;
|
1999-06-12 09:07:50 +00:00
|
|
|
else
|
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
unsigned size = objc_sizeof_type(objctype);
|
1999-06-12 09:07:50 +00:00
|
|
|
|
2001-01-12 14:29:34 +00:00
|
|
|
if (memcmp(((GSValue*)aValue)->data, data, size) != 0)
|
1999-06-12 09:07:50 +00:00
|
|
|
return NO;
|
|
|
|
return YES;
|
|
|
|
}
|
1998-03-12 14:21:20 +00:00
|
|
|
}
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
- (const char *)objCType
|
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
return objctype;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: need to check to make sure these hold the right values...
|
1999-06-12 14:37:58 +00:00
|
|
|
- (id) nonretainedObjectValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((id *)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (void *) pointerValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((void **)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (NSRect) rectValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((NSRect *)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (NSSize) sizeValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((NSSize *)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (NSPoint) pointValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((NSPoint *)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1997-09-01 22:12:12 +00:00
|
|
|
- (NSString *) description
|
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
unsigned size;
|
|
|
|
NSData *rep;
|
1997-09-01 22:12:12 +00:00
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
size = objc_sizeof_type(objctype);
|
1997-09-01 22:12:12 +00:00
|
|
|
rep = [NSData dataWithBytes: data length: size];
|
|
|
|
return [NSString stringWithFormat: @"(%@) %@", objctype, [rep description]];
|
|
|
|
}
|
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
// NSCoding
|
1999-06-12 14:37:58 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder *)coder
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
unsigned size;
|
|
|
|
|
|
|
|
size = strlen(objctype)+1;
|
|
|
|
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
|
|
|
[coder encodeArrayOfObjCType: @encode(char) count: size at: objctype];
|
|
|
|
size = objc_sizeof_type(objctype);
|
|
|
|
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
|
|
|
[coder encodeArrayOfObjCType: @encode(unsigned char) count: size at: data];
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (id) initWithCoder: (NSCoder *)coder
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
1999-06-14 09:07:52 +00:00
|
|
|
unsigned size;
|
|
|
|
|
|
|
|
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
2000-10-31 16:17:33 +00:00
|
|
|
objctype = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
1999-06-14 09:07:52 +00:00
|
|
|
[coder decodeArrayOfObjCType: @encode(char) count: size at: objctype];
|
|
|
|
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
2000-10-31 16:17:33 +00:00
|
|
|
data = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
1999-06-14 09:07:52 +00:00
|
|
|
[coder decodeArrayOfObjCType: @encode(unsigned char) count: size at: data];
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
return self;
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|