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

Re: [ns] HOW TO ACCESS C++ VARIABLE USING TCL (fwd)



IMHO, your c++ code is right, but your otcl code needs to be changed. 

There is NO way you can bind a class-static variable into otcl. The only
way you can access it is to define a otcl class linkage like you just
did. Then you should access it like:

set abc [Agent/Tap tapbytes]

You should not say '$agent tapbytes', this will triggers a call to the
OBJECT's otcl linkage but not the class's.

The simplest example of binding class-static variables is in
addr-param.{h,cc} and its otcl parts in tcl/lib/ns-address.tcl (grep for
AddrParams).

- Haobo

On Mon, 15 Jan 2001, shailesh sheoran wrote:

>  this is exactly what i am doing. 
> 
>  i need to access the class variable "tapbytes_" from .tcl.
>  so i have done following modifications to the 
>  TapAgentClass:
> static class TapAgentClass : public TclClass {
>  protected:
>         
>         // MY MODIFICATION 
>         virtual void bind();
>         virtual int method(int argc, const char*const* argv);
>         static int tapbytes_;	
>  
>  public:
> 	// MY MODIFICATION
> 	void recordbytes(Packet* );
> 
> 	TapAgentClass() : TclClass("Agent/Tap"),tapbytes_(0) {}
> 	TclObject* create(int, const char*const*) {
> 		return (new TapAgent());
> 	}
> 
>   then i go on to define recordbytes();
>   and i define bind() and method() as follows:
> 
> 
> void TapAgentClass::bind()
> {
>         // Call to base class bind() must precede add_method() 
>         TclClass::bind();
>         add_method("tapbytes");
> }
> int TapAgentClass::method(int ac, const char*const* av)
> {
>         Tcl& tcl = Tcl::instance();
>         // Notice this argument translation; we can then handle them 
> //as if in TclObject::command() 
>         int argc = ac - 2;
>         const char*const* argv = av + 2;
>         if (argc == 2) {
>                 if (strcmp(argv[1], "tapbytes") == 0) {
>                         tcl.resultf("%d", tapbytes_);
>                         return (TCL_OK);
>                 }
>         } else if (argc == 3) {
>                 if (strcmp(argv[1], "tapbytes") == 0) {
>                         tapbytes_ = atoi(argv[2]);
>                         return (TCL_OK);
>                 }
>         }
>         return TclClass::method(ac, av);
> }
> 
> 
> 
> are there any obvious problems with this code? i get the error:
> Undefined Symbol "TapAgentClass::tapbytes_" referenced from text.
> 
> now if i define tapbytes_ to be global. then it compiles but while running
> in the .tcl script i CAN"T do the following:
> 
> set agent2 [new Agent/Tap]
> $agent2 tapbytes
> 
> could someone be kind enough to look into this long code. i would really
> appreciate it.
> 
> 
> Shailesh Sheoran
> M.ASc. 
> Department of Electrical Engineering
> University of British Columbia
> 
> ---------- Forwarded message ----------
> Date: Mon, 15 Jan 2001 13:59:12 -0800
> From: Scott Michel <[email protected]>
> To: shailesh sheoran <[email protected]>
> Subject: Re: [ns] HOW TO ACCESS C++ VARIABLE USING TCL
> 
> On Sun, Jan 14, 2001 at 10:16:52PM -0800, shailesh sheoran wrote:
> > hi,
> >   i needed to define a variable in tap.cc and access it from my  .tcl
> > script. i couldnot quite decipher the information from the manual. would
> > someone please tell me how to do it.
> >   i would really appreciate it.
> > thanks,
> > Shailesh Sheoran
> > M.ASc. 
> > Department of Electrical Engineering
> > University of British Columbia
> 
> G'day, eh?!
> 
> All seriousness aside, is the C++ variable a class variable or an
> instance variable?
> 
> - For class variables (i.e. "static" in the class declaration), you
>   want to call bind(...).
> 
> - For instance variables (i.e. variables in each instance), you want
>   to look at examples that use "delay_bind(...)", which abound in the
>   code. Have a look at agent.cc, as one example.
> 
> HTH
> 
> 
> -scooter
> 
>