forked from Kentzo/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSNewlyAllocatedObject.m
More file actions
73 lines (57 loc) · 1.75 KB
/
FSNewlyAllocatedObject.m
File metadata and controls
73 lines (57 loc) · 1.75 KB
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
/* FSNewlyAllocatedObject.m Copyright (c) 2009 Philippe Mougin. */
/* This software is open source. See the license. */
#import "FSNewlyAllocatedObject.h"
#import <objc/objc-runtime.h>
@implementation FSNewlyAllocatedObject
+ (id)newlyAllocatedObjectWithTarget:(id)theTarget
{
return [[[self alloc] initWithTarget:theTarget] autorelease];
}
- (NSString *)description
{
return [[@"Proxy for a newly allocated " stringByAppendingString:NSStringFromClass(object_getClass(target))] stringByAppendingString:@". Don't forget to initialize it and to use the object returned by the init... method instead of this proxy." ];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
[anInvocation setTarget:target];
[anInvocation invoke];
return;
}
/*
NSProxy (10.6) unfortunately does not support fast forwarding (i.e., forwardingTargetForSelector:)
If it does in a future version, we will be able to replace forwardInvocation: and methodSignatureForSelector: by the method below
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return target;
}
*/
- (id)initWithTarget:(id)theTarget
{
retainCount = 1;
// We do not retain the target, as we stand for an object that has just been allocated but not yet initialized
// Retaining an uninitialized object is not a good idea, in particular because its retain count is not yet initialized
target = theTarget;
return self;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
return [target methodSignatureForSelector:aSelector];
}
- (NSString *)printString
{
return [self description];
}
- (id)retain
{
retainCount++;
return self;
}
- (NSUInteger)retainCount
{
return retainCount;
}
- (void)release
{
if (--retainCount == 0) [self dealloc];
}
@end