Last active
December 15, 2015 05:19
-
-
Save griotspeak/5207733 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STRUNG_ENUM(THE_ARGS...) STRUNG_SOMETHING(NS_ENUM, THE_ARGS) | |
#define STRUNG_OPTIONS(THE_ARGS...) STRUNG_SOMETHING(NS_OPTIONS, THE_ARGS) | |
#define STRUNG_SOMETHING(TARGET_NS_MACRO, ENUM_BASE_TYPE, ENUM_NULL_VALUE, ENUM_NAME, ENUM_VALUES...) typedef TARGET_NS_MACRO (ENUM_BASE_TYPE, ENUM_NAME) { \ | |
ENUM_VALUES \ | |
}; \ | |
\ | |
\ | |
\ | |
static NSString * ENUM_NAME ## ToDebugString(int enumValue) { \ | |
static NSString *enumDescription = @"" #ENUM_VALUES; \ | |
/*parse the enum values into a dict*/ \ | |
static NSDictionary *enumsByValue = nil; \ | |
if (enumsByValue == nil) { \ | |
NSMutableDictionary *mutableEnumsByValue = [NSMutableDictionary dictionary]; \ | |
NSArray *enumPairs = [enumDescription componentsSeparatedByString:@","]; \ | |
\ | |
NSInteger lastValue = 0 - 1; /*set to 1 before the default value for the first enum*/ \ | |
for (NSString *enumPair in enumPairs) { \ | |
NSArray *labelAndValue = [enumPair componentsSeparatedByString:@"="]; \ | |
NSString *label = [[labelAndValue objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; \ | |
BOOL hasExplictValue = [labelAndValue count] > 1; \ | |
NSInteger value = (hasExplictValue) ?[[labelAndValue objectAtIndex:1] integerValue] : lastValue + 1; \ | |
\ | |
[mutableEnumsByValue setObject:label forKey:[NSNumber numberWithInteger:value]]; \ | |
\ | |
lastValue = value; \ | |
} \ | |
\ | |
enumsByValue = [mutableEnumsByValue copy]; \ | |
} \ | |
\ | |
NSString *label = [enumsByValue objectForKey:[NSNumber numberWithInteger:enumValue]]; \ | |
if (label != nil) { return label; } \ | |
\ | |
return [NSString stringWithFormat:@"%i not defined as a %s value", enumValue, #ENUM_NAME]; \ | |
} \ | |
\ | |
static ENUM_NAME ENUM_NAME ## FromDebugString(NSString * string) { \ | |
static NSString *enumDescription = @"" #ENUM_VALUES; \ | |
/*parse the enum values into a dict*/ \ | |
static NSDictionary *enumsByString = nil; \ | |
if (enumsByString == nil) { \ | |
NSMutableDictionary *mutableEnumsByString = [NSMutableDictionary dictionary]; \ | |
NSArray *enumPairs = [enumDescription componentsSeparatedByString:@","]; \ | |
\ | |
NSInteger lastValue = 0 - 1; /*set to 1 before the default value for the first enum*/ \ | |
for (NSString *enumPair in enumPairs) { \ | |
NSArray *labelAndValue = [enumPair componentsSeparatedByString:@"="]; \ | |
NSString *label = [[labelAndValue objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; \ | |
BOOL hasExplictValue = [labelAndValue count] > 1; \ | |
NSInteger value = (hasExplictValue) ?[[labelAndValue objectAtIndex:1] integerValue] : lastValue + 1; \ | |
\ | |
[mutableEnumsByString setObject:[NSNumber numberWithInteger:value] forKey:label]; \ | |
\ | |
lastValue = value; \ | |
} \ | |
\ | |
enumsByString = [mutableEnumsByString copy]; \ | |
} \ | |
\ | |
NSNumber *value = [enumsByString objectForKey:string]; \ | |
if (value == nil) { \ | |
return (ENUM_NAME)ENUM_NULL_VALUE; \ | |
} else { \ | |
return value.integerValue; \ | |
} \ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Enum macro based on this gist which is based on this post