Initial checkin.
5 // Copyright 2008 Jens Alfke. All rights reserved.
11 @implementation ValueArray
14 - (id) initWithCount: (unsigned)count valueSize: (size_t)valueSize
19 _valueSize = valueSize;
24 + (ValueArray*) valueArrayWithCount: (unsigned)count valueSize: (size_t)valueSize
26 return [[(ValueArray*)NSAllocateObject(self,count*valueSize,nil)
27 initWithCount: count valueSize: valueSize]
31 - (unsigned) count {return _count;}
32 - (size_t) valueSize {return _valueSize;}
34 - (const void*) valueAtIndex: (unsigned)i
36 NSParameterAssert(i<_count);
37 return (const char*)object_getIndexedIvars(self) + i*_valueSize;
40 - (void) getValue: (void*)value atIndex: (unsigned)i
42 NSParameterAssert(i<_count);
43 NSParameterAssert(value!=NULL);
44 memcpy(value, object_getIndexedIvars(self) + i*_valueSize, _valueSize);
47 - (void) setValue: (const void*)value atIndex: (unsigned)i
49 NSParameterAssert(i<_count);
50 NSParameterAssert(value!=NULL);
51 memcpy(object_getIndexedIvars(self) + i*_valueSize, value, _valueSize);
56 - (id)initWithCoder:(NSCoder *)decoder
58 NSParameterAssert([decoder allowsKeyedCoding]);
59 NSKeyedUnarchiver *arch = (NSKeyedUnarchiver*)decoder;
60 unsigned count = [arch decodeIntForKey: @"count"];
61 size_t valueSize = [arch decodeIntForKey: @"valueSize"];
64 self = [[[self class] valueArrayWithCount: count valueSize: valueSize] retain];
67 const void *bytes = [arch decodeBytesForKey: @"values" returnedLength: &nBytes];
68 NSAssert(nBytes==count*valueSize,@"Value size mismatch");
69 memcpy( object_getIndexedIvars(self), bytes, nBytes );
75 - (void)encodeWithCoder:(NSCoder *)coder
77 NSParameterAssert([coder allowsKeyedCoding]);
78 NSKeyedArchiver *arch = (NSKeyedArchiver*)coder;
80 [arch encodeInt: _count forKey: @"count"];
81 [arch encodeInt: _valueSize forKey: @"valueSize"];
82 [arch encodeBytes: object_getIndexedIvars(self)
83 length: _count*_valueSize
91 ImplementValueArrayOf(Int,int)
93 ImplementValueArrayOf(Double,double)