API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CPInvocation.j
Go to the documentation of this file.
1 /*
2  * CPInvocation.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 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 
31 @implementation CPInvocation : CPObject
32 {
33  id _returnValue;
34  CPMutableArray _arguments;
35  CPMethodSignature _methodSignature;
36 }
37 
38 // Creating CPInvocation Objects
44 + (id)invocationWithMethodSignature:(CPMethodSignature)aMethodSignature
45 {
46  return [[self alloc] initWithMethodSignature:aMethodSignature];
47 }
48 
54 - (id)initWithMethodSignature:(CPMethodSignature)aMethodSignature
55 {
56  self = [super init];
57 
58  if (self)
59  {
60  _arguments = [];
61  _methodSignature = aMethodSignature;
62  }
63 
64  return self;
65 }
66 
67 // Configuring an Invocation Object
72 - (void)setSelector:(SEL)aSelector
73 {
74  _arguments[1] = aSelector;
75 }
76 
80 - (SEL)selector
81 {
82  return _arguments[1];
83 }
84 
89 - (void)setTarget:(id)aTarget
90 {
91  _arguments[0] = aTarget;
92 }
93 
97 - (id)target
98 {
99  return _arguments[0];
100 }
101 
107 - (void)setArgument:(id)anArgument atIndex:(CPUInteger)anIndex
108 {
109  _arguments[anIndex] = anArgument;
110 }
111 
118 - (id)argumentAtIndex:(CPUInteger)anIndex
119 {
120  return _arguments[anIndex];
121 }
122 
127 - (void)setReturnValue:(id)aReturnValue
128 {
129  _returnValue = aReturnValue;
130 }
131 
135 - (id)returnValue
136 {
137  return _returnValue;
138 }
139 
140 // Dispatching an Invocation
144 - (void)invoke
145 {
146  _returnValue = objj_msgSend.apply(objj_msgSend, _arguments);
147 }
148 
153 - (void)invokeWithTarget:(id)aTarget
154 {
155  _arguments[0] = aTarget;
156  _returnValue = objj_msgSend.apply(objj_msgSend, _arguments);
157 }
158 
159 @end
160 
161 var CPInvocationArguments = @"CPInvocationArguments",
162  CPInvocationReturnValue = @"CPInvocationReturnValue";
163 
164 @implementation CPInvocation (CPCoding)
165 
171 - (id)initWithCoder:(CPCoder)aCoder
172 {
173  self = [super init];
174 
175  if (self)
176  {
177  _returnValue = [aCoder decodeObjectForKey:CPInvocationReturnValue];
178  _arguments = [aCoder decodeObjectForKey:CPInvocationArguments];
179  }
180 
181  return self;
182 }
183 
188 - (void)encodeWithCoder:(CPCoder)aCoder
189 {
190  [aCoder encodeObject:_returnValue forKey:CPInvocationReturnValue];
191  [aCoder encodeObject:_arguments forKey:CPInvocationArguments];
192 }
193 
194 @end