swarm-support
[Top][All Lists]
Advanced

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

delegation


From: Rick Riolo
Subject: delegation
Date: Sat, 26 Sep 1998 11:40:49 -0400 (EDT)

I believe the example below shows how to delegate from a
collection, in this case, a List.
The basic idea is MyList has a List instance variable
which it creates as a List.
Then one adds a -forward:: method to MyList,
plus what other methods one wanted.
And of course one could add other IV's to MyList.
As you can see, there is not that much that needs to
be done versus regular subclassing (ie just add this
boilerplate forward method).  It does add some
inefficiency, but I guess we knew there would be some
of that using objectiveC!

Note that this works as long as list is created via:
   list = [List create: scratchZone]; 
but I tried it also using
   list = [List create: self];
which I think is supposed to work for (at least)
non-delegation cases (but correct me if I'm wrong).
When I do it this way, the list iv says it does NOT
respondTo variaous methods (eg allocIVars: in the create phase,
or even addLast: in the using phase), even though if
one picks those off in -forward and sends them to list,
it does handle them ok.   I gather when the list is
created this way, there is some more trickery going on
behind the scenes that confuses respondsTo.

 - r

Rick Riolo                           address@hidden
Program for Study of Complex Systems (PSCS)
4477 Randall Lab                
University of Michigan         Ann Arbor MI 48109-1120
Phone: 734 763 3323                  Fax: 734 763 9267
http://www.pscs.umich.edu/PEOPLE/rlr-home.html

---- main.m -----------------------------
#import <stdlib.h>

#import <simtools.h>
#import <simtoolsgui.h>
#import <collections.h>

#import "MyList.h"

int main(int argc, const char ** argv) {
  id  bugList;

  initSwarm(argc, argv);

  bugList = [MyList create: globalZone];

  [bugList printSelf];
  [bugList addLast: (id) 42];
  [bugList printSelf];

  return 0;
}

---- MyList.h -----------------------------
#import <objectbase/SwarmObject.h>      

@interface MyList: SwarmObject {
  id          list;
}

-createEnd;

-(void) printSelf;

@end

----- MyList.m ------------------------------

#import <stdlib.h>
#import <strings.h>

#import "MyList.h"
#import "collections.h"

@implementation MyList

-createEnd { 
  [super createEnd];
  // This fails with MyList does not recognize addLast: !!
  // list = [List create: self]; 
  list = [List create: scratchZone]; // This works too: globalZone];   
  return self;
}

-(void) printSelf {
    printf( "\n-printSelf: getCount %d\n\n", [list getCount] );
}

-(retval_t) forward: (SEL) aSelector : (arglist_t) argFrame {

    fprintf(stderr,"--> forward for class '%s', message '%s'.\n",
                        [[self class] name], sel_get_name( aSelector ) );

        if ( [list respondsTo: aSelector] ) {
                fprintf(stderr, "-*-> forward to list...\n" );
                return( [list performv: aSelector : argFrame] );
        }

        fprintf(stderr,"=*=> forwarding failed!\n" );
        [self doesNotRecognize: aSelector];
}

@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]