swarm-support
[Top][All Lists]
Advanced

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

Re: Sharing variables and inline functions


From: Axel von Kamp
Subject: Re: Sharing variables and inline functions
Date: Thu, 04 Mar 1999 12:07:23 +0000

I've also been thinking about how to efficiently gain read access to instance
variables without declaring them as @public. My idea (inspired by what
C++ does) is that, since in Objective C you can still use functions and gcc
supports inline functions (which is not ANSI standard), to use inline
functions for access. I have edited the example below to illustrate what I'm
talking about

> > You have the variable you want to share in the class you want to give
> > write access, for eksample in Modelswarm, and then you implement a
> > method (in ModelSwarm) to read the variable. Here is an eksample:
> >
> > @interface ModelSwarm: Swarm
> > {
> >    int  shared;
> > }
> > - (int) getShared;

inline int getShared (ModelSwarm *this);

> > @end;
> >
> > @implementation ModelSwarm
> > - (int) getShared
> > {
> >    return shared
> > }

inline int getShared (ModelSwarm *this)
{
    return self-> shared;
}

> > @end;
> >
> > Then you let all your Agents have a reference to the ModelSwarm, that
> > you have to initialize when you create the Agent objects
> >
> > @interface Agent: Swarm
> > {
> >    id modelSwarm;
> > }
> > - initModel: aModelSwarm;
> >
> > and then you can read the shared variable with
> > [modelSwarm getShared];

int myInt;
myInt= getShared (modelSwarm);

> > /Eric Werk
> >
>
> This's a good idea,but unfortunately the shared variables are all matrixes
> that are heavely used by each agent.In each step of simulation all the
> agents have to traverse all the rows of those matrixes.Wouldn't it be an
> excessive overhead to call every time a method on the ModelSwarm to get
> the needed row of each matrix?

This should (?; I'm no compiler expert, so anybody with more technical
knowledge please comment on this) be as efficient as using a @public instance
variable without the drawback that @public instance variables can also be
written to. (Furthermore, @public instance variables are not considered to be
good style in OO languages.)
Of course, when you use any functions, you cannot override these as you can
with messages. But in this case, why would a subclass want to override read
acces to one of the instance variables of its superclass?

>
> Then I was thinking about declaring these matrixes @public in ModelSwarm's
> interface and the acces their rows as a C structure ,i.e. modelSwarm->row.
>
> I tried to do this way modifying the simpleObserverSwarm2 to make each bug
> print a string declared @public in ModelSwarm  application,but it seems to
> crash it.Is the latter method correct?

It should be working. But whenever you use @public you have to make sure that
when you change the interface of the class which declares a @public instance
variable, to recompile all modules which access those instance variables. If
unsure: make clean; make.

-Axel




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