0 notes &
Simple primitives container in Objective-C
After reading this article about emulating templates with C macros, I implemented a small set of fixed-length container classes for primitives, with a template-like set of macros.
The macros were used to generate containers for all primitives (short, int, long, long long, char and unsigned variants, BOOL, float, double) in immutable and mutable flavors. The containers can return the primitive itself, or an autoreleased NSNumber instance that wraps the primitive. The interface for PCMutableDoubleArray, for instance, looks like this:
// Immutable interface - (id) initWithLength: (unsigned int) num; - (id) initWithLength: (unsigned int) num andDoubles: (double) double_0, ...; - (double) doubleAtIndex: (unsigned int) index; - (double*) array; - (unsigned int) count; - (unsigned int) length; - (NSNumber*) numberAtIndex: (unsigned int) index; // Mutable interface - (void) setNumber: (NSNumber*) number atIndex: (unsigned int) index; - (void) setDouble: (type) aDouble atIndex: (unsigned int) index; - (void) setDoubles: (int) length, ...; - (void) setDoublesFromArray: (PCDoubleArray*) arr;
A .zip archive of the containers is available here (BSD licensed).