Sunday, June 20, 2010

C++ and OTcl Linkage: Binding C++ and OTcl classes — Part II

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
   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 how C++ and OTcl classes are bound together. In this post, I will explain the internal mechanism of the binding process.




Let return to our example
Our objective was to bind two following classes together:
  • C++ class name = MyObject
  • OTcl class name = MyOTclObject
The main step in doing so was to add the following code in a C++ file otcl.cc:
1 #include "otcl.h"
2 static class MyObjectClass : public TclClass {
3 public:
4    MyObjectClass() : TclClass("MyOTclObject") {}
5    TclObject* create(int, const char*const*) {
6       return (new MyObject());
7   }
8 }  class_my_object;

In this post, we are going see what the above codes mean.

It's Just Declaration of a Variable
The above codes look quite scary. But it is quite simple. Let us go step by step, shall we?
  • I. We can declare a static C++ variable class_my_object whose class is MyObjectClass as follows.
static MyObjectClass class_my_object;
  • II. Now, class MyObjectClass has not been declared. We need to insert class definition before the variable declaration. The code would look like this. 
static class MyObjectClass : public TclClass {
    <CLASS DEFITION>
}  class_my_object;
which says that class MyObjectClass derives from class TclClass.

Class Definition

There are 2 main steps in class definition
1. Class constructor (Line 4)
MyObjectClass() : TclClass("MyOTclObject") {}
which feeds the OTcl class name MyOTclObject to the based class TclClass. 
2. Function create(...) (Lines 4-7)

This function is invoked when a MyOTclObject object is created in the OTcl domain using the following statement
new MyOTclObject
What function create(...) does is to create and return a C++ object, namely MyObject, to the caller (Line 6).

Wrap-up
So the above chunk of codes is just declaration of  a class and a static variable which maps a C++ class MyObject to an OTcl class MyOTclObject.
When an OTcl object is created, NS2 looks up the repository of mapping variables, and calls function creates(...) in order to create a shadow C++ object. I will blog about the object creation process again later in this series.
=======================================================
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009.

You may also find lecture notes and other resource at the following website: http://www.ece.ubc.ca/~teerawat/NS2.htm

No comments: