RACTuple.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // RACTuple.h
  3. // ReactiveObjC
  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 "RACmetamacros.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. @class RACTwoTuple<__covariant First, __covariant Second>;
  42. @class RACThreeTuple<__covariant First, __covariant Second, __covariant Third>;
  43. @class RACFourTuple<__covariant First, __covariant Second, __covariant Third, __covariant Fourth>;
  44. @class RACFiveTuple<__covariant First, __covariant Second, __covariant Third, __covariant Fourth, __covariant Fifth>;
  45. NS_ASSUME_NONNULL_BEGIN
  46. /// A sentinel object that represents nils in the tuple.
  47. ///
  48. /// It should never be necessary to create a tuple nil yourself. Just use
  49. /// +tupleNil.
  50. @interface RACTupleNil : NSObject <NSCopying, NSCoding>
  51. /// A singleton instance.
  52. + (RACTupleNil *)tupleNil;
  53. @end
  54. /// A tuple is an ordered collection of objects. It may contain nils, represented
  55. /// by RACTupleNil.
  56. @interface RACTuple : NSObject <NSCoding, NSCopying, NSFastEnumeration>
  57. @property (nonatomic, readonly) NSUInteger count;
  58. /// These properties all return the object at that index or nil if the number of
  59. /// objects is less than the index.
  60. @property (nonatomic, readonly, nullable) id first;
  61. @property (nonatomic, readonly, nullable) id second;
  62. @property (nonatomic, readonly, nullable) id third;
  63. @property (nonatomic, readonly, nullable) id fourth;
  64. @property (nonatomic, readonly, nullable) id fifth;
  65. @property (nonatomic, readonly, nullable) id last;
  66. /// Creates a new tuple out of the array. Does not convert nulls to nils.
  67. + (instancetype)tupleWithObjectsFromArray:(NSArray *)array;
  68. /// Creates a new tuple out of the array. If `convert` is YES, it also converts
  69. /// every NSNull to RACTupleNil.
  70. + (instancetype)tupleWithObjectsFromArray:(NSArray *)array convertNullsToNils:(BOOL)convert;
  71. /// Creates a new tuple with the given objects. Use RACTupleNil to represent
  72. /// nils.
  73. + (instancetype)tupleWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;
  74. /// Returns the object at `index` or nil if the object is a RACTupleNil. Unlike
  75. /// NSArray and friends, it's perfectly fine to ask for the object at an index
  76. /// past the tuple's count - 1. It will simply return nil.
  77. - (nullable id)objectAtIndex:(NSUInteger)index;
  78. /// Returns an array of all the objects. RACTupleNils are converted to NSNulls.
  79. - (NSArray *)allObjects;
  80. /// Appends `obj` to the receiver.
  81. ///
  82. /// obj - The object to add to the tuple. This argument may be nil.
  83. ///
  84. /// Returns a new tuple.
  85. - (__kindof RACTuple *)tupleByAddingObject:(nullable id)obj;
  86. @end
  87. @interface RACTuple (RACSequenceAdditions)
  88. /// Returns a sequence of all the objects. RACTupleNils are converted to NSNulls.
  89. @property (nonatomic, copy, readonly) RACSequence *rac_sequence;
  90. @end
  91. @interface RACTuple (ObjectSubscripting)
  92. /// Returns the object at that index or nil if the number of objects is less
  93. /// than the index.
  94. - (nullable id)objectAtIndexedSubscript:(NSUInteger)idx;
  95. @end
  96. /// A tuple with exactly one generic value.
  97. @interface RACOneTuple<__covariant First> : RACTuple
  98. + (instancetype)tupleWithObjects:(id)object, ... __attribute((unavailable("Use pack: instead.")));
  99. - (RACTwoTuple<First, id> *)tupleByAddingObject:(nullable id)obj;
  100. /// Creates a new tuple with the given values.
  101. + (RACOneTuple<First> *)pack:(nullable First)first;
  102. @property (nonatomic, readonly, nullable) First first;
  103. @end
  104. /// A tuple with exactly two generic values.
  105. @interface RACTwoTuple<__covariant First, __covariant Second> : RACTuple
  106. + (instancetype)tupleWithObjects:(id)object, ... __attribute((unavailable("Use pack:: instead.")));
  107. - (RACThreeTuple<First, Second, id> *)tupleByAddingObject:(nullable id)obj;
  108. /// Creates a new tuple with the given value.
  109. + (RACTwoTuple<First, Second> *)pack:(nullable First)first :(nullable Second)second;
  110. @property (nonatomic, readonly, nullable) First first;
  111. @property (nonatomic, readonly, nullable) Second second;
  112. @end
  113. /// A tuple with exactly three generic values.
  114. @interface RACThreeTuple<__covariant First, __covariant Second, __covariant Third> : RACTuple
  115. + (instancetype)tupleWithObjects:(id)object, ... __attribute((unavailable("Use pack::: instead.")));
  116. - (RACFourTuple<First, Second, Third, id> *)tupleByAddingObject:(nullable id)obj;
  117. /// Creates a new tuple with the given values.
  118. + (instancetype)pack:(nullable First)first :(nullable Second)second :(nullable Third)third;
  119. @property (nonatomic, readonly, nullable) First first;
  120. @property (nonatomic, readonly, nullable) Second second;
  121. @property (nonatomic, readonly, nullable) Third third;
  122. @end
  123. /// A tuple with exactly four generic values.
  124. @interface RACFourTuple<__covariant First, __covariant Second, __covariant Third, __covariant Fourth> : RACTuple
  125. + (instancetype)tupleWithObjects:(id)object, ... __attribute((unavailable("Use pack:::: instead.")));
  126. - (RACFiveTuple<First, Second, Third, Fourth, id> *)tupleByAddingObject:(nullable id)obj;
  127. /// Creates a new tuple with the given values.
  128. + (instancetype)pack:(nullable First)first :(nullable Second)second :(nullable Third)third :(nullable Fourth)fourth;
  129. @property (nonatomic, readonly, nullable) First first;
  130. @property (nonatomic, readonly, nullable) Second second;
  131. @property (nonatomic, readonly, nullable) Third third;
  132. @property (nonatomic, readonly, nullable) Fourth fourth;
  133. @end
  134. /// A tuple with exactly five generic values.
  135. @interface RACFiveTuple<__covariant First, __covariant Second, __covariant Third, __covariant Fourth, __covariant Fifth> : RACTuple
  136. + (instancetype)tupleWithObjects:(id)object, ... __attribute((unavailable("Use pack::::: instead.")));
  137. /// Creates a new tuple with the given values.
  138. + (instancetype)pack:(nullable First)first :(nullable Second)second :(nullable Third)third :(nullable Fourth)fourth :(nullable Fifth)fifth;
  139. @property (nonatomic, readonly, nullable) First first;
  140. @property (nonatomic, readonly, nullable) Second second;
  141. @property (nonatomic, readonly, nullable) Third third;
  142. @property (nonatomic, readonly, nullable) Fourth fourth;
  143. @property (nonatomic, readonly, nullable) Fifth fifth;
  144. @end
  145. /// This and everything below is for internal use only.
  146. ///
  147. /// See RACTuplePack() and RACTupleUnpack() instead.
  148. #define RACTuplePack_(...) \
  149. ([RACTuplePack_class_name(__VA_ARGS__) tupleWithObjectsFromArray:@[ metamacro_foreach(RACTuplePack_object_or_ractuplenil,, __VA_ARGS__) ]])
  150. #define RACTuplePack_object_or_ractuplenil(INDEX, ARG) \
  151. (ARG) ?: RACTupleNil.tupleNil,
  152. /// Returns the class that should be used to create a tuple with the provided
  153. /// variadic arguments to RACTuplePack_(). Supports up to 20 arguments.
  154. #define RACTuplePack_class_name(...) \
  155. metamacro_at(20, __VA_ARGS__, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACTuple, RACFiveTuple, RACFourTuple, RACThreeTuple, RACTwoTuple, RACOneTuple)
  156. #define RACTupleUnpack_(...) \
  157. metamacro_foreach(RACTupleUnpack_decl,, __VA_ARGS__) \
  158. \
  159. int RACTupleUnpack_state = 0; \
  160. \
  161. RACTupleUnpack_after: \
  162. ; \
  163. metamacro_foreach(RACTupleUnpack_assign,, __VA_ARGS__) \
  164. if (RACTupleUnpack_state != 0) RACTupleUnpack_state = 2; \
  165. \
  166. while (RACTupleUnpack_state != 2) \
  167. if (RACTupleUnpack_state == 1) { \
  168. goto RACTupleUnpack_after; \
  169. } else \
  170. for (; RACTupleUnpack_state != 1; RACTupleUnpack_state = 1) \
  171. [RACTupleUnpackingTrampoline trampoline][ @[ metamacro_foreach(RACTupleUnpack_value,, __VA_ARGS__) ] ]
  172. #define RACTupleUnpack_state metamacro_concat(RACTupleUnpack_state, __LINE__)
  173. #define RACTupleUnpack_after metamacro_concat(RACTupleUnpack_after, __LINE__)
  174. #define RACTupleUnpack_loop metamacro_concat(RACTupleUnpack_loop, __LINE__)
  175. #define RACTupleUnpack_decl_name(INDEX) \
  176. metamacro_concat(metamacro_concat(RACTupleUnpack, __LINE__), metamacro_concat(_var, INDEX))
  177. #define RACTupleUnpack_decl(INDEX, ARG) \
  178. __strong id RACTupleUnpack_decl_name(INDEX);
  179. #define RACTupleUnpack_assign(INDEX, ARG) \
  180. __strong ARG = RACTupleUnpack_decl_name(INDEX);
  181. #define RACTupleUnpack_value(INDEX, ARG) \
  182. [NSValue valueWithPointer:&RACTupleUnpack_decl_name(INDEX)],
  183. @interface RACTupleUnpackingTrampoline : NSObject
  184. + (instancetype)trampoline;
  185. - (void)setObject:(nullable RACTuple *)tuple forKeyedSubscript:(NSArray *)variables;
  186. @end
  187. NS_ASSUME_NONNULL_END