libs-base/Source/NSConcreteValue.m

161 lines
3.9 KiB
Mathematica
Raw Normal View History

/* NSConcreteValue.h - Object encapsulation for C types.
Copyright (C) 1993,1994 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@boulder.colorado.edu>
Date: Mar 1995
This file is part of the GNU Objective C Class Library.
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.
*/
#include <foundation/NSConcreteValue.h>
#include <foundation/NSString.h>
#include <foundation/NSException.h>
#include <foundation/NSCoder.h>
#include <foundation/NSZone.h>
#include <objects/stdobjects.h>
/* 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 */
#define NS_RAISE_MALLOC \
[NSException raise:NSMallocException \
format:@"No memory left to allocate"]
#define NS_CHECK_MALLOC(ptr) \
if (!ptr) {NS_RAISE_MALLOC;}
@implementation NSConcreteValue
// NSCopying
- deepen
{
void *old_ptr;
int size;
size = objc_sizeof_type([objctype cString]);
old_ptr = data;
data = (void *)NSZoneMalloc([self zone], size);
NS_CHECK_MALLOC(data)
memcpy(data, old_ptr, size);
objctype = [objctype copyWithZone:[self zone]];
return self;
}
// Allocating and Initializing
- initValue:(const void *)value
withObjCType:(const char *)type
{
int size;
if (!value || !type) {
[NSException raise:NSInvalidArgumentException
format:@"Cannot create with NULL value or NULL type"];
/* NOT REACHED */
}
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) {
[NSException raise:NSInternalInconsistencyException
format:@"Invalid Objective-C type"];
/* NOT REACHED */
}
data = (void *)NSZoneMalloc([self zone], size);
NS_CHECK_MALLOC(data)
memcpy(data, value, size);
objctype = [[NSString stringWithCString:type] retain];
return self;
}
- (void)dealloc
{
[objctype release];
NSZoneFree([self zone], data);
[super dealloc];
}
// 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([objctype cString]) );
}
- (const char *)objCType
{
return [objctype cString];
}
// FIXME: need to check to make sure these hold the right values...
- (id)nonretainedObjectValue
{
return *((id *)data);
}
- (void *)pointerValue
{
return *((void **)data);
}
- (NSRect)rectValue
{
return *((NSRect *)data);
}
- (NSSize)sizeValue
{
return *((NSSize *)data);
}
- (NSPoint)pointValue
{
return *((NSPoint *)data);
}
// NSCoding
- (void)encodeWithCoder:(NSCoder *)coder
{
//FIXME [super encodeWithCoder:coder];
// FIXME: Do we need to check for encoding void, void * or will
// NSCoder do this for us?
[coder encodeObject:objctype];
[coder encodeValueOfObjCType:[objctype cString] at:&data];
}
- (id)initWithCoder:(NSCoder *)coder
{
//FIXME self = [super initWithCoder:coder];
objctype = [[coder decodeObject] retain];
[coder decodeValueOfObjCType:[objctype cString] at:&data];
return self;
}
@end