A2BlockInvocation.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // A2BlockInvocation.h
  3. // BlocksKit
  4. //
  5. #import <Foundation/Foundation.h>
  6. /// If a block invocation is instiated with an invalid method signature,
  7. /// an `NSInvalidArgumentException` is thrown containing this key in the
  8. /// user info.
  9. extern NSString *const A2IncompatibleMethodSignatureKey;
  10. /** An `A2BlockInvocation` is an Objective-C block call rendered static, that
  11. is, it is an action turned into an object. `A2BlockInvocation` objects are used
  12. to store and forward closure invocations between objects, primarily by the
  13. `A2DynamicDelegate` system.
  14. A block invocation object can be repeatedly dispatched with multiple sets of
  15. arguments with the same flexibility as NSInvocation. This design makes
  16. `A2BlockInvocation` extremely useful, because blocks can be used to capture
  17. scope and a block invocation can be used to save it for later.
  18. Like `NSInvocation`, `A2BlockInvocation` does not support invocations of
  19. methods with variadic arguments or union arguments.
  20. */
  21. @interface A2BlockInvocation : NSObject
  22. /** Inspects the given block literal and returns a compatible method signature.
  23. The returned method signature is suitable for use in the Foundation forwarding
  24. system to link a method call to a block invocation.
  25. @param block An Objective-C block literal
  26. @return A method signature matching the declared prototype for the block
  27. */
  28. + (NSMethodSignature *)methodSignatureForBlock:(id)block;
  29. /** @name Creating A2BlockInvocation Objects */
  30. /** Returns a block invocation object able to construct calls to a given block.
  31. This method synthesizes a compatible method signature for the given block.
  32. @param block A block literal
  33. @return An initialized block invocation object
  34. @see methodSignatureForBlock
  35. */
  36. - (instancetype)initWithBlock:(id)block;
  37. /** Returns a block invocation object able to construct calls to a given block
  38. using a given Objective-C method signature.
  39. The method signature given must be compatible with the signature of the block;
  40. that is, equal to the block signature but with a `SEL` (`':'`) as the second
  41. parameter. Passing in an incompatible method signature will raise an exception.
  42. An example method returning a string for an integer argument would have the
  43. following properties:
  44. - Block type signature of `NSString *(^)(int)`
  45. - Block function definition of `NSString *(*)(id, int)`
  46. - Block signature of `"@@?i"`
  47. - Method signature of `"@:i"` or `"@i"`
  48. @param block An Objective-C block literal
  49. @param methodSignature An method signature matching the block
  50. @return An initialized block invocation object
  51. */
  52. - (instancetype)initWithBlock:(id)block methodSignature:(NSMethodSignature *)methodSignature;
  53. /** @name Getting the Block and Method Signatures */
  54. /// The receiver's method signature, reflecting the block with a selector.
  55. /// Appropriate for use in `-methodSignatureForSelector:`.
  56. @property (nonatomic, strong, readonly) NSMethodSignature *methodSignature;
  57. /// Returns the receiver's block.
  58. @property (nonatomic, copy, readonly) id block;
  59. /** Calls the receiver's block with the arguments from the given invocation,
  60. providing a buffer containing the block's return value upon return.
  61. @param inv An instance of NSInvocation with values for its arguments set.
  62. @param returnValue On return, the block's return value, or `nil` for a void
  63. return type.
  64. @see invokeWithInvocation:returnValue:
  65. */
  66. - (BOOL)invokeWithInvocation:(NSInvocation *)inv returnValue:(out NSValue **)returnValue;
  67. /** Calls the receiver's block with the arguments from the given invocation
  68. and sets the return value on the given invocation.
  69. @param inv An instance of NSInvocation with values for its arguments set.
  70. @see invokeWithInvocation:returnValue:
  71. */
  72. - (void)invokeWithInvocation:(NSInvocation *)inv;
  73. @end