Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

NSStringFromEnum.h

By: sophtwhere on Nov 20th, 2013  |  syntax: Objective C  |  size: 7.37 KB  |  views: 92  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //
  2. //  NSStringFromEnum.h
  3. //
  4. //  Created by Jonathan Annett on 21/11/13.
  5. //  Copyright (c) 2013 Sophtwhere. All rights reserved.
  6. //
  7.  
  8. /*
  9.  
  10.  example 1 : completely define a NEW enum typedef, with string converters.
  11.  
  12.  notes:
  13.  
  14.  - define_XXX is a macro that calls a macro to define an aspect of type XXX.
  15.  - interface_NSString_Enum_DefinitionAndConverters can take up to 4 parameters.
  16.  .     1 (manadtory) the enum name (minus the word "enum")
  17.  .     2 an option name to use for the parameter in the NSStringFromEnumXXX function. purely sugar.
  18.  .     3 an optional type rename for use in the function names (does not affect typedef)
  19.  .       you'd use this if you dont wan't the word Enum in the function name, for example.
  20.  .     4 if you want to camel case the function names, specify parameter 3 with first char caprilized,
  21.  .       and use the same name in 4, with the first character as lower case.
  22.  
  23.  - whatever parameters you use in interface_NSString_Enum_DefinitionAndConverters()
  24.  use the same ones in implementation_NSString_Enum_Converters()
  25.  
  26.  tip:
  27.  
  28.  to ensure the same settings are used, make a #define for them (see example 2 for an example of this).
  29.  
  30.  
  31.  this first example however is a simple case using default names, since the type name is already capitalized.
  32.  
  33.  in myfile.h
  34.  --------------------------------------------------------
  35.  
  36.  #import "NSStringFromEnum.h"
  37.  
  38.  #define define_Dispatch_chain_cmd(enum)\
  39.  enum(chain_done,=0)\
  40.  enum(chain_entry)\
  41.  enum(chain_bg)\
  42.  enum(chain_mt)\
  43.  enum(chain_alt)\
  44.  enum(chain_for_c)\
  45.  enum(chain_while)\
  46.  enum(chain_continue_for)\
  47.  enum(chain_continue_while)\
  48.  enum(chain_break_for)\
  49.  enum(chain_break_while)\
  50.  enum(chain_previous)\
  51.  enum(chain_if)\
  52.  enum(chain_else)\
  53.  
  54.  
  55.  interface_NSString_Enum_DefinitionAndConverters(Dispatch_chain_cmd)
  56.  --------------------------------------------------------
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  in myfile.m:
  63.  --------------------------------------------------------
  64.  #import "myfile.h"
  65.  
  66.  implementation_NSString_Enum_Converters(Dispatch_chain_cmd)
  67.  --------------------------------------------------------
  68.  
  69.  
  70.  to use :
  71.  
  72.  NSString *NSStringFromEnumDispatch_chain_cmd(enum Dispatch_chain_cmd value);
  73.  
  74.  NSStringFromEnumDispatch_chain_cmd(chain_for_c) returns @"chain_for_c"
  75.  
  76.  
  77.  enum Dispatch_chain_cmd enumDispatch_chain_cmdFromNSString(NSString *value);
  78.  
  79.  
  80.  enumDispatch_chain_cmdFromNSString(@"chain_previous") returns chain_previous
  81.  
  82.  
  83.  
  84.  Example 2: provide conversion routines for an existing enum
  85.  also demonstrates using a settings string, and renaming the typename used in the functions.
  86.  
  87.  
  88.  
  89.  in myfile.h
  90.  --------------------------------------------------------
  91.  
  92.  #import "NSStringFromEnum.h"
  93.  
  94.  
  95.  #define CAEdgeAntialiasingMask_SETTINGS_PARAMS CAEdgeAntialiasingMask,mask,EdgeMask,edgeMask
  96.  
  97.  interface_NSString_Enum_Converters(CAEdgeAntialiasingMask_SETTINGS_PARAMS)
  98.  
  99.  --------------------------------------------------------
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  in myfile.m:
  106.  --------------------------------------------------------
  107.  
  108.  // we can put this in the .m file as we are not defining a typedef, just the strings.
  109.  #define define_CAEdgeAntialiasingMask(enum)\
  110.  enum(kCALayerLeftEdge)\
  111.  enum(kCALayerRightEdge)\
  112.  enum(kCALayerBottomEdge)\
  113.  enum(kCALayerTopEdge)
  114.  
  115.  
  116.  
  117.  implementation_NSString_Enum_Converters(CAEdgeAntialiasingMask_SETTINGS_PARAMS)
  118.  
  119.  */
  120.  
  121.  
  122.  
  123.  
  124. #import "SW+Variadic.h"
  125.  
  126. #define make_typedef_enum(name,...)\
  127. name __VA_ARGS__,
  128.  
  129. #define make_enum_to_NSString(name,...)\
  130. [name] = @#name,
  131.  
  132. #define make_NSString_to_enum(name,...)\
  133. if ([value isEqualToString:@#name]) return name;
  134.  
  135. #define __NSString_Enum_Converters_ERROR__ Error_Please_Specify_Enum_Name
  136.  
  137.  
  138.  
  139. #define interface_NSString_Enum_Definition_0() \
  140. __NSString_Enum_Converters_ERROR__
  141.  
  142. #define interface_NSString_Enum_Definition_1(typeName) \
  143. interface_NSString_Enum_Definition_4(typeName,value,...)
  144.  
  145. #define interface_NSString_Enum_Definition_2(typeName,value) \
  146. interface_NSString_Enum_Definition_4(typeName,value,...)
  147.  
  148. #define interface_NSString_Enum_Definition_3(typeName,value,...) \
  149. interface_NSString_Enum_Definition_4(typeName,value)
  150.  
  151. #define interface_NSString_Enum_Definition_4(typeName,value,...) \
  152. typedef enum typeName {\
  153. define_##typeName(make_typedef_enum)\
  154. } typeName;
  155.  
  156.  
  157. #define interface_NSString_Enum_Converters_0() \
  158. __NSString_Enum_Converters_ERROR__
  159.  
  160. #define interface_NSString_Enum_Converters_1(typeName) \
  161. interface_NSString_Enum_Converters_4(typeName,value,Enum##typeName,enum##typeName)
  162.  
  163. #define interface_NSString_Enum_Converters_2(typeName,value) \
  164. interface_NSString_Enum_Converters_4(typeName,value,Enum##typeName,enum##typeName)
  165.  
  166.  
  167. #define interface_NSString_Enum_Converters_3(typeName,value,AltTypeName) \
  168. interface_NSString_Enum_Converters_4(typeName,value,AltTypeName,AltTypeName)
  169.  
  170.  
  171. #define interface_NSString_Enum_Converters_4(typeName,value,AltTypeName,altTypeName) \
  172. extern NSString * NSStringFrom##AltTypeName ( enum typeName value );\
  173. extern enum typeName altTypeName##FromNSString(NSString *value);\
  174.  
  175.  
  176.  
  177.  
  178.  
  179. #define interface_NSString_Enum_DefinitionAndConverters_0() \
  180. __NSString_Enum_Converters_ERROR__
  181.  
  182.  
  183. #define interface_NSString_Enum_DefinitionAndConverters_1(typeName) \
  184. interface_NSString_Enum_DefinitionAndConverters_4(typeName,value,Enum##typeName,enum##typeName)
  185.  
  186. #define interface_NSString_Enum_DefinitionAndConverters_2(typeName,value) \
  187. interface_NSString_Enum_DefinitionAndConverters_4(typeName,value,Enum##typeName,enum##typeName)
  188.  
  189.  
  190. #define interface_NSString_Enum_DefinitionAndConverters_3(typeName,value,AltTypeName) \
  191. interface_NSString_Enum_DefinitionAndConverters_4(typeName,value,AltTypeName,AltTypeName)
  192.  
  193.  
  194.  
  195. #define interface_NSString_Enum_DefinitionAndConverters_4(typeName,value,AltTypeName,altTypeName)\
  196. interface_NSString_Enum_Definition_4(typeName,value,AltTypeName,altTypeName)\
  197. interface_NSString_Enum_Converters_4(typeName,value,AltTypeName,altTypeName)\
  198.  
  199.  
  200.  
  201. #define implementation_NSString_Enum_Converters_0() __NSString_Enum_Converters_ERROR__
  202.  
  203.  
  204. #define implementation_NSString_Enum_Converters_1(typeName)\
  205. implementation_NSString_Enum_Converters_4(typeName,value,Enum##typeName,enum##typeName)
  206.  
  207. #define implementation_NSString_Enum_Converters_2(typeName,value)\
  208. implementation_NSString_Enum_Converters_4(typeName,value,Enum##typeName,enum##typeName)
  209.  
  210.  
  211. #define implementation_NSString_Enum_Converters_3(typeName,value,AltTypeName) \
  212. implementation_NSString_Enum_Converters_4(typeName,value,AltTypeName,altTypeName)
  213.  
  214.  
  215. #define implementation_NSString_Enum_Converters_4(typeName,value_,AltTypeName,altTypeName)\
  216. NSString * NSStringFrom##AltTypeName(enum typeName value_){\
  217. static NSString * const NSStringFrom_##typeName##_[] = {\
  218. define_##typeName(make_enum_to_NSString)\
  219. };\
  220. return NSStringFrom_##typeName##_[value_];\
  221. }\
  222. enum typeName altTypeName##FromNSString(NSString *value) {\
  223. define_##typeName(make_NSString_to_enum)\
  224. return (enum typeName) 0;\
  225. }
  226.  
  227.  
  228.  
  229.  
  230. #define interface_NSString_Enum_Definition(...) VARARG(interface_NSString_Enum_Definition_, __VA_ARGS__)
  231. #define interface_NSString_Enum_Converters(...) VARARG(interface_NSString_Enum_Converters_, __VA_ARGS__)
  232. #define interface_NSString_Enum_DefinitionAndConverters(...) VARARG(interface_NSString_Enum_DefinitionAndConverters_, __VA_ARGS__)
  233.  
  234.  
  235. #define implementation_NSString_Enum_Converters(...) VARARG(implementation_NSString_Enum_Converters_, __VA_ARGS__)