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