2001-12-17 14:31:42 +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
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1995-04-03 20:49:14 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1995-04-03 20:49:14 +00:00
|
|
|
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.
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1995-04-03 20:49:14 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-20 10:56:27 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
1995-04-03 20:49:14 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2007-11-29 20:53:26 +00:00
|
|
|
#import "Foundation/NSValue.h"
|
|
|
|
#import "Foundation/NSData.h"
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "Foundation/NSCoder.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
|
|
|
|
2001-04-23 10:41:03 +00:00
|
|
|
static inline int
|
|
|
|
typeSize(const char* type)
|
|
|
|
{
|
|
|
|
switch (*type)
|
|
|
|
{
|
|
|
|
case _C_ID: return sizeof(id);
|
|
|
|
case _C_CLASS: return sizeof(Class);
|
|
|
|
case _C_SEL: return sizeof(SEL);
|
|
|
|
case _C_CHR: return sizeof(char);
|
|
|
|
case _C_UCHR: return sizeof(unsigned char);
|
|
|
|
case _C_SHT: return sizeof(short);
|
|
|
|
case _C_USHT: return sizeof(unsigned short);
|
|
|
|
case _C_INT: return sizeof(int);
|
|
|
|
case _C_UINT: return sizeof(unsigned int);
|
|
|
|
case _C_LNG: return sizeof(long);
|
|
|
|
case _C_ULNG: return sizeof(unsigned long);
|
|
|
|
case _C_LNG_LNG: return sizeof(long long);
|
|
|
|
case _C_ULNG_LNG: return sizeof(unsigned long long);
|
|
|
|
case _C_FLT: return sizeof(float);
|
|
|
|
case _C_DBL: return sizeof(double);
|
|
|
|
case _C_PTR: return sizeof(void*);
|
|
|
|
case _C_CHARPTR: return sizeof(char*);
|
|
|
|
case _C_BFLD:
|
|
|
|
case _C_ARY_B:
|
|
|
|
case _C_UNION_B:
|
2011-05-12 16:03:08 +00:00
|
|
|
case _C_STRUCT_B:
|
|
|
|
{
|
|
|
|
NSUInteger size;
|
|
|
|
|
|
|
|
NSGetSizeAndAlignment(type, &size, 0);
|
|
|
|
return (int)size;
|
|
|
|
}
|
2001-04-23 10:41:03 +00:00
|
|
|
case _C_VOID: return 0;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
// Allocating and Initializing
|
1995-04-03 20:49:14 +00:00
|
|
|
|
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
|
|
|
{
|
2005-02-22 11:22:44 +00:00
|
|
|
if (!value || !type)
|
1999-06-12 14:37:58 +00:00
|
|
|
{
|
|
|
|
NSLog(@"Tried to create NSValue with NULL value or NULL type");
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
1999-06-12 14:37:58 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
self = [super init];
|
2001-04-23 10:19:17 +00:00
|
|
|
if (self != nil)
|
1999-06-12 14:37:58 +00:00
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
int size = typeSize(type);
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
|
|
if (size < 0)
|
2001-04-23 10:19:17 +00:00
|
|
|
{
|
|
|
|
NSLog(@"Tried to create NSValue with invalid Objective-C type");
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
2001-04-23 10:19:17 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2001-04-23 10:41:03 +00:00
|
|
|
if (size > 0)
|
|
|
|
{
|
2010-03-05 09:30:18 +00:00
|
|
|
data = (void *)NSZoneMalloc([self zone], size);
|
2001-04-23 10:41:03 +00:00
|
|
|
memcpy(data, value, size);
|
|
|
|
}
|
2011-03-07 11:34:17 +00:00
|
|
|
size = strlen(type);
|
|
|
|
objctype = (char *)NSZoneMalloc([self zone], size + 1);
|
|
|
|
strncpy(objctype, type, size);
|
|
|
|
objctype[size] = '\0';
|
1999-06-12 14:37:58 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
if (objctype != 0)
|
2010-03-05 09:30:18 +00:00
|
|
|
NSZoneFree([self zone], objctype);
|
2001-04-23 10:41:03 +00:00
|
|
|
if (data != 0)
|
2010-03-05 09:30:18 +00:00
|
|
|
NSZoneFree([self zone], data);
|
1997-09-23 14:03:36 +00:00
|
|
|
[super dealloc];
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
|
2005-02-22 11:22:44 +00:00
|
|
|
// Accessing Data
|
1999-06-12 14:37:58 +00:00
|
|
|
- (void) getValue: (void *)value
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
unsigned size;
|
|
|
|
|
|
|
|
size = (unsigned)typeSize(objctype);
|
|
|
|
if (size > 0)
|
1999-06-12 14:37:58 +00:00
|
|
|
{
|
2001-04-23 13:00:17 +00:00
|
|
|
if (value == 0)
|
2001-04-23 10:41:03 +00:00
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Cannot copy value into NULL buffer"];
|
|
|
|
/* NOT REACHED */
|
|
|
|
}
|
|
|
|
memcpy(value, data, size);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-12 17:05:30 +00:00
|
|
|
- (NSUInteger) hash
|
1998-03-12 14:21:20 +00:00
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
unsigned size = typeSize(objctype);
|
1999-06-12 09:07:50 +00:00
|
|
|
unsigned hash = 0;
|
|
|
|
|
|
|
|
while (size-- > 0)
|
2001-04-23 10:41:03 +00:00
|
|
|
{
|
|
|
|
hash += ((unsigned char*)data)[size];
|
|
|
|
}
|
1999-06-12 09:07:50 +00:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqualToValue: (NSValue*)aValue
|
|
|
|
{
|
2012-08-02 09:27:59 +00:00
|
|
|
if (aValue == self)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2000-10-31 16:17:33 +00:00
|
|
|
if (aValue == nil)
|
2012-08-02 09:27:59 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2010-02-22 10:13:20 +00:00
|
|
|
if (object_getClass(aValue) != object_getClass(self))
|
1999-06-12 09:07:50 +00:00
|
|
|
{
|
2012-08-02 09:27:59 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (!GSSelectorTypesMatch(objctype, ((GSValue*)aValue)->objctype))
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (memcmp(((GSValue*)aValue)->data, data, typeSize(objctype)) != 0)
|
|
|
|
{
|
|
|
|
return NO;
|
1999-06-12 09:07:50 +00:00
|
|
|
}
|
2012-08-02 09:27:59 +00:00
|
|
|
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
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (id) nonretainedObjectValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
unsigned size = (unsigned)typeSize(objctype);
|
|
|
|
|
|
|
|
if (size != sizeof(void*))
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"Return value of size %u as object", size];
|
|
|
|
}
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((id *)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2001-04-23 10:41:03 +00:00
|
|
|
- (NSPoint) pointValue
|
|
|
|
{
|
|
|
|
unsigned size = (unsigned)typeSize(objctype);
|
|
|
|
|
|
|
|
if (size != sizeof(NSPoint))
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"Return value of size %u as NSPoint", size];
|
|
|
|
}
|
|
|
|
return *((NSPoint *)data);
|
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (void *) pointerValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
unsigned size = (unsigned)typeSize(objctype);
|
|
|
|
|
|
|
|
if (size != sizeof(void*))
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"Return value of size %u as pointer", size];
|
|
|
|
}
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((void **)data);
|
2001-04-23 10:41:03 +00:00
|
|
|
}
|
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
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
unsigned size = (unsigned)typeSize(objctype);
|
|
|
|
|
|
|
|
if (size != sizeof(NSRect))
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"Return value of size %u as NSRect", size];
|
|
|
|
}
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((NSRect *)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (NSSize) sizeValue
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2001-04-23 10:41:03 +00:00
|
|
|
unsigned size = (unsigned)typeSize(objctype);
|
|
|
|
|
|
|
|
if (size != sizeof(NSSize))
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"Return value of size %u as NSSize", size];
|
|
|
|
}
|
1999-06-12 14:37:58 +00:00
|
|
|
return *((NSSize *)data);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
2005-02-22 11:22:44 +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
|
|
|
|
2001-04-23 10:41:03 +00:00
|
|
|
size = (unsigned)typeSize(objctype);
|
1997-09-01 22:12:12 +00:00
|
|
|
rep = [NSData dataWithBytes: data length: size];
|
2001-04-10 03:27:01 +00:00
|
|
|
return [NSString stringWithFormat: @"(%s) %@", objctype, [rep description]];
|
1997-09-01 22:12:12 +00:00
|
|
|
}
|
|
|
|
|
1999-06-12 14:37:58 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder *)coder
|
1995-04-03 20:49:14 +00:00
|
|
|
{
|
2011-05-12 16:03:08 +00:00
|
|
|
NSUInteger tsize;
|
1999-06-14 09:07:52 +00:00
|
|
|
unsigned size;
|
2002-02-27 07:02:15 +00:00
|
|
|
NSMutableData *d;
|
1999-06-14 09:07:52 +00:00
|
|
|
|
|
|
|
size = strlen(objctype)+1;
|
|
|
|
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
2001-05-11 15:23:24 +00:00
|
|
|
[coder encodeArrayOfObjCType: @encode(signed char) count: size at: objctype];
|
2011-05-12 16:03:08 +00:00
|
|
|
NSGetSizeAndAlignment(objctype, 0, &tsize);
|
|
|
|
size = tsize;
|
2001-12-03 12:44:33 +00:00
|
|
|
d = [NSMutableData new];
|
|
|
|
[d serializeDataAt: data ofObjCType: objctype context: nil];
|
2002-02-27 07:02:15 +00:00
|
|
|
size = [d length];
|
|
|
|
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
|
|
|
[coder encodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
count: size
|
|
|
|
at: [d bytes]];
|
2001-12-03 12:44:33 +00:00
|
|
|
RELEASE(d);
|
1995-04-03 20:49:14 +00:00
|
|
|
}
|
|
|
|
@end
|