RACTuple.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // RACTuple.h
  3. // ReactiveCocoa
  4. //
  5. // Created by Josh Abernathy on 4/12/12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "metamacros.h"
  10. @class RACSequence;
  11. /// Creates a new tuple with the given values. At least one value must be given.
  12. /// Values can be nil.
  13. #define RACTuplePack(...) \
  14. RACTuplePack_(__VA_ARGS__)
  15. /// Declares new object variables and unpacks a RACTuple into them.
  16. ///
  17. /// This macro should be used on the left side of an assignment, with the
  18. /// tuple on the right side. Nothing else should appear on the same line, and the
  19. /// macro should not be the only statement in a conditional or loop body.
  20. ///
  21. /// If the tuple has more values than there are variables listed, the excess
  22. /// values are ignored.
  23. ///
  24. /// If the tuple has fewer values than there are variables listed, the excess
  25. /// variables are initialized to nil.
  26. ///
  27. /// Examples
  28. ///
  29. /// RACTupleUnpack(NSString *string, NSNumber *num) = [RACTuple tupleWithObjects:@"foo", @5, nil];
  30. /// NSLog(@"string: %@", string);
  31. /// NSLog(@"num: %@", num);
  32. ///
  33. /// /* The above is equivalent to: */
  34. /// RACTuple *t = [RACTuple tupleWithObjects:@"foo", @5, nil];
  35. /// NSString *string = t[0];
  36. /// NSNumber *num = t[1];
  37. /// NSLog(@"string: %@", string);
  38. /// NSLog(@"num: %@", num);
  39. #define RACTupleUnpack(...) \
  40. RACTupleUnpack_(__VA_ARGS__)
  41. /// A sentinel object that represents nils in the tuple.
  42. ///
  43. /// It should never be necessary to create a tuple nil yourself. Just use
  44. /// +tupleNil.
  45. @interface RACTupleNil : NSObject <NSCopying, NSCoding>
  46. /// A singleton instance.
  47. + (RACTupleNil *)tupleNil;
  48. @end
  49. /// A tuple is an ordered collection of objects. It may contain nils, represented
  50. /// by RACTupleNil.
  51. @interface RACTuple : NSObject <NSCoding, NSCopying, NSFastEnumeration>
  52. @property (nonatomic, readonly) NSUInteger count;
  53. /// These properties all return the object at that index or nil if the number of
  54. /// objects is less than the index.
  55. @property (nonatomic, readonly) id first;
  56. @property (nonatomic, readonly) id second;
  57. @property (nonatomic, readonly) id third;
  58. @property (nonatomic, readonly) id fourth;
  59. @property (nonatomic, readonly) id fifth;
  60. @property (nonatomic, readonly) id last;
  61. /// Creates a new tuple out of the array. Does not convert nulls to nils.
  62. + (instancetype)tupleWithObjectsFromArray:(NSArray *)array;
  63. /// Creates a new tuple out of the array. If `convert` is YES, it also converts
  64. /// every NSNull to RACTupleNil.
  65. + (instancetype)tupleWithObjectsFromArray:(NSArray *)array convertNullsToNils:(BOOL)convert;
  66. /// Creates a new tuple with the given objects. Use RACTupleNil to represent
  67. /// nils.
  68. + (instancetype)tupleWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;
  69. /// Returns the object at `index` or nil if the object is a RACTupleNil. Unlike
  70. /// NSArray and friends, it's perfectly fine to ask for the object at an index
  71. /// past the tuple's count - 1. It will simply return nil.
  72. - (id)objectAtIndex:(NSUInteger)index;
  73. /// Returns an array of all the objects. RACTupleNils are converted to NSNulls.
  74. - (NSArray *)allObjects;
  75. /// Appends `obj` to the receiver.
  76. ///
  77. /// obj - The object to add to the tuple. This argument may be nil.
  78. ///
  79. /// Returns a new tuple.
  80. - (instancetype)tupleByAddingObject:(id)obj;
  81. @end
  82. @interface RACTuple (RACSequenceAdditions)
  83. /// Returns a sequence of all the objects. RACTupleNils are converted to NSNulls.
  84. @property (nonatomic, copy, readonly) RACSequence *rac_sequence;
  85. @end
  86. @interface RACTuple (ObjectSubscripting)
  87. /// Returns the object at that index or nil if the number of objects is less
  88. /// than the index.
  89. - (id)objectAtIndexedSubscript:(NSUInteger)idx;
  90. @end
  91. /// This and everything below is for internal use only.
  92. ///
  93. /// See RACTuplePack() and RACTupleUnpack() instead.
  94. #define RACTuplePack_(...) \
  95. ([RACTuple tupleWithObjectsFromArray:@[ metamacro_foreach(RACTuplePack_object_or_ractuplenil,, __VA_ARGS__) ]])
  96. #define RACTuplePack_object_or_ractuplenil(INDEX, ARG) \
  97. (ARG) ?: RACTupleNil.tupleNil,
  98. #define RACTupleUnpack_(...) \
  99. metamacro_foreach(RACTupleUnpack_decl,, __VA_ARGS__) \
  100. \
  101. int RACTupleUnpack_state = 0; \
  102. \
  103. RACTupleUnpack_after: \
  104. ; \
  105. metamacro_foreach(RACTupleUnpack_assign,, __VA_ARGS__) \
  106. if (RACTupleUnpack_state != 0) RACTupleUnpack_state = 2; \
  107. \
  108. while (RACTupleUnpack_state != 2) \
  109. if (RACTupleUnpack_state == 1) { \
  110. goto RACTupleUnpack_after; \
  111. } else \
  112. for (; RACTupleUnpack_state != 1; RACTupleUnpack_state = 1) \
  113. [RACTupleUnpackingTrampoline trampoline][ @[ metamacro_foreach(RACTupleUnpack_value,, __VA_ARGS__) ] ]
  114. #define RACTupleUnpack_state metamacro_concat(RACTupleUnpack_state, __LINE__)
  115. #define RACTupleUnpack_after metamacro_concat(RACTupleUnpack_after, __LINE__)
  116. #define RACTupleUnpack_loop metamacro_concat(RACTupleUnpack_loop, __LINE__)
  117. #define RACTupleUnpack_decl_name(INDEX) \
  118. metamacro_concat(metamacro_concat(RACTupleUnpack, __LINE__), metamacro_concat(_var, INDEX))
  119. #define RACTupleUnpack_decl(INDEX, ARG) \
  120. __strong id RACTupleUnpack_decl_name(INDEX);
  121. #define RACTupleUnpack_assign(INDEX, ARG) \
  122. __strong ARG = RACTupleUnpack_decl_name(INDEX);
  123. #define RACTupleUnpack_value(INDEX, ARG) \
  124. [NSValue valueWithPointer:&RACTupleUnpack_decl_name(INDEX)],
  125. @interface RACTupleUnpackingTrampoline : NSObject
  126. + (instancetype)trampoline;
  127. - (void)setObject:(RACTuple *)tuple forKeyedSubscript:(NSArray *)variables;
  128. @end