-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSObject+DIInvocation.m
306 lines (266 loc) · 6.04 KB
/
NSObject+DIInvocation.m
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
// (c) 2010, Deep IT, Uncle MiF
// COMMON FILE: Common
#import "NSObject+DIInvocation.h"
#include <stdarg.h>
/* ::: Just for information ::: */
#if (defined DEBUG) && (defined VERBOSE)
#ifdef __OBJC_GC__
#warning Selected Configuration: GC Environment
#else
#warning Selected Configuration: AR-Pool Environment
#endif
#endif
#ifndef __OBJC_GC__
@interface DIInvocationProducerDeallocatorHelper : NSObject
{
id object;
}
-(id)initWithObject:(id)newObject;
-(void)dealloc;
@end
@implementation DIInvocationProducerDeallocatorHelper
-(id)initWithObject:(id)newObject
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
if (!newObject)
{
[self release];
return nil;
}
self = [super init];
if (self)
object = newObject;
return self;
}
-(void)dealloc
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
[object dealloc];
[super dealloc];
}
@end
#endif
@implementation DIInvocationProducer
+(void)load
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
}
+(void)initialize
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
}
+(id)alloc
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return NSAllocateObject(self, 0, NSDefaultMallocZone());
}
-(void)finalize
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
ivcRef = nil;
}
-(void)dealloc
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
ivcRef = nil;
if (invocation)
[invocation autorelease], invocation = nil;
NSDeallocateObject(self);
}
-(oneway void)release
{
#ifndef __OBJC_GC__
[refHelper release];
#endif
}
-(id)retain
{
#ifndef __OBJC_GC__
[refHelper retain];
#endif
return self;
}
-(id)autorelease
{
#ifndef __OBJC_GC__
[refHelper autorelease];
#endif
return self;
}
-(NSUInteger)retainCount
{
#ifndef __OBJC_GC__
return [refHelper retainCount];
#else
return 1;
#endif
}
-(void)doesNotRecognizeSelector:(SEL)aSelector
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
[NSObject doesNotRecognizeSelector:aSelector];
}
-(id)initWithTarget:(id)aTarget outInvocation:(NSInvocation* *)ref
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
if (ref)
*ref = nil;
if (!aTarget)
{
#ifndef __OBJC_GC__
[self dealloc];
#endif
return nil;
}
target = aTarget;
ivcRef = ref;
return self;
}
+(id)producerForObject:(id)aTarget
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return [self producerForObject:aTarget outInvocation:nil];
}
+(id)producerForObject:(id)aTarget outInvocation:(NSInvocation* *)ivcRef
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
if (ivcRef)
*ivcRef = nil;
if (!aTarget)
return nil;
DIInvocationProducer * producer = [[self alloc] initWithTarget:aTarget outInvocation:ivcRef];
#ifndef __OBJC_GC__
if (producer)
producer->refHelper = [[[DIInvocationProducerDeallocatorHelper alloc] initWithObject:producer] autorelease];
#endif
return producer;
}
-(NSInvocation*)producedInvocation
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return [[invocation retain] autorelease];
}
-(void)forwardInvocation:(NSInvocation *)anInvocation
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
[invocation autorelease], invocation = [anInvocation retain];
[invocation setTarget:target];
if (ivcRef)
*ivcRef = [[anInvocation retain] autorelease];
}
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return [target methodSignatureForSelector:aSelector];
}
@end
@implementation NSObject (DIInvocation)
#define DIVoidIVCCode \
NSInvocation * ivc = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:aSelector]];\
[ivc setTarget:self];\
[ivc setSelector:aSelector];\
return ivc;
#define DIIVCCodeWithArgs \
NSMethodSignature * mSig = [self methodSignatureForSelector:aSelector];\
NSInvocation * ivc = [NSInvocation invocationWithMethodSignature:mSig];\
[ivc setTarget:self];\
[ivc setSelector:aSelector];\
int argIdx = 2;\
va_list ap;\
va_start(ap,first);\
void* argVal = first;\
unsigned argsCnt = [mSig numberOfArguments];\
argsCnt -= 2;\
while (argsCnt-- && argVal)\
{\
[ivc setArgument:argVal atIndex:argIdx];\
argIdx++;\
argVal = va_arg(ap,void*);\
}\
va_end(ap);\
return ivc;
+(NSInvocation*)invocationForSelector:(SEL)aSelector
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
DIVoidIVCCode
}
-(NSInvocation*)invocationForSelector:(SEL)aSelector
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
DIVoidIVCCode
}
+(NSInvocation*)invocationForSelector:(SEL)aSelector withArguments:(void*)first,...
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
DIIVCCodeWithArgs
}
-(NSInvocation*)invocationForSelector:(SEL)aSelector withArguments:(void*)first,...
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
DIIVCCodeWithArgs
}
+(id/* DIInvocationProducer* */)producedInvocation:(NSInvocation* *)ivcRef
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return [DIInvocationProducer producerForObject:[self class] outInvocation:ivcRef];
}
-(id/* DIInvocationProducer* */)producedInvocation:(NSInvocation* *)ivcRef
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return [DIInvocationProducer producerForObject:self outInvocation:ivcRef];
}
+(id/* DIInvocationProducer* */)invocationProducer
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return [DIInvocationProducer producerForObject:[self class]];
}
-(id/* DIInvocationProducer* */)invocationProducer
{
#if (defined DEBUG) && (defined VERBOSE)
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
return [DIInvocationProducer producerForObject:self];
}
@end