Skip to content

Instantly share code, notes, and snippets.

@farcaller
Created February 11, 2012 15:56
Show Gist options
  • Save farcaller/1801278 to your computer and use it in GitHub Desktop.
Save farcaller/1801278 to your computer and use it in GitHub Desktop.
Refactored macros from bitbucket.org/snej/myutilities
#define $array(OBJS...) ({id objs[]={OBJS}; \
[NSArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
#define $marray(OBJS...) ({id objs[]={OBJS}; \
[NSMutableArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
#define $mdict(PAIRS...) ( \
{id pairs[]={PAIRS}; \
NSMutableDictionary *d = [NSMutableDictionary dictionary]; \
int cnt = sizeof(pairs)/sizeof(id); \
for(int i=0; i<cnt; i+=2) { \
[d setObject:pairs[i+1] forKey:pairs[i]]; \
} \
d; \
})
#define $object(VAL) ({__typeof(VAL) v=(VAL); _collection_helper_box(&v,@encode(__typeof(v)));})
NSValue* _collection_helper_box(const void *value, const char *encoding);
----------------
#import "NCCollectionHelpers.h"
NSValue* _collection_helper_box(const void *value, const char *encoding)
{
// file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.DeveloperTools.docset/Contents/Resources/Documents/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html
char e = encoding[0];
if( e=='r' ) // ignore 'const' modifier
e = encoding[1];
switch( e ) {
case 'B': return [NSNumber numberWithBool: *(BOOL*)value];
case 'c': return [NSNumber numberWithChar: *(char*)value];
case 'C': return [NSNumber numberWithUnsignedChar: *(char*)value];
case 's': return [NSNumber numberWithShort: *(short*)value];
case 'S': return [NSNumber numberWithUnsignedShort: *(unsigned short*)value];
case 'i': return [NSNumber numberWithInt: *(int*)value];
case 'I': return [NSNumber numberWithUnsignedInt: *(unsigned int*)value];
case 'l': return [NSNumber numberWithLong: *(long*)value];
case 'L': return [NSNumber numberWithUnsignedLong: *(unsigned long*)value];
case 'q': return [NSNumber numberWithLongLong: *(long long*)value];
case 'Q': return [NSNumber numberWithUnsignedLongLong: *(unsigned long long*)value];
case 'f': return [NSNumber numberWithFloat: *(float*)value];
case 'd': return [NSNumber numberWithDouble: *(double*)value];
case '*': return [NSString stringWithUTF8String: *(char**)value];
case '@': return *(id*)value;
default: return [NSValue value: value withObjCType: encoding];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment