NSInvocation+OCMAdditions.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2006-2015 Erik Doernenburg and contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use these files except in compliance with the License. You may obtain
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. #import "NSInvocation+OCMAdditions.h"
  17. #import "OCMFunctions.h"
  18. @implementation NSInvocation(OCMAdditions)
  19. - (BOOL)hasCharPointerArgument
  20. {
  21. NSMethodSignature *signature = [self methodSignature];
  22. for(NSUInteger i = 0; i < [signature numberOfArguments]; i++)
  23. {
  24. const char *argType = OCMTypeWithoutQualifiers([signature getArgumentTypeAtIndex:i]);
  25. if(strcmp(argType, "*") == 0)
  26. return YES;
  27. }
  28. return NO;
  29. }
  30. - (id)getArgumentAtIndexAsObject:(NSInteger)argIndex
  31. {
  32. const char *argType = OCMTypeWithoutQualifiers([[self methodSignature] getArgumentTypeAtIndex:(NSUInteger)argIndex]);
  33. if((strlen(argType) > 1) && (strchr("{^", argType[0]) == NULL) && (strcmp("@?", argType) != 0))
  34. [NSException raise:NSInvalidArgumentException format:@"Cannot handle argument type '%s'.", argType];
  35. if(OCMIsObjectType(argType))
  36. {
  37. id value;
  38. [self getArgument:&value atIndex:argIndex];
  39. return value;
  40. }
  41. switch(argType[0])
  42. {
  43. case ':':
  44. {
  45. SEL s = (SEL)0;
  46. [self getArgument:&s atIndex:argIndex];
  47. return [NSValue valueWithBytes:&s objCType:":"];
  48. }
  49. case 'i':
  50. {
  51. int value;
  52. [self getArgument:&value atIndex:argIndex];
  53. return @(value);
  54. }
  55. case 's':
  56. {
  57. short value;
  58. [self getArgument:&value atIndex:argIndex];
  59. return @(value);
  60. }
  61. case 'l':
  62. {
  63. long value;
  64. [self getArgument:&value atIndex:argIndex];
  65. return @(value);
  66. }
  67. case 'q':
  68. {
  69. long long value;
  70. [self getArgument:&value atIndex:argIndex];
  71. return @(value);
  72. }
  73. case 'c':
  74. {
  75. char value;
  76. [self getArgument:&value atIndex:argIndex];
  77. return @(value);
  78. }
  79. case 'C':
  80. {
  81. unsigned char value;
  82. [self getArgument:&value atIndex:argIndex];
  83. return @(value);
  84. }
  85. case 'I':
  86. {
  87. unsigned int value;
  88. [self getArgument:&value atIndex:argIndex];
  89. return @(value);
  90. }
  91. case 'S':
  92. {
  93. unsigned short value;
  94. [self getArgument:&value atIndex:argIndex];
  95. return @(value);
  96. }
  97. case 'L':
  98. {
  99. unsigned long value;
  100. [self getArgument:&value atIndex:argIndex];
  101. return @(value);
  102. }
  103. case 'Q':
  104. {
  105. unsigned long long value;
  106. [self getArgument:&value atIndex:argIndex];
  107. return @(value);
  108. }
  109. case 'f':
  110. {
  111. float value;
  112. [self getArgument:&value atIndex:argIndex];
  113. return @(value);
  114. }
  115. case 'd':
  116. {
  117. double value;
  118. [self getArgument:&value atIndex:argIndex];
  119. return @(value);
  120. }
  121. case 'D':
  122. {
  123. long double value;
  124. [self getArgument:&value atIndex:argIndex];
  125. return [NSValue valueWithBytes:&value objCType:@encode(__typeof__(value))];
  126. }
  127. case 'B':
  128. {
  129. bool value;
  130. [self getArgument:&value atIndex:argIndex];
  131. return @(value);
  132. }
  133. case '^':
  134. case '*':
  135. {
  136. void *value = NULL;
  137. [self getArgument:&value atIndex:argIndex];
  138. return [NSValue valueWithPointer:value];
  139. }
  140. case '{': // structure
  141. {
  142. NSUInteger argSize;
  143. NSGetSizeAndAlignment([[self methodSignature] getArgumentTypeAtIndex:(NSUInteger)argIndex], &argSize, NULL);
  144. if(argSize == 0) // TODO: Can this happen? Is frameLength a good choice in that case?
  145. argSize = [[self methodSignature] frameLength];
  146. NSMutableData *argumentData = [[[NSMutableData alloc] initWithLength:argSize] autorelease];
  147. [self getArgument:[argumentData mutableBytes] atIndex:argIndex];
  148. return [NSValue valueWithBytes:[argumentData bytes] objCType:argType];
  149. }
  150. }
  151. [NSException raise:NSInvalidArgumentException format:@"Argument type '%s' not supported", argType];
  152. return nil;
  153. }
  154. - (NSString *)invocationDescription
  155. {
  156. NSMethodSignature *methodSignature = [self methodSignature];
  157. NSUInteger numberOfArgs = [methodSignature numberOfArguments];
  158. if (numberOfArgs == 2)
  159. return NSStringFromSelector([self selector]);
  160. NSArray *selectorParts = [NSStringFromSelector([self selector]) componentsSeparatedByString:@":"];
  161. NSMutableString *description = [[NSMutableString alloc] init];
  162. NSUInteger i;
  163. for(i = 2; i < numberOfArgs; i++)
  164. {
  165. [description appendFormat:@"%@%@:", (i > 2 ? @" " : @""), [selectorParts objectAtIndex:(i - 2)]];
  166. [description appendString:[self argumentDescriptionAtIndex:(NSInteger)i]];
  167. }
  168. return [description autorelease];
  169. }
  170. - (NSString *)argumentDescriptionAtIndex:(NSInteger)argIndex
  171. {
  172. const char *argType = OCMTypeWithoutQualifiers([[self methodSignature] getArgumentTypeAtIndex:(NSUInteger)argIndex]);
  173. switch(*argType)
  174. {
  175. case '@': return [self objectDescriptionAtIndex:argIndex];
  176. case 'B': return [self boolDescriptionAtIndex:argIndex];
  177. case 'c': return [self charDescriptionAtIndex:argIndex];
  178. case 'C': return [self unsignedCharDescriptionAtIndex:argIndex];
  179. case 'i': return [self intDescriptionAtIndex:argIndex];
  180. case 'I': return [self unsignedIntDescriptionAtIndex:argIndex];
  181. case 's': return [self shortDescriptionAtIndex:argIndex];
  182. case 'S': return [self unsignedShortDescriptionAtIndex:argIndex];
  183. case 'l': return [self longDescriptionAtIndex:argIndex];
  184. case 'L': return [self unsignedLongDescriptionAtIndex:argIndex];
  185. case 'q': return [self longLongDescriptionAtIndex:argIndex];
  186. case 'Q': return [self unsignedLongLongDescriptionAtIndex:argIndex];
  187. case 'd': return [self doubleDescriptionAtIndex:argIndex];
  188. case 'f': return [self floatDescriptionAtIndex:argIndex];
  189. case 'D': return [self longDoubleDescriptionAtIndex:argIndex];
  190. case '{': return [self structDescriptionAtIndex:argIndex];
  191. case '^': return [self pointerDescriptionAtIndex:argIndex];
  192. case '*': return [self cStringDescriptionAtIndex:argIndex];
  193. case ':': return [self selectorDescriptionAtIndex:argIndex];
  194. default: return [@"<??" stringByAppendingString:@">"]; // avoid confusion with trigraphs...
  195. }
  196. }
  197. - (NSString *)objectDescriptionAtIndex:(NSInteger)anInt
  198. {
  199. id object;
  200. [self getArgument:&object atIndex:anInt];
  201. if (object == nil)
  202. return @"nil";
  203. else if(![object isProxy] && [object isKindOfClass:[NSString class]])
  204. return [NSString stringWithFormat:@"@\"%@\"", [object description]];
  205. else
  206. // The description cannot be nil, if it is then replace it
  207. return [object description] ?: @"<nil description>";
  208. }
  209. - (NSString *)boolDescriptionAtIndex:(NSInteger)anInt
  210. {
  211. bool value;
  212. [self getArgument:&value atIndex:anInt];
  213. return value? @"YES" : @"NO";
  214. }
  215. - (NSString *)charDescriptionAtIndex:(NSInteger)anInt
  216. {
  217. unsigned char buffer[128];
  218. memset(buffer, 0x0, 128);
  219. [self getArgument:&buffer atIndex:anInt];
  220. // If there's only one character in the buffer, and it's 0 or 1, then we have a BOOL
  221. if (buffer[1] == '\0' && (buffer[0] == 0 || buffer[0] == 1))
  222. return (buffer[0] == 1 ? @"YES" : @"NO");
  223. else
  224. return [NSString stringWithFormat:@"'%c'", *buffer];
  225. }
  226. - (NSString *)unsignedCharDescriptionAtIndex:(NSInteger)anInt
  227. {
  228. unsigned char buffer[128];
  229. memset(buffer, 0x0, 128);
  230. [self getArgument:&buffer atIndex:anInt];
  231. return [NSString stringWithFormat:@"'%c'", *buffer];
  232. }
  233. - (NSString *)intDescriptionAtIndex:(NSInteger)anInt
  234. {
  235. int intValue;
  236. [self getArgument:&intValue atIndex:anInt];
  237. return [NSString stringWithFormat:@"%d", intValue];
  238. }
  239. - (NSString *)unsignedIntDescriptionAtIndex:(NSInteger)anInt
  240. {
  241. unsigned int intValue;
  242. [self getArgument:&intValue atIndex:anInt];
  243. return [NSString stringWithFormat:@"%d", intValue];
  244. }
  245. - (NSString *)shortDescriptionAtIndex:(NSInteger)anInt
  246. {
  247. short shortValue;
  248. [self getArgument:&shortValue atIndex:anInt];
  249. return [NSString stringWithFormat:@"%hi", shortValue];
  250. }
  251. - (NSString *)unsignedShortDescriptionAtIndex:(NSInteger)anInt
  252. {
  253. unsigned short shortValue;
  254. [self getArgument:&shortValue atIndex:anInt];
  255. return [NSString stringWithFormat:@"%hu", shortValue];
  256. }
  257. - (NSString *)longDescriptionAtIndex:(NSInteger)anInt
  258. {
  259. long longValue;
  260. [self getArgument:&longValue atIndex:anInt];
  261. return [NSString stringWithFormat:@"%ld", longValue];
  262. }
  263. - (NSString *)unsignedLongDescriptionAtIndex:(NSInteger)anInt
  264. {
  265. unsigned long longValue;
  266. [self getArgument:&longValue atIndex:anInt];
  267. return [NSString stringWithFormat:@"%lu", longValue];
  268. }
  269. - (NSString *)longLongDescriptionAtIndex:(NSInteger)anInt
  270. {
  271. long long longLongValue;
  272. [self getArgument:&longLongValue atIndex:anInt];
  273. return [NSString stringWithFormat:@"%qi", longLongValue];
  274. }
  275. - (NSString *)unsignedLongLongDescriptionAtIndex:(NSInteger)anInt
  276. {
  277. unsigned long long longLongValue;
  278. [self getArgument:&longLongValue atIndex:anInt];
  279. return [NSString stringWithFormat:@"%qu", longLongValue];
  280. }
  281. - (NSString *)doubleDescriptionAtIndex:(NSInteger)anInt
  282. {
  283. double doubleValue;
  284. [self getArgument:&doubleValue atIndex:anInt];
  285. return [NSString stringWithFormat:@"%f", doubleValue];
  286. }
  287. - (NSString *)floatDescriptionAtIndex:(NSInteger)anInt
  288. {
  289. float floatValue;
  290. [self getArgument:&floatValue atIndex:anInt];
  291. return [NSString stringWithFormat:@"%f", floatValue];
  292. }
  293. - (NSString *)longDoubleDescriptionAtIndex:(NSInteger)anInt
  294. {
  295. long double longDoubleValue;
  296. [self getArgument:&longDoubleValue atIndex:anInt];
  297. return [NSString stringWithFormat:@"%Lf", longDoubleValue];
  298. }
  299. - (NSString *)structDescriptionAtIndex:(NSInteger)anInt
  300. {
  301. return [NSString stringWithFormat:@"(%@)", [[self getArgumentAtIndexAsObject:anInt] description]];
  302. }
  303. - (NSString *)pointerDescriptionAtIndex:(NSInteger)anInt
  304. {
  305. void *buffer;
  306. [self getArgument:&buffer atIndex:anInt];
  307. return [NSString stringWithFormat:@"%p", buffer];
  308. }
  309. - (NSString *)cStringDescriptionAtIndex:(NSInteger)anInt
  310. {
  311. char buffer[104];
  312. char *cStringPtr;
  313. [self getArgument:&cStringPtr atIndex:anInt];
  314. strlcpy(buffer, cStringPtr, sizeof(buffer));
  315. strlcpy(buffer + 100, "...", (sizeof(buffer) - 100));
  316. return [NSString stringWithFormat:@"\"%s\"", buffer];
  317. }
  318. - (NSString *)selectorDescriptionAtIndex:(NSInteger)anInt
  319. {
  320. SEL selectorValue;
  321. [self getArgument:&selectorValue atIndex:anInt];
  322. return [NSString stringWithFormat:@"@selector(%@)", NSStringFromSelector(selectorValue)];
  323. }
  324. @end