Home

Sponsors

Download

Install

Running

Examples

Tcl Scripts

News

Future Work

Copyright

There is no better way to get started building wrappers with CABLE than to look at examples. Here we provide the following: In order to run the Tcl examples, you will need to make sure that the "cable" package provided by CABLE can be loaded. You will need to make sure that your TCLLIBPATH is set correctly. See the Tcl Scripts page for more information on how to set this environment variable.

Tcl wrapper for the STL "string" class:

We will use CABLE to create a Tcl package called "StringTcl" containing a wrapper for the std::string class using the name "stdstring". First we write a configuration file, stringConfig.cxx:
  #include <string>
  #ifdef CABLE_CONFIGURATION
  namespace _cable_
  {
    const char* const group="StringTcl1";
    const char* const package="StringTcl";
    const char* const groups[]={"StringTcl1"};
    namespace wrappers
    {
      typedef ::std::string stdstring;
    }
  }
  
  void cable_instantiate()
  {
    using namespace _cable_::wrappers;
    sizeof(stdstring);
  }
  
  #endif
Now we run cable to produce Tcl wrappers.
  $ cable stringConfig.cxx -tcl string_tcl.cxx
  Writing Tcl wrappers to "string_tcl.cxx"
Next, we compile the generated code. This example assumes we are using GCC with CABLE installed with prefix /usr/local. GCC-XML must have been configured to simulate your version of GCC before running cable. This was probably done automatically by your GCC-XML build or installation.
  $ g++ -I/usr/local/include/Cable -c string_tcl.cxx
Finally, we create the Tcl package:
  $ g++ -shared -o libStringTcl.so string_tcl.o -L/usr/local/lib/Cable -lCableTclFacility
This package can be loaded into tclsh like this:
  % load ./libStringTcl.so
We can then source the sample script stringSample.tcl:
  % source stringSample.tcl
  FooBar
  Hello, World!
  Hello
The contents of this script are:
# Create an instance of std::string.
set s0 [stdstring]

# Call the C++ assignment operator to set it.
$s0 = "FooBar"

# Print the string's value:
puts [$s0 c_str]

# Create another instance 
set s1 [stdstring "Hello, World!"]
puts [$s1 c_str]

# Find the substring before the first comma.
set s2 [$s1 substr 0 [$s1 find_first_of ,]]
puts [$s2 c_str]

# The output from this script should be:
#   FooBar
#   Hello, World!
#   Hello