Note: The content in this series is extracted from the book, Introduction to Network Simulator NS2. You may have to read chapter 3 of the book for better understanding.
Introduction
This post is the second post in the series on C++ and OTcl Linkage:
1. Why Two Languages?
2. Binding C++ and OTcl classes [Main steps, The mechanism].
3. Variable binding
4. OTcl command: Invoking C++ statements from the OTcl domain [ Getting Started, Invocation process ]
5. Eval and result: Invoking OTcl statements from the C++ domain
6. Object binding and object construction process.
In the previous post, I blogged about OTcl command. I took you to see how to create a OTcl command in C++ and invoke the command from the OTcl domain. how C++ and OTcl classes are bound together. In this post, I will explain the key steps for OTcl command invocation.
Objective of this post
From the previous post, our OTcl file looks like this:
1 set obj [new MyOTclObject]
2 $obj show-delay
In this post, we are going to look at the main step OTcl does when seeing
$obj show-delay
Key Steps:
Step 1: Look for the instproc
This is a simple structure of OTcl: Object name followed by the name of instproc. So, in the first step, OTcl see if the instproc "show-delay" is defined in the class of $obj. If so, it will execute the instproc. Otherwise, it will go to Step 2.
Step 2: Look for the default instproc "unknown"
OTcl has a special instproc called "unknown" which will be invoked if the invoked instproc is not defined in the current class.
To see this, recall from the previous post that when executing "otcl.tcl" you will see
Now let's add the followings to the beginning of the file "otcl.tcl".
3 MyOTclObject instproc unknown args {
4 puts "Invoking unknown"
5 }
Now if you try to run "otcl.tcl", you will see
Since the instproc unknown is defined in the class of $obj, OTcl invokes what defined in the instproc unknown, and moves to the next statement.
Step 3: Move to the base class
OTcl is an object oriented program. According to the inheritance principle, if OTcl cannot find the method in the current class, it will moves to the base class and look for the method again. So, in this step, OTcl moves to the base class and repeat Step 1 and 2.
Step 4: The instproc unknown of class SplitObject
SplitObject is the uppermost class of every TclObject classes--meaning every TclObject must derive from this class. It defines instproc unknown as follows (see file ns-allinone-xx/tclcl-x.xx/tcl-object.tcl for more detail):
SplitObject instproc unknown args {
if [catch "$self cmd $args" ret] {
set cls [$self info class]
global errorInfo
set savedInfo $errorInfo
error "error when calling class $cls: $args" $savedInfo
}
return $ret
}
This instproc is in fact bound to the C++ domain. We shall talk about it in details in the next post. Please stay tune!!
======================================================
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009. Buy it now from
You may also find lecture notes and other resource at the following website: http://www.ece.ubc.ca/~teerawat/NS2.htm
No comments:
Post a Comment