• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Forwarding to Super
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Forwarding to Super


  • Subject: Re: Forwarding to Super
  • From: Herr Witten <email@hidden>
  • Date: Sun, 4 Jul 2004 13:57:32 -0400

On 4 Jul 2004, at 3:57, John Randolph wrote:

> The thing I have to wonder here, is why you want to break
> encapsulation and message super from *outside* the class in question?

Sometimes encapsulation is not well thought out, so workarounds are
needed.

I have included a more complete implementation, which handles
structures as return types and has other changes:

//
// SuperProxy.h
//
// Michael Witten on Thur 1 July 2004 43470 85
// Copyright (c) 2004 Michael Witten. All rights reserved.
// NOTE: You may reuse this code as long as the copyright remains
intact.

@interface SuperProxy : NSObject
{
id _theTarget;
Class _theClass;
Class _theSuperclass;
BOOL _isObject;
}
+ (id)superProxyWithTarget: (id)theTarget;

- (id)initWithTarget: (id)theTarget;
@end

//
// SuperProxy.m
//
// Michael Witten on Thur 1 July 2004 43470 85
// Copyright (c) 2004 Michael Witten. All rights reserved.
// NOTE: You may reuse this code as long as the copyright remains
intact.

#import "SuperProxy.h"
#import <objc/objc-runtime.h>

static Method _method;
static IMP _method_imp_old;

id _imp_proxy(id theReceiver, SEL theSelector, ...)
{
struct objc_super superContext;
superContext.receiver = theReceiver;
superContext.class = [theReceiver superclass];

void* args = __builtin_apply_args();

void* ptr = &superContext;

memmove((int*)args + 1, &ptr, sizeof(int*));

_method->method_imp = _method_imp_old;

__builtin_return(__builtin_apply((void(*)())objc_msgSendSuper, args,
method_getSizeOfArguments(_method)));
}

void _imp_proxy_stret(void* stretAddr, id theReceiver, SEL theSelector,
...)
{
struct objc_super superContext;
superContext.receiver = theReceiver;
superContext.class = [theReceiver superclass];

void* args = __builtin_apply_args();

void* ptr = &superContext;

memmove((int*)args + 2, &ptr, sizeof(int*));

_method->method_imp = _method_imp_old;

__builtin_apply((void(*)())objc_msgSendSuper_stret, args,
method_getSizeOfArguments(_method));
}

@implementation SuperProxy
+ (id)superProxyWithTarget: (id)theTarget
{
return [[[[SuperProxy alloc] initWithTarget: theTarget] retain]
autorelease];
}

- (id)initWithTarget: (id)theTarget
{
self = [super init];
if (self)
{
_theTarget = [theTarget retain];
_theClass = [_theTarget class];
_theSuperclass = [_theTarget superclass];
_isObject = [_theTarget isMemberOfClass: _theClass];
}

return self;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
return [_theSuperclass instanceMethodSignatureForSelector: aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
SEL theSelector = [anInvocation selector];

_method = (_isObject) ? class_getInstanceMethod(_theClass,
theSelector) : class_getClassMethod(_theClass, theSelector);

_method_imp_old = _method->method_imp;

const char* type = [[anInvocation methodSignature] methodReturnType];
switch (type[0])
{
case _C_ARY_B:
case _C_UNION_B:
case _C_STRUCT_B:
_method->method_imp = (IMP)_imp_proxy_stret;
break;
default:
_method->method_imp = _imp_proxy;
break;
}

[anInvocation invokeWithTarget: _theTarget];
}

- (void)dealloc
{
[_theTarget release];

[super dealloc];
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: Forwarding to Super
      • From: "Louis C. Sacha" <email@hidden>
References: 
 >Re: Forwarding to Super (From: Herr Witten <email@hidden>)
 >Re: Forwarding to Super (From: "Louis C. Sacha" <email@hidden>)
 >Re: Forwarding to Super (From: Herr Witten <email@hidden>)
 >Re: Forwarding to Super (From: John Randolph <email@hidden>)

  • Prev by Date: Re: Newbie: I can't see what is wrong?
  • Next by Date: Re: Action isn't sent
  • Previous by thread: Re: Forwarding to Super
  • Next by thread: Re: Forwarding to Super
  • Index(es):
    • Date
    • Thread