1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
An example showing how you can create an object oriented interface
to SWIG generated functions using only a short Tcl script. This is
much more elegant than the default object-oriented interface generated
by SWIG and probably more memory efficient. On the downside, it may
be a little slower, but it's still pretty cool.
swig_c++.tcl - A file containing the shadow class code
list.tcl - An example that uses it.
Contributed by Erik Bierwagen and Paul Saxe. Many thanks!
--------------------------------------------------------------------
Dave,
We have been using swig to wrap our C++ objects, and it works very well.
A collegue of mine, Paul Saxe, wrote the attached script to make the
object syntax match C++'s a bit more closely; all you need to do is
source the script, and you can use the new syntax.
The way it works is:
To create an object:
new {ObjectName} {Handle}
To use an object:
$Handle function arguments
To delete an object:
delete Handle.
So, for a Vec with member functions length, get_elem, and set_elem:
new Vec vector
$vector length
$vector set_elem 1 5
delete vector
This matches a little more closely the more "standard" object syntax,
and also has the benefit that when the handle is unlinked, the object
is automatically deleted.
Enjoy!
Erik
|