swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: objc question


From: Marcus G. Daniels
Subject: Re: objc question
Date: 14 Jul 1998 18:59:44 -0700

>>>>> "VJ" == Vladimir Jojic <address@hidden> writes:

VJ> This would
VJ> mean that if I were to "replace" object A with object B I would
VJ> have all messages sent to object A delivered to object B.

Well, one way is with message forwarding (see below).

VJ> Example where this would be useful is: replacing object with a
VJ> proxy, that would pass somewhere else all messages delivered to
VJ> it.

What OpenStep does is to use instances of a proxy class that implement
a forwarding method (per some instance variables in that class,
e.g. the real destination object).

The code below is the basic idea, but lower level than what OpenStep
does.  Notice the effect of switching between "#if 0" and "#if 1".

#import <objc/Object.h>

static void
msg (id self, const char *string)
{
  printf ("in `%s' msg: \"%s\"\n", [self name], string);
}

@interface BaseObject: Object
{
  id delegateObject;
}
- setDelegateObject: delegateObject;
- (retval_t)forward: (SEL)sel :(arglist_t)argFrame;
- realMessage: (const char *)message;
@end

@implementation BaseObject
- setDelegateObject: anObject
{
  delegateObject = anObject;
  return self;
}

- (retval_t)forward: (SEL)sel :(arglist_t)argFrame
{
  return [delegateObject performv: sel : argFrame];
}

- realMessage: (const char *)str
{
  msg (self, str);
  
  return self;
}

#if 0
- fakeMessage: (const char *)str
{
  msg (self, str);
  
  return self;
}
#endif

@end

@interface DelegateObject: Object
- fakeMessage: (const char *)string;
@end

@implementation DelegateObject
- fakeMessage: (const char *)str
{
  msg (self, str);

  return self;
}
@end

main ()
{
  id obj = [[[BaseObject alloc] init] setDelegateObject:
                                        [[DelegateObject alloc] init]];
  [obj realMessage: [obj name]];
  [obj fakeMessage: [obj name]];
}

/*
Local Variables:
compile-command: "gcc -V 2.7.2.3 -Wno-import forward.m -o forward -lobjc"
End:
*/


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]