Skip to content

Instantly share code, notes, and snippets.

@beccadax
Created March 6, 2013 11:21
Show Gist options
  • Save beccadax/5098673 to your computer and use it in GitHub Desktop.
Save beccadax/5098673 to your computer and use it in GitHub Desktop.
Stupid Macro Tricks™
// TsImageSizeConstraintType is an enum with these four values:
#define MAP \
ENTRY(TsEditionImageSizeConstraintNone) \
ENTRY(TsEditionImageSizeConstraintDimensions) \
ENTRY(TsEditionImageSizeConstraintPixelCount) \
ENTRY(TsEditionImageSizeConstraintByteCount)
NSString * TsStringForImageSizeConstraintType(TsImageSizeConstraintType type) {
switch(type) {
#define ENTRY(TYPE) case TYPE: return @#TYPE;
MAP
#undef ENTRY
}
}
TsImageSizeConstraintType TsImageSizeConstraintTypeFromString(NSString * string) {
static NSDictionary * table;
static dispatch_once_t once;
dispatch_once(&once, ^{
#define ENTRY(TYPE) @#TYPE : @(TYPE),
table = @{ MAP };
#undef ENTRY
});
return [table[string] integerValue];
}
#undef MAP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment