jens@0
|
1 |
//
|
jens@0
|
2 |
// ValueArray.m
|
jens@0
|
3 |
// MYUtilities
|
jens@0
|
4 |
//
|
jens@0
|
5 |
// Copyright 2008 Jens Alfke. All rights reserved.
|
jens@0
|
6 |
//
|
jens@0
|
7 |
|
jens@0
|
8 |
#import "ValueArray.h"
|
jens@0
|
9 |
|
jens@0
|
10 |
|
jens@0
|
11 |
@implementation ValueArray
|
jens@0
|
12 |
|
jens@0
|
13 |
|
jens@0
|
14 |
- (id) initWithCount: (unsigned)count valueSize: (size_t)valueSize
|
jens@0
|
15 |
{
|
jens@0
|
16 |
self = [super init];
|
jens@0
|
17 |
if( self ) {
|
jens@0
|
18 |
_count = count;
|
jens@0
|
19 |
_valueSize = valueSize;
|
jens@0
|
20 |
}
|
jens@0
|
21 |
return self;
|
jens@0
|
22 |
}
|
jens@0
|
23 |
|
jens@0
|
24 |
+ (ValueArray*) valueArrayWithCount: (unsigned)count valueSize: (size_t)valueSize
|
jens@0
|
25 |
{
|
jens@0
|
26 |
return [[(ValueArray*)NSAllocateObject(self,count*valueSize,nil)
|
jens@0
|
27 |
initWithCount: count valueSize: valueSize]
|
jens@0
|
28 |
autorelease];
|
jens@0
|
29 |
}
|
jens@0
|
30 |
|
jens@0
|
31 |
- (unsigned) count {return _count;}
|
jens@0
|
32 |
- (size_t) valueSize {return _valueSize;}
|
jens@0
|
33 |
|
jens@0
|
34 |
- (const void*) valueAtIndex: (unsigned)i
|
jens@0
|
35 |
{
|
jens@0
|
36 |
NSParameterAssert(i<_count);
|
jens@0
|
37 |
return (const char*)object_getIndexedIvars(self) + i*_valueSize;
|
jens@0
|
38 |
}
|
jens@0
|
39 |
|
jens@0
|
40 |
- (void) getValue: (void*)value atIndex: (unsigned)i
|
jens@0
|
41 |
{
|
jens@0
|
42 |
NSParameterAssert(i<_count);
|
jens@0
|
43 |
NSParameterAssert(value!=NULL);
|
jens@0
|
44 |
memcpy(value, object_getIndexedIvars(self) + i*_valueSize, _valueSize);
|
jens@0
|
45 |
}
|
jens@0
|
46 |
|
jens@0
|
47 |
- (void) setValue: (const void*)value atIndex: (unsigned)i
|
jens@0
|
48 |
{
|
jens@0
|
49 |
NSParameterAssert(i<_count);
|
jens@0
|
50 |
NSParameterAssert(value!=NULL);
|
jens@0
|
51 |
memcpy(object_getIndexedIvars(self) + i*_valueSize, value, _valueSize);
|
jens@0
|
52 |
}
|
jens@0
|
53 |
|
jens@0
|
54 |
|
jens@0
|
55 |
|
jens@0
|
56 |
- (id)initWithCoder:(NSCoder *)decoder
|
jens@0
|
57 |
{
|
jens@0
|
58 |
NSParameterAssert([decoder allowsKeyedCoding]);
|
jens@0
|
59 |
NSKeyedUnarchiver *arch = (NSKeyedUnarchiver*)decoder;
|
jens@0
|
60 |
unsigned count = [arch decodeIntForKey: @"count"];
|
jens@0
|
61 |
size_t valueSize = [arch decodeIntForKey: @"valueSize"];
|
jens@0
|
62 |
|
jens@0
|
63 |
[super release];
|
jens@0
|
64 |
self = [[[self class] valueArrayWithCount: count valueSize: valueSize] retain];
|
jens@0
|
65 |
if( self ) {
|
jens@0
|
66 |
unsigned nBytes;
|
jens@0
|
67 |
const void *bytes = [arch decodeBytesForKey: @"values" returnedLength: &nBytes];
|
jens@0
|
68 |
NSAssert(nBytes==count*valueSize,@"Value size mismatch");
|
jens@0
|
69 |
memcpy( object_getIndexedIvars(self), bytes, nBytes );
|
jens@0
|
70 |
}
|
jens@0
|
71 |
return self;
|
jens@0
|
72 |
}
|
jens@0
|
73 |
|
jens@0
|
74 |
|
jens@0
|
75 |
- (void)encodeWithCoder:(NSCoder *)coder
|
jens@0
|
76 |
{
|
jens@0
|
77 |
NSParameterAssert([coder allowsKeyedCoding]);
|
jens@0
|
78 |
NSKeyedArchiver *arch = (NSKeyedArchiver*)coder;
|
jens@0
|
79 |
|
jens@0
|
80 |
[arch encodeInt: _count forKey: @"count"];
|
jens@0
|
81 |
[arch encodeInt: _valueSize forKey: @"valueSize"];
|
jens@0
|
82 |
[arch encodeBytes: object_getIndexedIvars(self)
|
jens@0
|
83 |
length: _count*_valueSize
|
jens@0
|
84 |
forKey: @"values"];
|
jens@0
|
85 |
}
|
jens@0
|
86 |
|
jens@0
|
87 |
|
jens@0
|
88 |
@end
|
jens@0
|
89 |
|
jens@0
|
90 |
|
jens@0
|
91 |
ImplementValueArrayOf(Int,int)
|
jens@0
|
92 |
|
jens@0
|
93 |
ImplementValueArrayOf(Double,double)
|
jens@11
|
94 |
|
jens@11
|
95 |
|
jens@11
|
96 |
/*
|
jens@11
|
97 |
Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
|
jens@11
|
98 |
|
jens@11
|
99 |
Redistribution and use in source and binary forms, with or without modification, are permitted
|
jens@11
|
100 |
provided that the following conditions are met:
|
jens@11
|
101 |
|
jens@11
|
102 |
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
jens@11
|
103 |
and the following disclaimer.
|
jens@11
|
104 |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
jens@11
|
105 |
and the following disclaimer in the documentation and/or other materials provided with the
|
jens@11
|
106 |
distribution.
|
jens@11
|
107 |
|
jens@11
|
108 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
jens@11
|
109 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
jens@11
|
110 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
|
jens@11
|
111 |
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
jens@11
|
112 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
jens@11
|
113 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
jens@11
|
114 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
jens@11
|
115 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
jens@11
|
116 |
*/
|