jens@0: // jens@0: // ValueArray.m jens@0: // MYUtilities jens@0: // jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@0: jens@0: #import "ValueArray.h" jens@0: jens@0: jens@0: @implementation ValueArray jens@0: jens@0: jens@0: - (id) initWithCount: (unsigned)count valueSize: (size_t)valueSize jens@0: { jens@0: self = [super init]; jens@0: if( self ) { jens@0: _count = count; jens@0: _valueSize = valueSize; jens@0: } jens@0: return self; jens@0: } jens@0: jens@0: + (ValueArray*) valueArrayWithCount: (unsigned)count valueSize: (size_t)valueSize jens@0: { jens@0: return [[(ValueArray*)NSAllocateObject(self,count*valueSize,nil) jens@0: initWithCount: count valueSize: valueSize] jens@0: autorelease]; jens@0: } jens@0: jens@0: - (unsigned) count {return _count;} jens@0: - (size_t) valueSize {return _valueSize;} jens@0: jens@0: - (const void*) valueAtIndex: (unsigned)i jens@0: { jens@0: NSParameterAssert(i<_count); jens@0: return (const char*)object_getIndexedIvars(self) + i*_valueSize; jens@0: } jens@0: jens@0: - (void) getValue: (void*)value atIndex: (unsigned)i jens@0: { jens@0: NSParameterAssert(i<_count); jens@0: NSParameterAssert(value!=NULL); jens@0: memcpy(value, object_getIndexedIvars(self) + i*_valueSize, _valueSize); jens@0: } jens@0: jens@0: - (void) setValue: (const void*)value atIndex: (unsigned)i jens@0: { jens@0: NSParameterAssert(i<_count); jens@0: NSParameterAssert(value!=NULL); jens@0: memcpy(object_getIndexedIvars(self) + i*_valueSize, value, _valueSize); jens@0: } jens@0: jens@0: jens@0: jens@0: - (id)initWithCoder:(NSCoder *)decoder jens@0: { jens@0: NSParameterAssert([decoder allowsKeyedCoding]); jens@0: NSKeyedUnarchiver *arch = (NSKeyedUnarchiver*)decoder; jens@0: unsigned count = [arch decodeIntForKey: @"count"]; jens@0: size_t valueSize = [arch decodeIntForKey: @"valueSize"]; jens@0: jens@0: [super release]; jens@0: self = [[[self class] valueArrayWithCount: count valueSize: valueSize] retain]; jens@0: if( self ) { jens@0: unsigned nBytes; jens@0: const void *bytes = [arch decodeBytesForKey: @"values" returnedLength: &nBytes]; jens@0: NSAssert(nBytes==count*valueSize,@"Value size mismatch"); jens@0: memcpy( object_getIndexedIvars(self), bytes, nBytes ); jens@0: } jens@0: return self; jens@0: } jens@0: jens@0: jens@0: - (void)encodeWithCoder:(NSCoder *)coder jens@0: { jens@0: NSParameterAssert([coder allowsKeyedCoding]); jens@0: NSKeyedArchiver *arch = (NSKeyedArchiver*)coder; jens@0: jens@0: [arch encodeInt: _count forKey: @"count"]; jens@0: [arch encodeInt: _valueSize forKey: @"valueSize"]; jens@0: [arch encodeBytes: object_getIndexedIvars(self) jens@0: length: _count*_valueSize jens@0: forKey: @"values"]; jens@0: } jens@0: jens@0: jens@0: @end jens@0: jens@0: jens@0: ImplementValueArrayOf(Int,int) jens@0: jens@0: ImplementValueArrayOf(Double,double)