A simple example illustrates how TCL maps to C ++. The purpose of a program is to create a new network object class "myagent", which is a subclass of "agent.
Below I paste the ex-linkage.cc code here:
// This is a simple agent just for show
# Include <stdio. h>
# Include <string. h>
# Include "agent. H"
Class myagent: public agent {
Public:
Myagent ();
Protected:
Int command (INT argc, const char * const * argv );
PRIVATE:
Int my_val1;
Double my_val2;
Void myprivfunc (void );
};
// ================================================ ==================================
// Assume that the class "myagent" of a new network object is created in C ++, which is derived from the "agent" class,
// Then it can create an OTCL object instance. To implement this function, we must define
// The connected object "myagentclass", derived from tclclass. This join object creates
// You can specify an OTCL object ("agent/myagentotcl" in this example) and create a connection.
// Between the OTCL object and the C ++ object ("myagent"), its instance runs the "Create" member function to specify
// Program
// ================================================ ==========================================
Static class myagentclass: Public tclclass {// This is the ing object class; it is a static class; inherits the tclclass
Public:
Myagentclass (): tclclass ("agent/myagentotcl "){}
// Create an OTCL object named "agent/myagentotcl"
Tclobject * Create (INT, const char * const *){
Return (New myagent (); // in this way, the OTCL and C ++ object are connected, which is the Startup Program of the instance.
}
} Class_my_agent; // an instance is created when the NS is started. class_my_agent is a static variable.
Myagent: myagent (): Agent (pt_udp) {// This function outputs the C ++ class member variables to OTCL
BIND ("my_val1_otcl", & my_val1); // bind the class member variable in otcl to the c ++ member variable. You can use OTCL to control the C ++ member variable, it is recommended to set the default value in the ns-lib.tcl, otherwise there is warning
BIND ("my_val2_otcl", & my_val2 );
}
Int myagent: Command (INT argc, const char * const * argv) {// output the C ++ object control command to OTCL, which defines a command interpreter
If (argc = 2 ){
If (strcmp (argv [1], "Call-my-priv-func") = 0 ){
Myprivfunc ();
Return (tcl_ OK );
}
}
Return (AGENT: Command (argc, argv); // If no result is found, the command function of the parent and parent classes is called.
}
Void myagent: myprivfunc (void) {// execute the OTCL command in the C ++ object, which is complementary to the above function
TCL & TCL = TCL: instance (); // generate a Tcl instance and PASS commands through the function. For example, the following two functions are called:
TCL. eval ("puts \" Message from myprivfunc \"");
TCL. evalf ("puts \" my_val1 = % d \ "", my_val1 );
TCL. evalf ("puts \" my_val2 = % d \ "", my_val2 );
}
Procedure:
1. Create a New ex-linkage.cc file and place it in ~ /Ns2.29 directory.
2. Open ~ /Tcl/lib/tcl-default.tcl file, assign default values to member variables:
# Agent set seqno _ 0 now is gone
# Agent set class _ 0 now is gone
Agent/myagentotcl set my_val1_otcl 1
Agent/myagentotcl set my_val2_otcl 0.0001
Agent/ping set packetsize _ 64
3. Add ex-linkage.o \ In makefile. In \
4. Final./configure make
Attach the Tcl test script:
# Create myagent
Set myagent [new agent/myagentotcl]
$ Myagent set my_val1_otcl 2
$ Myagent set my_val2_otcl 3.14
$ Myagent call-my-priv-func
To sum up, the Tcl ing mainly consists of five steps.
Create a network object class
Create object ing class public tclclass
Bind C ++ member variables and OTCL Variables)
Control of C ++ member variables (command or member function)
Compile the. CC file (make)