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