NSInvocation+OCMAdditions.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //---------------------------------------------------------------------------------------
  2. // $Id$
  3. // Copyright (c) 2006-2009 by Mulle Kybernetik. See License file for details.
  4. //---------------------------------------------------------------------------------------
  5. #import "NSInvocation+OCMAdditions.h"
  6. @implementation NSInvocation(OCMAdditions)
  7. - (id)getArgumentAtIndexAsObject:(int)argIndex
  8. {
  9. const char* argType;
  10. argType = [[self methodSignature] getArgumentTypeAtIndex:argIndex];
  11. while(strchr("rnNoORV", argType[0]) != NULL)
  12. argType += 1;
  13. if((strlen(argType) > 1) && (strchr("{^", argType[0]) == NULL) && (strcmp("@?", argType) != 0))
  14. [NSException raise:NSInvalidArgumentException format:@"Cannot handle argument type '%s'.", argType];
  15. switch (argType[0])
  16. {
  17. case '#':
  18. case '@':
  19. {
  20. __unsafe_unretained id value;
  21. [self getArgument:&value atIndex:argIndex];
  22. return value;
  23. }
  24. case ':':
  25. {
  26. SEL s = (SEL)0;
  27. [self getArgument:&s atIndex:argIndex];
  28. id value = NSStringFromSelector(s);
  29. return value;
  30. }
  31. case 'i':
  32. {
  33. int value;
  34. [self getArgument:&value atIndex:argIndex];
  35. return @(value);
  36. }
  37. case 's':
  38. {
  39. short value;
  40. [self getArgument:&value atIndex:argIndex];
  41. return @(value);
  42. }
  43. case 'l':
  44. {
  45. long value;
  46. [self getArgument:&value atIndex:argIndex];
  47. return @(value);
  48. }
  49. case 'q':
  50. {
  51. long long value;
  52. [self getArgument:&value atIndex:argIndex];
  53. return @(value);
  54. }
  55. case 'c':
  56. {
  57. char value;
  58. [self getArgument:&value atIndex:argIndex];
  59. return @(value);
  60. }
  61. case 'C':
  62. {
  63. unsigned char value;
  64. [self getArgument:&value atIndex:argIndex];
  65. return @(value);
  66. }
  67. case 'I':
  68. {
  69. unsigned int value;
  70. [self getArgument:&value atIndex:argIndex];
  71. return @(value);
  72. }
  73. case 'S':
  74. {
  75. unsigned short value;
  76. [self getArgument:&value atIndex:argIndex];
  77. return @(value);
  78. }
  79. case 'L':
  80. {
  81. unsigned long value;
  82. [self getArgument:&value atIndex:argIndex];
  83. return @(value);
  84. }
  85. case 'Q':
  86. {
  87. unsigned long long value;
  88. [self getArgument:&value atIndex:argIndex];
  89. return @(value);
  90. }
  91. case 'f':
  92. {
  93. float value;
  94. [self getArgument:&value atIndex:argIndex];
  95. return @(value);
  96. }
  97. case 'd':
  98. {
  99. double value;
  100. [self getArgument:&value atIndex:argIndex];
  101. return @(value);
  102. }
  103. case 'B':
  104. {
  105. bool value;
  106. [self getArgument:&value atIndex:argIndex];
  107. return @(value);
  108. }
  109. case '^':
  110. {
  111. void *value = NULL;
  112. [self getArgument:&value atIndex:argIndex];
  113. return [NSValue valueWithPointer:value];
  114. }
  115. case '*':
  116. {
  117. char *value = NULL;
  118. [self getArgument:&value atIndex:argIndex];
  119. return [NSValue valueWithPointer:value];
  120. }
  121. case '{': // structure
  122. {
  123. NSUInteger maxArgSize = [[self methodSignature] frameLength];
  124. NSMutableData *argumentData = [[NSMutableData alloc] initWithLength:maxArgSize];
  125. [self getArgument:[argumentData mutableBytes] atIndex:argIndex];
  126. return [NSValue valueWithBytes:[argumentData bytes] objCType:argType];
  127. }
  128. }
  129. [NSException raise:NSInvalidArgumentException format:@"Argument type '%s' not supported", argType];
  130. return nil;
  131. }
  132. - (NSString *)invocationDescription
  133. {
  134. NSMethodSignature *methodSignature = [self methodSignature];
  135. NSUInteger numberOfArgs = [methodSignature numberOfArguments];
  136. if (numberOfArgs == 2)
  137. return NSStringFromSelector([self selector]);
  138. NSArray *selectorParts = [NSStringFromSelector([self selector]) componentsSeparatedByString:@":"];
  139. NSMutableString *description = [[NSMutableString alloc] init];
  140. unsigned int i;
  141. for(i = 2; i < numberOfArgs; i++)
  142. {
  143. [description appendFormat:@"%@%@:", (i > 2 ? @" " : @""), selectorParts[(i - 2)]];
  144. [description appendString:[self argumentDescriptionAtIndex:i]];
  145. }
  146. return description;
  147. }
  148. - (NSString *)argumentDescriptionAtIndex:(int)argIndex
  149. {
  150. const char *argType = [[self methodSignature] getArgumentTypeAtIndex:argIndex];
  151. if(strchr("rnNoORV", argType[0]) != NULL)
  152. argType += 1;
  153. switch(*argType)
  154. {
  155. case '@': return [self objectDescriptionAtIndex:argIndex];
  156. case 'c': return [self charDescriptionAtIndex:argIndex];
  157. case 'C': return [self unsignedCharDescriptionAtIndex:argIndex];
  158. case 'i': return [self intDescriptionAtIndex:argIndex];
  159. case 'I': return [self unsignedIntDescriptionAtIndex:argIndex];
  160. case 's': return [self shortDescriptionAtIndex:argIndex];
  161. case 'S': return [self unsignedShortDescriptionAtIndex:argIndex];
  162. case 'l': return [self longDescriptionAtIndex:argIndex];
  163. case 'L': return [self unsignedLongDescriptionAtIndex:argIndex];
  164. case 'q': return [self longLongDescriptionAtIndex:argIndex];
  165. case 'Q': return [self unsignedLongLongDescriptionAtIndex:argIndex];
  166. case 'd': return [self doubleDescriptionAtIndex:argIndex];
  167. case 'f': return [self floatDescriptionAtIndex:argIndex];
  168. // Why does this throw EXC_BAD_ACCESS when appending the string?
  169. // case NSObjCStructType: return [self structDescriptionAtIndex:index];
  170. case '^': return [self pointerDescriptionAtIndex:argIndex];
  171. case '*': return [self cStringDescriptionAtIndex:argIndex];
  172. case ':': return [self selectorDescriptionAtIndex:argIndex];
  173. default: return [@"<??" stringByAppendingString:@">"]; // avoid confusion with trigraphs...
  174. }
  175. }
  176. - (NSString *)objectDescriptionAtIndex:(int)anInt
  177. {
  178. __unsafe_unretained id object;
  179. [self getArgument:&object atIndex:anInt];
  180. if (object == nil)
  181. return @"nil";
  182. else if(![object isProxy] && [object isKindOfClass:[NSString class]])
  183. return [NSString stringWithFormat:@"@\"%@\"", [object description]];
  184. else
  185. return [object description];
  186. }
  187. - (NSString *)charDescriptionAtIndex:(int)anInt
  188. {
  189. unsigned char buffer[128];
  190. memset(buffer, 0x0, 128);
  191. [self getArgument:&buffer atIndex:anInt];
  192. // If there's only one character in the buffer, and it's 0 or 1, then we have a BOOL
  193. if (buffer[1] == '\0' && (buffer[0] == 0 || buffer[0] == 1))
  194. return [NSString stringWithFormat:@"%@", (buffer[0] == 1 ? @"YES" : @"NO")];
  195. else
  196. return [NSString stringWithFormat:@"'%c'", *buffer];
  197. }
  198. - (NSString *)unsignedCharDescriptionAtIndex:(int)anInt
  199. {
  200. unsigned char buffer[128];
  201. memset(buffer, 0x0, 128);
  202. [self getArgument:&buffer atIndex:anInt];
  203. return [NSString stringWithFormat:@"'%c'", *buffer];
  204. }
  205. - (NSString *)intDescriptionAtIndex:(int)anInt
  206. {
  207. int intValue;
  208. [self getArgument:&intValue atIndex:anInt];
  209. return [NSString stringWithFormat:@"%d", intValue];
  210. }
  211. - (NSString *)unsignedIntDescriptionAtIndex:(int)anInt
  212. {
  213. unsigned int intValue;
  214. [self getArgument:&intValue atIndex:anInt];
  215. return [NSString stringWithFormat:@"%d", intValue];
  216. }
  217. - (NSString *)shortDescriptionAtIndex:(int)anInt
  218. {
  219. short shortValue;
  220. [self getArgument:&shortValue atIndex:anInt];
  221. return [NSString stringWithFormat:@"%hi", shortValue];
  222. }
  223. - (NSString *)unsignedShortDescriptionAtIndex:(int)anInt
  224. {
  225. unsigned short shortValue;
  226. [self getArgument:&shortValue atIndex:anInt];
  227. return [NSString stringWithFormat:@"%hu", shortValue];
  228. }
  229. - (NSString *)longDescriptionAtIndex:(int)anInt
  230. {
  231. long longValue;
  232. [self getArgument:&longValue atIndex:anInt];
  233. return [NSString stringWithFormat:@"%ld", longValue];
  234. }
  235. - (NSString *)unsignedLongDescriptionAtIndex:(int)anInt
  236. {
  237. unsigned long longValue;
  238. [self getArgument:&longValue atIndex:anInt];
  239. return [NSString stringWithFormat:@"%lu", longValue];
  240. }
  241. - (NSString *)longLongDescriptionAtIndex:(int)anInt
  242. {
  243. long long longLongValue;
  244. [self getArgument:&longLongValue atIndex:anInt];
  245. return [NSString stringWithFormat:@"%qi", longLongValue];
  246. }
  247. - (NSString *)unsignedLongLongDescriptionAtIndex:(int)anInt
  248. {
  249. unsigned long long longLongValue;
  250. [self getArgument:&longLongValue atIndex:anInt];
  251. return [NSString stringWithFormat:@"%qu", longLongValue];
  252. }
  253. - (NSString *)doubleDescriptionAtIndex:(int)anInt;
  254. {
  255. double doubleValue;
  256. [self getArgument:&doubleValue atIndex:anInt];
  257. return [NSString stringWithFormat:@"%f", doubleValue];
  258. }
  259. - (NSString *)floatDescriptionAtIndex:(int)anInt
  260. {
  261. float floatValue;
  262. [self getArgument:&floatValue atIndex:anInt];
  263. return [NSString stringWithFormat:@"%f", floatValue];
  264. }
  265. - (NSString *)structDescriptionAtIndex:(int)anInt;
  266. {
  267. void *buffer;
  268. [self getArgument:&buffer atIndex:anInt];
  269. return [NSString stringWithFormat:@":(struct)%p", buffer];
  270. }
  271. - (NSString *)pointerDescriptionAtIndex:(int)anInt
  272. {
  273. void *buffer;
  274. [self getArgument:&buffer atIndex:anInt];
  275. return [NSString stringWithFormat:@"%p", buffer];
  276. }
  277. - (NSString *)cStringDescriptionAtIndex:(int)anInt
  278. {
  279. char buffer[128];
  280. memset(buffer, 0x0, 128);
  281. [self getArgument:&buffer atIndex:anInt];
  282. return [NSString stringWithFormat:@"\"%s\"", buffer];
  283. }
  284. - (NSString *)selectorDescriptionAtIndex:(int)anInt
  285. {
  286. SEL selectorValue;
  287. [self getArgument:&selectorValue atIndex:anInt];
  288. return [NSString stringWithFormat:@"@selector(%@)", NSStringFromSelector(selectorValue)];
  289. }
  290. @end