KSVarArgs.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // KSVarArgs.h
  3. //
  4. // Created by Karl Stenerud on 2012-08-19.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. /* KSVarArgs is a set of macros designed to make dealing with variable arguments
  27. * easier in Objective-C. All macros assume that the varargs list contains only
  28. * objective-c objects or object-like structures (assignable to type id).
  29. *
  30. * The base macro ksva_iterate_list() iterates over the variable arguments,
  31. * invoking a block for each argument, until it encounters a terminating nil.
  32. *
  33. * The other macros are for convenience when converting to common collections.
  34. */
  35. /** Block type used by ksva_iterate_list.
  36. *
  37. * @param entry The current argument in the vararg list.
  38. */
  39. typedef void (^KSVA_Block)(id entry);
  40. /**
  41. * Iterate over a va_list, executing the specified code block for each entry.
  42. *
  43. * @param FIRST_ARG_NAME The name of the first argument in the vararg list.
  44. * @param BLOCK A code block of type KSVA_Block.
  45. */
  46. #define ksva_iterate_list(FIRST_ARG_NAME, BLOCK) \
  47. { \
  48. KSVA_Block ksva_block = BLOCK; \
  49. va_list ksva_args; \
  50. va_start(ksva_args,FIRST_ARG_NAME); \
  51. for(id ksva_arg = FIRST_ARG_NAME; ksva_arg != nil; ksva_arg = va_arg(ksva_args, id)) \
  52. { \
  53. ksva_block(ksva_arg); \
  54. } \
  55. va_end(ksva_args); \
  56. }
  57. /**
  58. * Convert a variable argument list into an array. An autoreleased
  59. * NSMutableArray will be created in the current scope with the specified name.
  60. *
  61. * @param FIRST_ARG_NAME The name of the first argument in the vararg list.
  62. * @param ARRAY_NAME The name of the array to create in the current scope.
  63. */
  64. #define ksva_list_to_nsarray(FIRST_ARG_NAME, ARRAY_NAME) \
  65. NSMutableArray* ARRAY_NAME = [NSMutableArray array]; \
  66. ksva_iterate_list(FIRST_ARG_NAME, ^(id entry) \
  67. { \
  68. [ARRAY_NAME addObject:entry]; \
  69. })
  70. /**
  71. * Convert a variable argument list into a dictionary, interpreting the vararg
  72. * list as object, key, object, key, ...
  73. * An autoreleased NSMutableDictionary will be created in the current scope with
  74. * the specified name.
  75. *
  76. * @param FIRST_ARG_NAME The name of the first argument in the vararg list.
  77. * @param DICT_NAME The name of the dictionary to create in the current scope.
  78. */
  79. #define ksva_list_to_nsdictionary(FIRST_ARG_NAME, DICT_NAME) \
  80. NSMutableDictionary* DICT_NAME = [NSMutableDictionary dictionary]; \
  81. { \
  82. __block id ksva_object = nil; \
  83. ksva_iterate_list(FIRST_ARG_NAME, ^(id entry) \
  84. { \
  85. if(ksva_object == nil) \
  86. { \
  87. ksva_object = entry; \
  88. } \
  89. else \
  90. { \
  91. [DICT_NAME setObject:ksva_object forKey:entry]; \
  92. ksva_object = nil; \
  93. } \
  94. }); \
  95. }