API  0.9.10
CPProxy.j
Go to the documentation of this file.
1 /*
2  * CPProxy.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2009, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 @implementation CPProxy
25 {
26  Class isa;
27 }
28 
29 + (void)load
30 {
31 }
32 
33 + (void)initialize
34 {
35 }
36 
37 + (Class)class
38 {
39  return self;
40 }
41 
42 + (id)alloc
43 {
44  return class_createInstance(self);
45 }
46 
47 + (BOOL)respondsToSelector:(SEL)selector
48 {
49  return !!class_getInstanceMethod(isa, aSelector);
50 }
51 
52 - (CPMethodSignature)methodSignatureForSelector:(SEL)aSelector
53 {
54  [CPException raise:CPInvalidArgumentException
55  reason:@"-methodSignatureForSelector: called on abstract CPProxy class."];
56 }
57 
58 - (void)forwardInvocation:(CPInvocation)anInvocation
59 {
60  [CPException raise:CPInvalidArgumentException
61  reason:@"-forwardInvocation: called on abstract CPProxy class."];
62 }
63 
64 // FIXME: This should be moved to the runtime?
65 - (void)forward:(SEL)aSelector :(marg_list)args
66 {
67  return [CPObject methodForSelector:_cmd](self, _cmd, aSelector, args);
68 }
69 
70 - (unsigned)hash
71 {
72  return [self UID];
73 }
74 
75 - (unsigned)UID
76 {
77  if (typeof self._UID === "undefined")
78  self._UID = objj_generateObjectUID();
79 
80  return self._UID;
81 }
82 
83 - (BOOL)isEqual:(id)anObject
84 {
85  return self === anObject;
86 }
87 
88 - (id)self
89 {
90  return self;
91 }
92 
93 - (Class)class
94 {
95  return isa;
96 }
97 
98 - (Class)superclass
99 {
100  return class_getSuperclass(isa);
101 }
102 
103 - (id)performSelector:(SEL)aSelector
104 {
105  return self.isa.objj_msgSend0(self, aSelector);
106 }
107 
108 - (id)performSelector:(SEL)aSelector withObject:(id)anObject
109 {
110  return self.isa.objj_msgSend1(self, aSelector, anObject);
111 }
112 
113 - (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject
114 {
115  return self.isa.objj_msgSend2(self, aSelector, anObject, anotherObject);
116 }
117 
118 - (BOOL)isProxy
119 {
120  return YES;
121 }
122 
123 - (BOOL)isKindOfClass:(Class)aClass
124 {
125  var signature = [self methodSignatureForSelector:_cmd],
126  invocation = [CPInvocation invocationWithMethodSignature:signature];
127 
128  [self forwardInvocation:invocation];
129 
130  return [invocation returnValue];
131 }
132 
133 - (BOOL)isMemberOfClass:(Class)aClass
134 {
135  var signature = [self methodSignatureForSelector:_cmd],
136  invocation = [CPInvocation invocationWithMethodSignature:signature];
137 
138  [self forwardInvocation:invocation];
139 
140  return [invocation returnValue];
141 }
142 
143 - (BOOL)respondsToSelector:(SEL)aSelector
144 {
145  var signature = [self methodSignatureForSelector:_cmd],
146  invocation = [CPInvocation invocationWithMethodSignature:signature];
147 
148  [self forwardInvocation:invocation];
149 
150  return [invocation returnValue];
151 }
152 
154 {
155  return "<" + class_getName(isa) + " 0x" + [CPString stringWithHash:[self UID]] + ">";
156 }
157 
158 @end
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
unsigned hash()
Definition: CPProxy.j:70
Class superclass()
Definition: CPProxy.j:98
id invocationWithMethodSignature:(CPMethodSignature aMethodSignature)
Definition: CPInvocation.j:43
var isEqual
Class class()
Definition: CPProxy.j:37
CFData prototype isa
Definition: CPData.j:214
Definition: CPProxy.h:2
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
CPMethodSignature methodSignatureForSelector:(SEL aSelector)
Definition: CPProxy.j:52
CPString description()
Definition: CPProxy.j:153
Class isa
Definition: CPProxy.h:5
An object representation of a message.
Definition: CPInvocation.h:2
IMP methodForSelector:(SEL aSelector)
Definition: CPObject.j:328
An immutable string (collection of characters).
Definition: CPString.h:2
BOOL isProxy()
Definition: CPProxy.j:118
void forwardInvocation:(CPInvocation anInvocation)
Definition: CPProxy.j:58
id stringWithHash:(unsigned aHash)
Definition: CPString.j:107
id self()
Definition: CPProxy.j:88
unsigned UID()
Definition: CPProxy.j:75
id alloc()
Definition: CPProxy.j:42
void initialize()
Definition: CPProxy.j:33
void load()
Definition: CPProxy.j:29