ftp.nice.ch/Attic/openStep/implementation/gnustep/sources/libFoundation.0.7.tgz#/libFoundation-0.7/libFoundation/Foundation/NSConcreteValue.m

This is NSConcreteValue.m in view mode; [Download] [Up]

/* 
   NSConcreteValue.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <Foundation/common.h>
#include <Foundation/NSString.h>
#include <Foundation/NSException.h>
#include <Foundation/NSCoder.h>
#include <Foundation/NSZone.h>
#include <Foundation/NSGeometry.h>
#include <Foundation/exceptions/NSValueExceptions.h>

#include <extensions/objc-runtime.h>

#include "NSConcreteValue.h"

/*
 * Abstract superclass of concrete value classes
 */

@implementation NSConcreteValue

+ allocForType:(const char*)type zone:(NSZone*)zone
{
    int dataSize = objc_sizeof_type(type);
    id  value = NSAllocateObject([NSConcreteObjCValue class], dataSize, zone);

    return value;
}

- initValue:(const void*)value withObjCType:(const char*)type
{
    [self subclassResponsibility:_cmd];
    return self;
}

- (void*)valueBytes
{
    [self subclassResponsibility:_cmd];
    return NULL;
}

- (id)nonretainedObjectValue
{
    THROW([[NSValueException new]
	    setReason:@"this value does not contain an id type value"]);
    return nil;
}

- (void*)pointerValue
{
    THROW([[NSValueException new]
	    setReason:@"this value does not contain a void* value"]);
    return NULL;
}

- (NSRect)rectValue
{
    THROW([[NSValueException new]
	    setReason:@"this value does not contain a NSRect"]);
    return NSMakeRect(0,0,0,0);
}
 
- (NSSize)sizeValue
{
    THROW([[NSValueException new]
	    setReason:@"this value does not contain a NSSize"]);
    return NSMakeSize(0,0);
}
 
- (NSPoint)pointValue
{
    THROW([[NSValueException new]
	    setReason:@"this value does not contain a NSPoint"]);
   return NSMakePoint(0,0);
}

@end

/*
 * Any type concrete value class
 */

@implementation NSConcreteObjCValue

// Allocating and Initializing 

- initValue:(const void*)value withObjCType:(const char*)type
{
    int	size;
    
    if (!value || !type) 
	THROW([[NSValueException new]
		setReason:@"null value or type"]);
		
    self = [super init];
    objctype = Strdup(type);
    size = objc_sizeof_type(type);
    memcpy(data, value, size);
    return self;
}

- (void)dealloc
{
    Free(objctype);
    [super dealloc];
}

// NSCopying

- (id)copyWithZone:(NSZone*)zone
{
    if (NSShouldRetainWithZone(self, zone))
	return [self retain];
    else
	return [[NSConcreteObjCValue allocForType:objctype zone:zone]
		    initValue:(void*)data withObjCType:objctype];
}

// Accessing Data 

- (void*)valueBytes
{
    return data;
}

- (void)getValue:(void*)value
{
    if (!value)
	THROW([[NSValueException new]
		setReason:@"NULL buffer in -getValue"]);
    else 
	memcpy(value, data, objc_sizeof_type(objctype));
}

- (const char*)objCType
{
    return objctype;
}
 
- (void*)pointerValue
{
    if (*objctype != _C_PTR)
	THROW([[NSValueException new]
		setReason:@"this value does not contain a pointer"]);
    return *((void **)data);
} 

- (NSRect)rectValue
{
    if (Strcmp(objctype, @encode(NSRect)))
	THROW([[NSValueException new]
		setReason:@"this value does not contain a NSRect object"]);
    return *((NSRect*)data);
}
 
- (NSSize)sizeValue
{
    if (Strcmp(objctype, @encode(NSSize)))
	THROW([[NSValueException new]
		setReason:@"this value does not contain a NSSize object"]);
    return *((NSSize*)data);
}
 
- (NSPoint)pointValue
{
    if (Strcmp(objctype, @encode(NSPoint)))
	THROW([[NSValueException new]
		setReason:@"this value does not contain a NSPoint object"]);
    return *((NSPoint*)data);
}

- (unsigned)hash
{
    return (unsigned)objctype;
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"<Value with objc type '%s'>", 
	[self objCType]];
}

@end /* NSConcreteObjCValue */

/*
 * Non retained object concrete value
 */

@implementation NSNonretainedObjectValue

// Allocating and Initializing 

- initValue:(const void*)value withObjCType:(const char*)type
{
    data = *(id*)value;
    return self;
}

- (BOOL)isEqual:(NSValue*)aValue
{
    return Strcmp([self objCType], [aValue objCType]) == 0
	    && [[self nonretainedObjectValue]
		    isEqual:[aValue nonretainedObjectValue]];
}

// NSCopying

- (id)copyWithZone:(NSZone*)zone
{
    if (NSShouldRetainWithZone(self, zone))
	return [self retain];
    else
	return [[NSNonretainedObjectValue alloc]
		initValue:(void*)&data withObjCType:NULL];
}

// Accessing Data 

- (void*)valueBytes
{
    return &data;
}

- (void)getValue:(void*)value
{
    if (!value)
	THROW([[NSValueException new]
		setReason:@"NULL buffer in -getValue"]);
    else 
	*(id*)value = data;
}

- (const char*)objCType
{
    return @encode(id);
}
 
-(id)nonretainedObjectValue;
{
    return data;
} 

- (unsigned)hash
{
    return (unsigned)data;
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"<Value with object %@>", data];
}

/* NSCoding */

- (Class)classForCoder
{
    return isa;
}

- (void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeValueOfObjCType:@encode(id) at:&data];
}

- (id)initWithCoder:(NSCoder*)coder
{
    [coder decodeValueOfObjCType:@encode(id) at:&data];
    return self;
}

@end /* NSNonretainedObjectValue */

/*
 * Void Pointer concrete value
 */

@implementation NSPointerValue

// Allocating and Initializing 

- initValue:(const void*)value withObjCType:(const char*)type
{
    data = *(void**)value;
    return self;
}

// NSCopying

- (id)copyWithZone:(NSZone*)zone
{
    if (NSShouldRetainWithZone(self, zone))
	return [self retain];
    else
	return [[NSNonretainedObjectValue alloc]
		    initValue:(void*)&data withObjCType:NULL];
}

// Accessing Data 

- (void*)valueBytes
{
    return &data;
}

- (void)getValue:(void*)value
{
    if (!value)
	THROW([[NSValueException new]
		setReason:@"NULL buffer in -getValue"]);
    else 
	    *(void**)value = data;
}

- (const char*)objCType
{
    return @encode(void*);
}
 
- (void*)pointerValue;
{
    return data;
} 

- (unsigned)hash
{
    return (unsigned)data;
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"<Value with pointer 0x%08x>", data];
}

@end /* NSPointerValue */

/*
 * NSRect concrete value
 */

@implementation NSRectValue

// Allocating and Initializing 

- initValue:(const void*)value withObjCType:(const char*)type
{
    data = *(NSRect*)value;
    return self;
}

// NSCopying

- (id)copyWithZone:(NSZone*)zone
{
    if (NSShouldRetainWithZone(self, zone))
	return [self retain];
    else
	return [[NSRectValue alloc]
		initValue:(void*)&data withObjCType:NULL];
}

// Accessing Data 

- (void*)valueBytes
{
    return &data;
}

- (void)getValue:(void*)value
{
    if (!value)
	THROW([[NSValueException new]
		setReason:@"NULL buffer in -getValue"]);
    else 
	*(NSRect*)value = data;
}

- (const char*)objCType
{
    return @encode(NSRect);
}
 
- (NSRect)rectValue;
{
    return data;
} 

- (unsigned)hash
{
    return (unsigned)(data.origin.x + data.origin.y
	    + data.size.width + data.size.height);
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"<Value with rect %@>",
	NSStringFromRect(data)];
}

/* NSCoding */

- (Class)classForCoder
{
    return isa;
}

- (void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeValueOfObjCType:@encode(NSRect) at:&data];
}

- (id)initWithCoder:(NSCoder*)coder
{
    [coder decodeValueOfObjCType:@encode(NSRect) at:&data];
    return self;
}

@end /* NSRectValue */

/*
 * NSSize concrete value
 */

@implementation NSSizeValue

// Allocating and Initializing 

- initValue:(const void*)value withObjCType:(const char*)type
{
    data = *(NSSize*)value;
    return self;
}

// NSCopying

- (id)copyWithZone:(NSZone*)zone
{
    if (NSShouldRetainWithZone(self, zone))
	return [self retain];
    else
	return [[NSSizeValue alloc]
		    initValue:(void*)&data withObjCType:NULL];
}

// Accessing Data 

- (void*)valueBytes
{
    return &data;
}

- (void)getValue:(void*)value
{
    if (!value)
	THROW([[NSValueException new]
		setReason:@"NULL buffer in -getValue"]);
    else 
	*(NSSize*)value = data;
}

- (const char*)objCType
{
    return @encode(NSSize);
}
 
- (NSPoint)pointValue;
{
    return NSMakePoint(data.width, data.height);
} 

- (NSSize)sizeValue;
{
    return data;
} 

- (unsigned)hash
{
    return (unsigned)(data.width + data.height);
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"<Value with size %@>",
	NSStringFromSize(data)];
}

/* NSCoding */

- (Class)classForCoder
{
    return isa;
}

- (void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeValueOfObjCType:@encode(NSSize) at:&data];
}

- (id)initWithCoder:(NSCoder*)coder
{
    [coder decodeValueOfObjCType:@encode(NSSize) at:&data];
    return self;
}

@end /* NSSizeValue */

/*
 * NSPoint concrete value
 */

@implementation NSPointValue

// Allocating and Initializing 

- initValue:(const void*)value withObjCType:(const char*)type
{
    data = *(NSPoint*)value;
    return self;
}

// NSCopying

- (id)copyWithZone:(NSZone*)zone
{
    if (NSShouldRetainWithZone(self, zone))
	return [self retain];
    else
	return [[NSPointValue alloc]
		    initValue:(void*)&data withObjCType:NULL];
}

// Accessing Data 

- (void*)valueBytes
{
    return &data;
}

- (void)getValue:(void*)value
{
    if (!value)
	THROW([[NSValueException new]
		setReason:@"NULL buffer in -getValue"]);
    else 
	*(NSPoint*)value = data;
}

- (const char*)objCType
{
    return @encode(NSPoint);
}
 
- (NSPoint)pointValue;
{
    return data;
} 

- (NSSize)sizeValue;
{
    return NSMakeSize(data.x, data.y);
} 

- (unsigned)hash
{
    return (unsigned)(data.x + data.y);
}

- (NSString*)description
{
    return [NSString stringWithFormat:@"<Value with point %@>",
	NSStringFromPoint(data)];
}

/* NSCoding */

- (Class)classForCoder
{
    return isa;
}

- (void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeValueOfObjCType:@encode(NSPoint) at:&data];
}

- (id)initWithCoder:(NSCoder*)coder
{
    [coder decodeValueOfObjCType:@encode(NSPoint) at:&data];
    return self;
}

@end /* NSPointValue */

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.