home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / ObjectTcl-1.1 / docs / Example.txt < prev    next >
Encoding:
INI File  |  1995-06-30  |  5.7 KB  |  241 lines

  1. [Image]
  2.     ------------------------------------------------------------------------
  3.  
  4.  
  5. Introduction
  6.  
  7. This page provides a simple example of how to:
  8.  
  9.      Export C++ classes to the Object Tcl system
  10.      Describe new classes in Object Tcl
  11.      Inherit Object Tcl classes from C++ classes
  12.      Build an augmented Tcl shell
  13.      Manipulate Object Tcl objects
  14.  
  15. C++ Class
  16.  
  17. Class A is a simple C++ class that maintains a reference (pointer) to another
  18. class A object and provides three methods. The first method, doIt, is a virtual
  19. method that prints a simple message identifying itself as class A's version of
  20. doIt. The second method, doItNext, invokes the doIt method upon the other class
  21. A object referenced by the private attribute next. The final method, setNext,
  22. can be used to set the next class A object reference.
  23.  
  24. Class A is described and implemented in C++ in the following two files:
  25.  
  26. File A.H
  27.  
  28. #ifndef A_H
  29. #define A_H
  30.  
  31. // File A.H
  32.  
  33. class A
  34. {
  35. public:
  36.    A (A *next);                 // Constructor member function
  37.    virtual ~A ();               // Destructor member function
  38.    virtual void doIt (void);    // Virtual member function
  39.    void doItNext (void);        // Member function
  40.    void setNext (A *obj);       // Member function
  41. private:
  42.    A *next;                     // Member variable
  43. };
  44.  
  45. #endif // A_H
  46.  
  47. File A.C
  48.  
  49. #include <iostream.h>
  50. #include "A.H"
  51.  
  52. // File A.C
  53.  
  54. A::A (A *n)
  55. {
  56.    next = n;
  57.    cout << "A::constructed" << endl;
  58. }
  59.  
  60. A::~A ()
  61. {
  62.    cout << "A::destructed" << endl;
  63. }
  64.  
  65. void A::doIt (void)
  66. {
  67.    cout << "A::doIt" << endl;
  68. }
  69.  
  70. void A::setNext (A *n)
  71. {
  72.    next = n;
  73. }
  74.  
  75. void A::doItNext (void)
  76. {
  77.    if (next != NULL)
  78.    {
  79.       next->doIt();     // "doIt" is dynamically bound so it could be the
  80.                         // "doIt" of class A or a subclass of A.
  81.    }
  82. }
  83.  
  84. Exporting C++ Class A to Object Tcl
  85.  
  86. The following file contains the CDL for class A. This file may be processed by
  87. the CDL processor and the resulting C++ files linked into the final
  88. application.
  89.  
  90. # File A_cdl.cdl
  91.  
  92. pass {
  93. // Generated from A_cdl.cdl
  94. #include "A.H"
  95. #include "A_cdl.H"
  96. }
  97.  
  98. class A {
  99.    constructor {obref A}
  100.    method doIt -dynamic {void} {void}   # "doIt" required dynamic binding
  101.    method doItNext -static {void} {void}
  102.    method setNext -static {obptr A} {void}
  103.    # The "A" after the obref type indicates the actual type of the
  104.    # parameter expected by the "setNext" method.
  105. }
  106.  
  107. An Object Tcl Class
  108.  
  109. Class B is a new class described in Object Tcl, below, that inherits from class
  110. A.
  111.  
  112. Class B provides its own version of the doIt method, that is also defined in
  113. class A. Class B's version of the doIt method prints a simple message
  114. identifying itself and then calls the doIt method of its parent class (A).
  115.  
  116. # File B.tcl
  117.  
  118. otclInterface B -isA A {
  119.    constructor {next value}
  120.    method doIt {}       # Redefines the "doIt" method available in class A
  121. }
  122.  
  123. otclImplementation B {
  124.    # The constructor method passes on its first argument up to
  125.    # the constructor of the "A" parent class.
  126.    constructor {n v} {{A $n}} {
  127.       set value $v
  128.       puts "B::constructed with value $value"
  129.    }
  130.    destructor {
  131.       puts "B::destructed"
  132.    }
  133.    # The new version of the "doIt" method.
  134.    method doIt {} {
  135.       puts "B::doIt, value is $value, calling A::doIt"
  136.  
  137.       # Invoke the "doIt" method but make sure it is the version
  138.       # of the "A" parent class and this version that would be
  139.       # chosen by default using dynamic binding.
  140.       $this -A doIt
  141.    }
  142.    attribute value
  143. }
  144.  
  145. Building
  146.  
  147. To build a version of the Tcl shell (tclsh) that includes the Object Tcl
  148. package, with class A and class A exported to Object Tcl, perform the
  149. following:
  150.  
  151.   1. Compile class A:
  152.  
  153.      system% CC -c A.C
  154.  
  155.   2. Process the CDL for class A:
  156.  
  157.      system% cdl -h A_cdl.cdl A_cdl.H
  158.      system% cdl -s A_cdl.cdl A_cdl.C
  159.  
  160.   3. Compile the C++ code generated by the processing of A's CDL description:
  161.  
  162.      system% CC -I$(OTCL) -I$(TCL) -c A_cdl.C
  163.  
  164.   4. Link the object files together using a C++ linker:
  165.  
  166.      system% CC -o examplesh -L$(OTCL) -L$(TCL) A.o A_cdl.o \
  167.      $(OTCL)/tclAppInit.o -lotcl -ltcl -lm
  168.  
  169. Note: the files tclAppInit.o and tclMain.o are modified version of the files
  170. from the tcl7.3 distribution. These files have been modified to initialize the
  171. Object Tcl package and compile under C++.
  172.  
  173. Testing
  174.  
  175. To test the augmented Tcl shell:
  176.  
  177. system% ./examplesh
  178. % source TestB.tcl
  179. About to construct an instance of class A
  180. A::constructed
  181.  
  182. About to call method doIt on instance of class A
  183. A::doIt
  184.  
  185. About to construct an instance of class B
  186. A::constructed
  187. B::constructed with value 5
  188.  
  189. About to call method doIt on instance of class B
  190. B::doIt, value is 5, calling A::doIt
  191. A::doIt
  192.  
  193. Setting instance of class A to point at instance of class B
  194. About to call method doItNext on instance of class A
  195. B::doIt, value is 5, calling A::doIt
  196. A::doIt
  197.  
  198. About to destruct instance of class A
  199. A::destructed
  200.  
  201. About to destruct instance of class B
  202. B::destructed
  203. A::destructed
  204. %
  205.  
  206. Where TestB.tcl is:
  207.  
  208. # TestB.tcl
  209.  
  210. puts "About to construct an instance of class A"
  211. set a [otclNew A ""]
  212.  
  213. puts "\nAbout to call method doIt on instance of class A"
  214. $a doIt
  215.  
  216. source B.tcl
  217. puts "\nAbout to construct an instance of class B"
  218. set b [otclNew B "" 5]
  219.  
  220. puts "\nAbout to call method doIt on instance of class B"
  221. $b doIt
  222.  
  223. puts "\nSetting instance of class A to point at instance of class B"
  224. $a setNext $b
  225. puts "About to call method doItNext on instance of class A"
  226. $a doItNext
  227.  
  228. puts "\nAbout to destruct instance of class A"
  229. otclDelete $a
  230.  
  231. puts "\nAbout to destruct instance of class B"
  232. otclDelete $b
  233.  
  234.     ------------------------------------------------------------------------
  235.  
  236. Object Tcl | Overview | Language Reference | C++ Binding Reference | Example |
  237. Source Code
  238.     ------------------------------------------------------------------------
  239.  
  240. otcl@x.co.uk
  241.