BKMacros.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // BKMacros.h
  3. // BlocksKit
  4. //
  5. // Includes code by Michael Ash. <https://github.com/mikeash>.
  6. //
  7. #import "NSArray+BlocksKit.h"
  8. #import "NSSet+BlocksKit.h"
  9. #import "NSDictionary+BlocksKit.h"
  10. #import "NSIndexSet+BlocksKit.h"
  11. #ifndef __BLOCKSKIT_MACROS__
  12. #define __BLOCKSKIT_MACROS__
  13. #define __BK_EACH_WRAPPER(...) (^{ __block CFMutableDictionaryRef BK_eachTable = nil; \
  14. (void)BK_eachTable; \
  15. __typeof__(__VA_ARGS__) BK_retval = __VA_ARGS__; \
  16. if(BK_eachTable) \
  17. CFRelease(BK_eachTable); \
  18. return BK_retval; \
  19. }())
  20. #define __BK_EACH_WRAPPER_VOID(...) (^{ __block CFMutableDictionaryRef BK_eachTable = nil; \
  21. (void)BK_eachTable; \
  22. __VA_ARGS__; \
  23. if(BK_eachTable) \
  24. CFRelease(BK_eachTable); \
  25. }())
  26. #define BK_EACH(collection, ...) __BK_EACH_WRAPPER_VOID([collection bk_each:^(id obj) { __VA_ARGS__ }])
  27. #define BK_MAP(collection, ...) __BK_EACH_WRAPPER([collection bk_map:^id(id obj) { return (__VA_ARGS__); }])
  28. #define BK_SELECT(collection, ...) __BK_EACH_WRAPPER([collection bk_select: ^BOOL (id obj) { return (__VA_ARGS__) != 0; }])
  29. #define BK_REJECT(collection, ...) __BK_EACH_WRAPPER([collection bk_select: ^BOOL (id obj) { return (__VA_ARGS__) == 0; }])
  30. #define BK_MATCH(collection, ...) __BK_EACH_WRAPPER([collection bk_match: ^BOOL (id obj) { return (__VA_ARGS__) != 0; }])
  31. #define BK_REDUCE(collection, initial, ...) __BK_EACH_WRAPPER([collection bk_reduce: (initial) withBlock: ^id (id a, id b) { return (__VA_ARGS__); }])
  32. // BK_APPLY is not wrapped, because we don't guarantee that the order matches the current collection during parallel execution.
  33. #define BK_APPLY(collection, ...) [collection bk_apply:^(id obj) { __VA_ARGS__ }]
  34. static inline id BKNextHelper(NSArray *array, CFMutableDictionaryRef *eachTablePtr) {
  35. if (!*eachTablePtr) {
  36. CFDictionaryKeyCallBacks keycb = {
  37. 0,
  38. kCFTypeDictionaryKeyCallBacks.retain,
  39. kCFTypeDictionaryKeyCallBacks.release,
  40. kCFTypeDictionaryKeyCallBacks.copyDescription,
  41. NULL,
  42. NULL
  43. };
  44. *eachTablePtr = CFDictionaryCreateMutable(NULL, 0, &keycb, &kCFTypeDictionaryValueCallBacks);
  45. }
  46. NSEnumerator *enumerator = (__bridge id)CFDictionaryGetValue(*eachTablePtr, (__bridge CFArrayRef)array);
  47. if (!enumerator) {
  48. enumerator = [array objectEnumerator];
  49. CFDictionarySetValue(*eachTablePtr, (__bridge CFArrayRef)array, (__bridge void *)enumerator);
  50. }
  51. return [enumerator nextObject];
  52. }
  53. #define BK_NEXT(array) BKNextHelper(array, &BK_eachTable)
  54. #ifndef EACH
  55. #define EACH BK_EACH
  56. #endif
  57. #ifndef APPLY
  58. #define APPLY BK_APPLY
  59. #endif
  60. #ifndef MAP
  61. #define MAP BK_MAP
  62. #endif
  63. #ifndef SELECT
  64. #define SELECT BK_SELECT
  65. #endif
  66. #ifndef REJECT
  67. #define REJECT BK_REJECT
  68. #endif
  69. #ifndef MATCH
  70. #define MATCH BK_MATCH
  71. #endif
  72. #ifndef REDUCE
  73. #define REDUCE BK_REDUCE
  74. #endif
  75. #ifndef NEXT
  76. #define NEXT BK_NEXT
  77. #endif
  78. #endif