📄 node24.html
字号:
<html><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><!--Converted with jLaTeX2HTML 2002 (1.62) JA patch-1.4patched version by: Kenshi Muto, Debian Project.LaTeX2HTML 2002 (1.62),original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>3.5 Class TclClass</TITLE><META NAME="description" CONTENT="3.5 Class TclClass"><META NAME="keywords" CONTENT="everything"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><META NAME="Generator" CONTENT="jLaTeX2HTML v2002 JA patch-1.4"><META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"><LINK REL="STYLESHEET" HREF="everything.css" tppabs="http://www.isi.edu/nsnam/ns/doc/everything.css"><LINK REL="next" HREF="node26.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node26.html"><LINK REL="previous" HREF="node15.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node15.html"><LINK REL="up" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html"><LINK REL="next" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html"></HEAD><BODY ><!--Navigation Panel--><A NAME="tex2html1494" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html1488" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html1482" HREF="node23.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node23.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html1490" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html1492" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html1495" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html">3.5.1 How to Bind</A><B> Up:</B> <A NAME="tex2html1489" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html">3. OTcl Linkage</A><B> Previous:</B> <A NAME="tex2html1483" HREF="node23.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node23.html">3.4.4 command Methods: Definition</A>   <B> <A NAME="tex2html1491" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html1493" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <BR><BR><!--End of Navigation Panel--><H1><A NAME="SECTION02150000000000000000"></A><A NAME="sec:TclClass"></A><BR>3.5 Class TclClass</H1><P>This compiled class (TclClass../Tcl/Tcl.h)is a pure virtual class.Classes derived from this base class provide two functions:construct the interpreted class hierarchyto mirror the compiled class hierarchy; andprovide methods to instantiate new TclObjects.Each such derived class is associated with a particular compiled classin the compiled class hierarchy, and can instantiate new objects in theassociated class.<P>As an example, consider a class such as theclass <TT>RenoTcpClass</TT>.It is derived from class <TT>TclClass</TT>, andis associated with the class <TT>RenoTcpAgent</TT>.It will instantiate new objects in the class <TT>RenoTcpAgent</TT>.The compiled class hierarchy for <TT>RenoTcpAgent</TT> is thatit derives from <TT>TcpAgent</TT>, that in turn derives from <TT>Agent</TT>,that in turn derives (roughly) from <TT>TclObject</TT>.<TT>RenoTcpClass</TT> is defined as<PRE> static class RenoTcpClass: public TclClass { public: RenoTcpClass() : TclClass("Agent/TCP/Reno") {} TclObject* create(int argc, const char*const* argv) { return (new RenoTcpAgent()); } } class_reno;</PRE>We can make the following observations from this definition:<OL><LI>The class defines only the constructor, and one additional method, to <TT>create</TT> instances of the associated TclObject.</LI><LI> will execute the <TT>RenoTcpClass</TT> constructor for the static variable <TT>class_reno</TT>, when it is first started. This sets up the appropriate methods and the interpreted class hierarchy.</LI><LI>The constructor specifies the interpreted class explicitly as <TT>Agent/TCP/Reno</TT>. This also specifies the interpreted class hierarchy implicitly.<P>Recall that the convention in is to use the character slash ('/') is a separator. For any given class <TT>A/B/C/D</TT>, the class <TT>A/B/C/D</TT> is a sub-class of <TT>A/B/C</TT>, that is itself a sub-class of <TT>A/B</TT>, that, in turn, is a sub-class of <TT>A</TT>. <TT>A</TT> itself is a sub-class of <TT>TclObject</TT>.<P>In our case above, the TclClass constructor creates three classes, <TT>Agent/TCP/Reno</TT> sub-class of <TT>Agent/TCP</TT> sub-class of <TT>Agent</TT> sub-class of <TT>TclObject</TT>.</LI><LI>This class is associated with the class <TT>RenoTcpAgent</TT>; it creats new objects in this associated class.</LI><LI>The <TT>RenoTcpClass::create</TT> method returns TclObjects in the class <TT>RenoTcpAgent</TT>.</LI><LI>When the user specifies <TT>new Agent/TCP/Reno</TT>, the routine <TT>RenoTcpClass::create</TT> is invoked.</LI><LI>The arguments vector (<TT>argv</TT>) consists of<P>-- <TT>argv[0]</TT> contains the name of the object.<P>-- <TT>argv[1...3]</TT> contain <TT>$self</TT>, <TT>$class</TT>, and <TT>$proc</TT>.Since <TT>create</TT> is called through the instance procedure <TT>create-shadow</TT>, <TT>argv[3]</TT> contains <TT>create-shadow</TT>.<P>-- <TT>argv[4]</TT> contain any additional arguments (passed as a string) provided by the user.</LI></OL>The Trace../ns-2/trace.cc illustratesargument handling by TclClass methods.<PRE> class TraceClass : public TclClass { public: TraceClass() : TclClass("Trace") {} TclObject* create(int args, const char*const* argv) { if (args \>= 5) return (new Trace(*argv[4])); else return NULL; } } trace_class;</PRE>A new Trace object is created as<PRE> new Trace "X"</PRE>Finally, the nitty-gritty details of how the interpreted class hierarchy is constructed:<OL><LI>The object constructor is executed when first starts.</LI><LI>This constructor calls the TclClass constructor with the name of the interpreted class as its argument.</LI><LI>The TclClass constructor stores the name of the class, and inserts this object into a linked list of the TclClass objects.</LI><LI>During initialization of the simulator, Tcl_AppInit../ns-2/ns_tclsh.cc::Tcl_AppInit invokes TclClass::bind../Tcl/Tcl.ccTclClass::bind</LI><LI>For each object in the list of TclClass objects, []bind invokes []register../Tcl/tcl-object.tclTclObject::register, specifying the name of the interpreted class as its argument.</LI><LI>[]register establishes the class hierarchy, creating the classes that are required, and not yet created.</LI><LI>Finally, []bind defines instance procedures <TT>create-shadow</TT> and <TT>delete-shadow</TT> for this new class.</LI></OL><P><BR><HR><!--Table of Child-Links--><A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A><UL><LI><A NAME="tex2html1496" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html">3.5.1 How to Bind Static C++ Class Member Variables</A></UL><!--End of Table of Child-Links--><HR><!--Navigation Panel--><A NAME="tex2html1494" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html1488" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html1482" HREF="node23.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node23.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html1490" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html1492" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html1495" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html">3.5.1 How to Bind</A><B> Up:</B> <A NAME="tex2html1489" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html">3. OTcl Linkage</A><B> Previous:</B> <A NAME="tex2html1483" HREF="node23.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node23.html">3.4.4 command Methods: Definition</A>   <B> <A NAME="tex2html1491" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html1493" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <!--End of Navigation Panel--><ADDRESS>2003-09-23</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -