FROM : Simon Strandgaard
DATE : Sat Aug 26 19:57:09 2006
On 8/26/06, Julien Jalon <<email_removed>> wrote:
> On 8/26/06, Nick Zitzmann <<email_removed>> wrote:
> >
> > On Aug 26, 2006, at 3:23 AM, Simon Strandgaard wrote:
> >
> > > How does one make a transparent proxy?
> >
> > Subclassing NSProxy and overriding -forwardInvocation: ought to work...
[snip]
Ok, I am now inheriting from NSProxy... code is below.
[snip]
> Some notes about NSProxy:
> 1) don't need to call [super init] (NSProxy has no init'er...)
> 2) also implement:
> - methodSignatureForSelector:
> - conformsToProtocol:
> - and, if you want to, respondsToSelector:
I have just seen Julien's reply.. I am about to leave my computer for
some hours.
I see that I need to impl comformsToProtocol and respondToSelector.
If I don't override class: (and superclass: as well) then I an exception
when I invoke [obj class]. Should'nt this be forwarded through the proxy?
*** Uncaught exception: <NSInvalidArgumentException> *** -[NSProxy
methodSignatureForSelector:] called!
Are these methods enough to do a transparent proxy?
methodSignatureForSelector:
forwardInvocation:
comformsToProtocol:
respondToSelector:
--
Simon Strandgaard
// main.mm
#include <Foundation/Foundation.h>
@interface TransparentProxy : NSProxy {
id _proxied_object;
}
@end
@implementation TransparentProxy
-(id)initWithProxiedObject:(id)object {
NSLog(@"[proxy.init]\n");
_proxied_object = object;
return self;
}
-(NSMethodSignature*)methodSignatureForSelector:(SEL)sel {
NSLog(@"[proxy.msfs] methodSignatureForSelector \"%@\"\n",
NSStringFromSelector(sel));
NSMethodSignature* sig = nil;
sig = [_proxied_object methodSignatureForSelector:sel];
if(sig) {
NSLog(@"[proxy.msfs] recognized by proxied_object\n");
return sig;
}
sig = [NSObject methodSignatureForSelector:@selector(self)];
NSLog(@"[proxy.msfs] ERROR: selector not recognized, absorbing!\n");
return sig;
}
-(void)forwardInvocation:(NSInvocation*)inv {
SEL sel = [inv selector];
NSLog(@"[proxy.fi] forwardInvocation \"%@\"\n",
NSStringFromSelector(sel));
if([_proxied_object respondsToSelector:sel]) {
NSLog(@"[proxy.fi] invoking with proxy\n");
[inv invokeWithTarget:_proxied_object];
return;
}
NSLog(@"[proxy.fi] ERROR: absorbing!\n");
}
-(Class)class {
NSLog(@"[proxy.class]\n");
return [_proxied_object class];
}
-(Class)superclass {
NSLog(@"[proxy.superclass]\n");
return [_proxied_object superclass];
}
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSNumber* num = [[NSNumber alloc] initWithInt:42];
id obj = [[TransparentProxy alloc] initWithProxiedObject:num];
NSLog(@"---------------\n");
NSLog(@"[obj xintValue]\n");
int i = [obj xintValue];
NSLog(@"i=%i\n", i);
NSLog(@"---------------\n");
NSLog(@"[obj setValue:5]\n");
[obj setValue:5];
NSLog(@"---------------\n");
NSLog(@"[obj intValue]\n");
int j = [obj intValue];
NSLog(@"j=%i\n", j);
NSLog(@"---------------\n");
NSLog(@"instance=%@\n", obj);
NSLog(@"---------------\n");
NSLog(@"class=%@\n", [obj class]);
NSLog(@"---------------\n");
NSLog(@"superclass=%@\n", [obj superclass]);
NSLog(@"---------------\n");
NSLog(@"[obj self]\n");
NSLog(@"self=%@\n", [obj self]);
NSLog(@"---------------\n");
NSLog(@"[obj respondsToSelector:@selector(dontUnderstand)];\n");
[obj respondsToSelector:@selector(dontUnderstand)];
NSLog(@"---------------\n");
NSLog(@"[obj performSelector:@selector(dontUnderstand)];\n");
[obj performSelector:@selector(dontUnderstand)];
NSLog(@"---------------\n");
NSLog(@"[obj isKindOfClass: [NSCell class]];\n");
BOOL b1 = [obj isKindOfClass: [NSCell class]];
NSLog(@"is same: %s\n", (b1 ? "yes" : "no"));
NSLog(@"---------------\n");
NSLog(@"[obj isKindOfClass: [NSNumber class]];\n");
BOOL b2 = [obj isKindOfClass: [NSNumber class]];
NSLog(@"is same: %s\n", (b2 ? "yes" : "no"));
NSLog(@"---------------\n");
return 0;
}
DATE : Sat Aug 26 19:57:09 2006
On 8/26/06, Julien Jalon <<email_removed>> wrote:
> On 8/26/06, Nick Zitzmann <<email_removed>> wrote:
> >
> > On Aug 26, 2006, at 3:23 AM, Simon Strandgaard wrote:
> >
> > > How does one make a transparent proxy?
> >
> > Subclassing NSProxy and overriding -forwardInvocation: ought to work...
[snip]
Ok, I am now inheriting from NSProxy... code is below.
[snip]
> Some notes about NSProxy:
> 1) don't need to call [super init] (NSProxy has no init'er...)
> 2) also implement:
> - methodSignatureForSelector:
> - conformsToProtocol:
> - and, if you want to, respondsToSelector:
I have just seen Julien's reply.. I am about to leave my computer for
some hours.
I see that I need to impl comformsToProtocol and respondToSelector.
If I don't override class: (and superclass: as well) then I an exception
when I invoke [obj class]. Should'nt this be forwarded through the proxy?
*** Uncaught exception: <NSInvalidArgumentException> *** -[NSProxy
methodSignatureForSelector:] called!
Are these methods enough to do a transparent proxy?
methodSignatureForSelector:
forwardInvocation:
comformsToProtocol:
respondToSelector:
--
Simon Strandgaard
// main.mm
#include <Foundation/Foundation.h>
@interface TransparentProxy : NSProxy {
id _proxied_object;
}
@end
@implementation TransparentProxy
-(id)initWithProxiedObject:(id)object {
NSLog(@"[proxy.init]\n");
_proxied_object = object;
return self;
}
-(NSMethodSignature*)methodSignatureForSelector:(SEL)sel {
NSLog(@"[proxy.msfs] methodSignatureForSelector \"%@\"\n",
NSStringFromSelector(sel));
NSMethodSignature* sig = nil;
sig = [_proxied_object methodSignatureForSelector:sel];
if(sig) {
NSLog(@"[proxy.msfs] recognized by proxied_object\n");
return sig;
}
sig = [NSObject methodSignatureForSelector:@selector(self)];
NSLog(@"[proxy.msfs] ERROR: selector not recognized, absorbing!\n");
return sig;
}
-(void)forwardInvocation:(NSInvocation*)inv {
SEL sel = [inv selector];
NSLog(@"[proxy.fi] forwardInvocation \"%@\"\n",
NSStringFromSelector(sel));
if([_proxied_object respondsToSelector:sel]) {
NSLog(@"[proxy.fi] invoking with proxy\n");
[inv invokeWithTarget:_proxied_object];
return;
}
NSLog(@"[proxy.fi] ERROR: absorbing!\n");
}
-(Class)class {
NSLog(@"[proxy.class]\n");
return [_proxied_object class];
}
-(Class)superclass {
NSLog(@"[proxy.superclass]\n");
return [_proxied_object superclass];
}
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSNumber* num = [[NSNumber alloc] initWithInt:42];
id obj = [[TransparentProxy alloc] initWithProxiedObject:num];
NSLog(@"---------------\n");
NSLog(@"[obj xintValue]\n");
int i = [obj xintValue];
NSLog(@"i=%i\n", i);
NSLog(@"---------------\n");
NSLog(@"[obj setValue:5]\n");
[obj setValue:5];
NSLog(@"---------------\n");
NSLog(@"[obj intValue]\n");
int j = [obj intValue];
NSLog(@"j=%i\n", j);
NSLog(@"---------------\n");
NSLog(@"instance=%@\n", obj);
NSLog(@"---------------\n");
NSLog(@"class=%@\n", [obj class]);
NSLog(@"---------------\n");
NSLog(@"superclass=%@\n", [obj superclass]);
NSLog(@"---------------\n");
NSLog(@"[obj self]\n");
NSLog(@"self=%@\n", [obj self]);
NSLog(@"---------------\n");
NSLog(@"[obj respondsToSelector:@selector(dontUnderstand)];\n");
[obj respondsToSelector:@selector(dontUnderstand)];
NSLog(@"---------------\n");
NSLog(@"[obj performSelector:@selector(dontUnderstand)];\n");
[obj performSelector:@selector(dontUnderstand)];
NSLog(@"---------------\n");
NSLog(@"[obj isKindOfClass: [NSCell class]];\n");
BOOL b1 = [obj isKindOfClass: [NSCell class]];
NSLog(@"is same: %s\n", (b1 ? "yes" : "no"));
NSLog(@"---------------\n");
NSLog(@"[obj isKindOfClass: [NSNumber class]];\n");
BOOL b2 = [obj isKindOfClass: [NSNumber class]];
NSLog(@"is same: %s\n", (b2 ? "yes" : "no"));
NSLog(@"---------------\n");
return 0;
}
| Related mails | Author | Date |
|---|---|---|
| Simon Strandgaard | Aug 26, 11:23 | |
| Nick Zitzmann | Aug 26, 19:28 | |
| Julien Jalon | Aug 26, 19:33 | |
| Simon Strandgaard | Aug 26, 19:57 |






Cocoa mail archive

