GINArgument.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseDynamicLinks/Sources/GINInvocation/GINArgument.h"
  17. // Currently only supporting arguments of types id and integer.
  18. // Will support more argument types when it is needed.
  19. typedef NS_ENUM(NSUInteger, GINArgumentType) {
  20. kGINArgumentTypeObject = 0,
  21. kGINArgumentTypeInteger
  22. };
  23. @interface GINArgument ()
  24. @property(nonatomic, assign) GINArgumentType type;
  25. @property(nonatomic, strong) id object;
  26. @property(nonatomic, assign) NSInteger integer;
  27. @end
  28. @implementation GINArgument
  29. + (instancetype)argumentWithObject:(NSObject *)object {
  30. GINArgument *arg = [[GINArgument alloc] init];
  31. arg.type = kGINArgumentTypeObject;
  32. arg.object = object;
  33. return arg;
  34. }
  35. + (instancetype)argumentWithInteger:(NSInteger)integer {
  36. GINArgument *arg = [[GINArgument alloc] init];
  37. arg.type = kGINArgumentTypeInteger;
  38. arg.integer = integer;
  39. return arg;
  40. }
  41. + (BOOL)setNextArgumentInList:(va_list)argumentList
  42. atIndex:(NSUInteger)index
  43. inInvocation:(NSInvocation *)invocation {
  44. id argument = va_arg(argumentList, id);
  45. if (!argument) {
  46. return NO;
  47. }
  48. if (![argument isKindOfClass:[GINArgument class]]) {
  49. [NSException raise:@"InvalidArgumentException"
  50. format:@"Invalid argument type at index %lu", (unsigned long)index];
  51. }
  52. [argument setArgumentInInvocation:invocation atIndex:index];
  53. return YES;
  54. }
  55. - (void)setArgumentInInvocation:(NSInvocation *)invocation atIndex:(NSUInteger)index {
  56. switch (self.type) {
  57. case kGINArgumentTypeObject:
  58. [invocation setArgument:&_object atIndex:index];
  59. break;
  60. case kGINArgumentTypeInteger:
  61. [invocation setArgument:&_integer atIndex:index];
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. @end