12.07.2015 Views

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

TraceClass() : TclClass("Trace") {}TclObject* cre<strong>at</strong>e(int args, co<strong>ns</strong>t char*co<strong>ns</strong>t* argv) {if (args >= 5)return (new Trace(*argv[4]));elsereturn NULL;}} trace_class;A new Trace object is cre<strong>at</strong>ed asnew Trace "X"Finally, the nitty-gritty details of how the interpreted class hierarchy is co<strong>ns</strong>tructed:1. <strong>The</strong> object co<strong>ns</strong>tructor is executed when <strong>ns</strong> first starts.2. This co<strong>ns</strong>tructor calls the TclClass co<strong>ns</strong>tructor with the name of the interpreted class as its argument.3. <strong>The</strong> TclClass co<strong>ns</strong>tructor stores the name of the class, <strong>and</strong> i<strong>ns</strong>erts this object into a linked list of the TclClass objects.4. During initializ<strong>at</strong>ion of the simul<strong>at</strong>or, Tcl_AppInit(void) invokes TclClass::bind(void)5. For each object in the list of TclClass objects, bind() invokes register{}, specifying the name of the interpretedclass as its argument.6. register{} establishes the class hierarchy, cre<strong>at</strong>ing the classes th<strong>at</strong> are required, <strong>and</strong> not yet cre<strong>at</strong>ed.7. Finally, bind() defines i<strong>ns</strong>tance procedures cre<strong>at</strong>e-shadow <strong>and</strong> delete-shadow for this new class.3.5.1 How to Bind St<strong>at</strong>ic C++ Class Member VariablesIn Section 3.4, we have seen how to expose member variables of a C++ object into OTcl space. This, however, does not applyto st<strong>at</strong>ic member variables of a C++ class. Of course, one may cre<strong>at</strong>e an OTcl variable for the st<strong>at</strong>ic member variable of everyC++ object; obviously this defe<strong>at</strong>s the whole meaning of st<strong>at</strong>ic members.We cannot solve this binding problem using a similar solution as binding in TclObject, which is based on I<strong>ns</strong>tVar, becauseI<strong>ns</strong>tVars in TclCL require the presence of a TclObject. However, we can cre<strong>at</strong>e a method of the corresponding TclClass <strong>and</strong>access st<strong>at</strong>ic members of a C++ class through the methods of its corresponding TclClass. <strong>The</strong> procedure is as follows:1. Cre<strong>at</strong>e your own derived TclClass as described above;2. Declare methods bind() <strong>and</strong> method() in your derived class;3. Cre<strong>at</strong>e your binding methods in the implement<strong>at</strong>ion of your bind() with add_method("your_method"), thenimplement the h<strong>and</strong>ler in method() in a similar way as you would do in TclObject::comm<strong>and</strong>(). Notice th<strong>at</strong> thenumber of arguments passed to TclClass::method() are different from those passed to TclObject::comm<strong>and</strong>().<strong>The</strong> former has two more arguments in the front.As an example, we show a simplified version of PacketHeaderClass in ~<strong>ns</strong>/packet.cc. Suppose we have the followingclass Packet which has a st<strong>at</strong>ic variable hdrlen_ th<strong>at</strong> we want to access from OTcl:31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!