http://www.santafe.edu/projects/swarm/archive/list-archive.9904/0119.html /* The two main points are: 1) Use of addRef:withArgument: and notifyFunction 2) Conservative use of remove: in notifyFunction [apologies to Stevie Wonder] */ #import #import #import @interface TargetObject: SwarmObject - printTime; @end @implementation TargetObject - printTime { printf (">TargetObject@%lu\n", getCurrentTime ()); return self; } @end @interface Controller: SwarmObject { id schedule; id targetObject; } + createBegin: aZone; - (id )getSchedule; - scheduleStuff; - (void)dropTarget; - ponderTheUniverse; - go; @end static void notifyFunction (id obj, id unused, void *arg) { id controller = (id) arg; id schedule = [controller getSchedule]; id action; do { id index = [schedule begin: scratchZone]; while ((action = [index next])) { if ((timeval_t) [index getKey] > getCurrentTime ()) { if ([action respondsTo: M(getTarget)]) if (obj == [action getTarget]) { printf (">Removal of ActionTo@%lu! (target=%s)\n", (timeval_t) [index getKey], [[action getTarget] name]); [schedule remove: action]; break; } } } [index drop]; } while (action); } @implementation Controller + createBegin: aZone { Controller *obj = [super createBegin: aZone]; obj->targetObject = [TargetObject create: aZone]; [obj->targetObject addRef: notifyFunction withArgument: obj]; return obj; } - scheduleStuff { schedule = [Schedule create: globalZone]; [schedule at: 0 createActionTo: targetObject message: M(printTime)]; [schedule at: 1 createActionTo: self message: M(dropTarget)]; [schedule at: 2 createActionTo: self message: M(ponderTheUniverse)]; [schedule at: 3 createActionTo: self message: M(ponderTheUniverse)]; [schedule at: 4 createActionTo: targetObject message: M(printTime)]; [schedule at: 5 createActionTo: self message: M(ponderTheUniverse)]; [schedule at: 6 createActionTo: self message: M(ponderTheUniverse)]; [schedule at: 7 createActionTo: self message: M(ponderTheUniverse)]; [schedule at: 8 createActionTo: self message: M(ponderTheUniverse)]; [schedule at: 9 createActionTo: self message: M(ponderTheUniverse)]; return self; } - ponderTheUniverse { timeval_t t = getCurrentTime (); const char *strs[] = { "", "", "\nVery superstitious, wash your face and hands", "Rid me of the problem, do all that you can", "", "Keep me in a daydream, keep me goin' strong", "You don't wanna save me, sad is my song", "\nWhen you believe in things that you don't understand", "Then you suffer", "Superstition ain't the way" }; printf ("%s\n", strs[t]); } - (id )getSchedule { return schedule; } - (void)dropTarget { [targetObject drop]; } - go { return [[schedule activateIn: nil] run]; } @end int main (int argc, const char **argv) { id controller; initSwarmBatch (argc, argv); controller = [Controller create: globalZone]; [controller scheduleStuff]; [controller go]; return 0; } /* Local Variables: compile-command: "/opt/egcs/bin/gcc -o s -g -Wno-import -L/opt/SUNWtcl/8.0/sun4/lib -R/opt/SUNWtcl/8.0/sun4/lib -L/opt/SDGblt/2.4g/lib -R/opt/SDGblt/2.4g/lib -L/opt/SDGlibffi/1.20/lib -R/opt/SDGlibffi/1.20/lib -L/opt/SDGswarm/1.4.1/lib -L/opt/SDGzlib/1.1.3/lib -L/usr/local/X11/lib -R/usr/local/X11/lib -L/usr/openwin/lib -R/usr/openwin/lib -L/opt/SDGhdf5/1.0.1/lib -I/opt/SDGswarm/1.4.1/include s.m -lanalysis -lsimtools -lsimtoolsgui -lactivity -ltkobjc -lrandom -lobjectbase -ldefobj -lcollections -lmisc -ltclobjc -ltk8.0 -ltcl8.0 -lBLT -lsocket -ldl -lnsl -L/usr/openwin/lib -lhdf5 -lpng -lz -lXpm -lX11 -lffi -lm -lobjc -lpthread -lposix4" End: */