Tcl.html 78.6 KB
Newer Older
1 2 3 4 5 6 7 8
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SWIG and Tcl</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body bgcolor="#ffffff">
9
<H1><a name="Tcl"></a>40 SWIG and Tcl</H1>
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<!-- INDEX -->
<div class="sectiontoc">
<ul>
<li><a href="#Tcl_nn2">Preliminaries</a>
<ul>
<li><a href="#Tcl_nn3">Getting the right header files</a>
<li><a href="#Tcl_nn4">Compiling a dynamic module</a>
<li><a href="#Tcl_nn5">Static linking</a>
<li><a href="#Tcl_nn6">Using your module</a>
<li><a href="#Tcl_nn7">Compilation of C++ extensions</a>
<li><a href="#Tcl_nn8">Compiling for 64-bit platforms</a>
<li><a href="#Tcl_nn9">Setting a package prefix</a>
<li><a href="#Tcl_nn10">Using namespaces</a>
</ul>
<li><a href="#Tcl_nn11">Building Tcl/Tk Extensions under Windows 95/NT</a>
<ul>
<li><a href="#Tcl_nn12">Running SWIG from Developer Studio</a>
<li><a href="#Tcl_nn13">Using NMAKE</a>
</ul>
<li><a href="#Tcl_nn14">A tour of basic C/C++ wrapping</a>
<ul>
<li><a href="#Tcl_nn15">Modules</a>
<li><a href="#Tcl_nn16">Functions</a>
<li><a href="#Tcl_nn17">Global variables</a>
<li><a href="#Tcl_nn18">Constants and enums</a>
<li><a href="#Tcl_nn19">Pointers</a>
<li><a href="#Tcl_nn20">Structures</a>
<li><a href="#Tcl_nn21">C++ classes</a>
<li><a href="#Tcl_nn22">C++ inheritance</a>
<li><a href="#Tcl_nn23">Pointers, references, values, and arrays</a>
<li><a href="#Tcl_nn24">C++ overloaded functions</a>
<li><a href="#Tcl_nn25">C++ operators</a>
<li><a href="#Tcl_nn26">C++ namespaces</a>
<li><a href="#Tcl_nn27">C++ templates</a>
<li><a href="#Tcl_nn28">C++ Smart Pointers</a>
</ul>
<li><a href="#Tcl_nn29">Further details on the Tcl class interface</a>
<ul>
<li><a href="#Tcl_nn30">Proxy classes</a>
<li><a href="#Tcl_nn31">Memory management</a>
</ul>
<li><a href="#Tcl_nn32">Input and output parameters</a>
<li><a href="#Tcl_nn33">Exception handling </a>
<li><a href="#Tcl_nn34">Typemaps</a>
<ul>
<li><a href="#Tcl_nn35">What is a typemap?</a>
<li><a href="#Tcl_nn36">Tcl typemaps</a>
<li><a href="#Tcl_nn37">Typemap variables</a>
<li><a href="#Tcl_nn38">Converting  a Tcl list to a char ** </a>
<li><a href="#Tcl_nn39">Returning values in arguments</a>
<li><a href="#Tcl_nn40">Useful functions</a>
<li><a href="#Tcl_nn41">Standard  typemaps</a>
<li><a href="#Tcl_nn42">Pointer handling</a>
</ul>
<li><a href="#Tcl_nn43">Turning a SWIG module into a Tcl Package.</a>
<li><a href="#Tcl_nn44">Building new kinds of Tcl interfaces (in Tcl)</a>
<ul>
<li><a href="#Tcl_nn45">Proxy classes</a>
</ul>
<li><a href="#Tcl_nn46">Tcl/Tk Stubs</a>
</ul>
</div>
<!-- INDEX -->



<p>
<b>Caution: This chapter is under repair!</b>
</p>

<p>
This chapter discusses SWIG's support of Tcl. SWIG currently requires
Tcl 8.0 or a later release.   Earlier releases of SWIG supported Tcl 7.x, but
this is no longer supported.
</p>

86
<H2><a name="Tcl_nn2"></a>40.1 Preliminaries</H2>
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111


<p>
To build a Tcl module, run SWIG using the <tt>-tcl</tt> option :
</p>

<div class="code"><pre>
$ swig -tcl example.i
</pre></div>

<p>
If building a C++ extension, add the <tt>-c++</tt> option:
</p>

<div class="code"><pre>
$ swig -c++ -tcl example.i
</pre></div>

<p>
This creates a file <tt>example_wrap.c</tt> or
<tt>example_wrap.cxx</tt> that contains all of the code needed to
build a Tcl extension module.  To finish building the module, you 
need to compile this file and link it with the rest of your program.
</p>

112
<H3><a name="Tcl_nn3"></a>40.1.1 Getting the right header files</H3>
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129


<p>
In order to compile the wrapper code, the compiler needs the <tt>tcl.h</tt> header file.
This file is usually contained in the directory
</p>

<div class="code"><pre>
/usr/local/include
</pre></div>

<p>
Be aware that some Tcl versions install this header file with a version number attached to it.  If
this is the case, you should probably make a symbolic link so that <tt>tcl.h</tt> points to the correct
header file.
</p>

130
<H3><a name="Tcl_nn4"></a>40.1.2 Compiling a dynamic module</H3>
131 132 133 134 135 136 137 138 139 140 141


<p>
The preferred approach to building an extension module is to compile it into
a shared object file or DLL. Assuming you have code you need to link to in a file
called <tt>example.c</tt>, you will need to compile your program
using commands like this (shown for Linux):
</p>

<div class="code"><pre>
$ swig -tcl example.i
142 143
$ gcc -fPIC -c example.c
$ gcc -fPIC -c example_wrap.c -I/usr/local/include
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
$ gcc -shared example.o example_wrap.o -o example.so
</pre></div>

<p>
The exact commands for doing this vary from platform to platform. 
SWIG tries to guess the right options when it is installed.  Therefore, 
you may want to start with one of the examples in the <tt>SWIG/Examples/tcl</tt> 
directory.   If that doesn't work, you will need to read the man-pages for
your compiler and linker to get the right set of options.  You might also
check the <a href="http://www.dabeaz.com/cgi-bin/wiki.pl">SWIG Wiki</a> for
additional information.
</p>

<p>
When linking the module, the name of the output file has to match the name
of the module.  If the name of your SWIG module is "<tt>example</tt>", the
name of the corresponding object file should be
"<tt>example.so</tt>".
The name of the module is specified using the <tt>%module</tt> directive or the 
163
 <tt>-module</tt> command line option.
164 165
</p>

166
<H3><a name="Tcl_nn5"></a>40.1.3 Static linking</H3>
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231


<p>
An alternative approach to dynamic linking is to rebuild the Tcl
interpreter with your extension module added to it.  In the past,
this approach was sometimes necessary due to limitations in dynamic loading
support on certain machines.  However, the situation has improved greatly
over the last few years and you should not consider this approach 
unless there is really no other option.
</p>

<p>
The usual procedure for adding a new module to Tcl involves writing a
special function <tt>Tcl_AppInit()</tt> and using it to initialize the interpreter and
your module.  With SWIG, the <tt>tclsh.i</tt> and <tt>wish.i</tt> library files
can be used to rebuild the <tt>tclsh</tt> and <tt>wish</tt> interpreters respectively.
For example:
</p>

<div class="code"><pre>
%module example

%inline %{
extern int fact(int);
extern int mod(int, int);
extern double My_variable;
%}

%include "tclsh.i"       // Include code for rebuilding tclsh

</pre></div>

<p>
The <tt>tclsh.i</tt> library file includes supporting code that
contains everything needed to rebuild tclsh. To rebuild the interpreter,
you simply do something like this:
</p>

<div class="code"><pre>
$ swig -tcl example.i
$ gcc example.c example_wrap.c \
        -Xlinker -export-dynamic \
        -DHAVE_CONFIG_H -I/usr/local/include/ \
	-L/usr/local/lib -ltcl -lm -ldl \
	-o mytclsh

</pre></div>

<p>
You will need to supply the same libraries that were used to build Tcl the first
time.  This may include system libraries such as <tt>-lsocket</tt>, <tt>-lnsl</tt>,
and <tt>-lpthread</tt>.  If this actually works, the new version of Tcl
should be identical to the default version except that your extension module will be
a built-in part of the interpreter.
</p>

<p>
<b>Comment:</b> In practice, you should probably try to avoid static
linking if possible. Some programmers may be inclined
to use static linking in the interest of getting better performance.
However, the performance gained by static linking tends to be rather
minimal in most situations (and quite frankly not worth the extra
hassle in the opinion of this author). 
</p>

232
<H3><a name="Tcl_nn6"></a>40.1.4 Using your module</H3>
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359


<p>
To use your module, simply use the Tcl <tt>load</tt> command.  If
all goes well, you will be able to this:
</p>

<div class="code"><pre>
$ tclsh
% load ./example.so
% fact 4
24
%
</pre></div>

<p>
A common error received by first-time users is the following:
</p>

<div class="code">
<pre>
% load ./example.so
couldn't find procedure Example_Init
% 
</pre>
</div>

<p>
This error is almost always caused when the name of the shared object file doesn't
match the name of the module supplied using the SWIG <tt>%module</tt> directive.
Double-check the interface to make sure the module name and the shared object
file match.  Another possible cause of this error is forgetting to link the SWIG-generated
wrapper code with the rest of your application when creating the extension module.
</p>

<p>
Another common error is something similar to the following:
</p>

<div class="code">
<pre>
% load ./example.so
couldn't load file "./example.so": ./example.so: undefined symbol: fact
% 
</pre>
</div>

<p>
This error usually indicates that you forgot to include some object
files or libraries in the linking of the shared library file.  Make
sure you compile both the SWIG wrapper file and your original program
into a shared library file.  Make sure you pass all of the required libraries
to the linker.  
</p>

<p>
Sometimes unresolved symbols occur because a wrapper has been created
for a function that doesn't actually exist in a library.  This usually
occurs when a header file includes a declaration for a function that
was never actually implemented or it was removed from a library
without updating the header file.  To fix this, you can either edit
the SWIG input file to remove the offending declaration or you can use
the <tt>%ignore</tt> directive to ignore the declaration.
</p>

<p>
Finally, suppose that your extension module is linked with another library like this:
</p>

<div class="code">
<pre>
$ gcc -shared example.o example_wrap.o -L/home/beazley/projects/lib -lfoo \
      -o example.so
</pre>
</div>

<p>
If the <tt>foo</tt> library is compiled as a shared library, you might get the following
problem when you try to use your module:
</p>

<div class="code">
<pre>
% load ./example.so
couldn't load file "./example.so": libfoo.so: cannot open shared object file:
No such file or directory
%        
</pre>
</div>

<p>
This error is generated because the dynamic linker can't locate the
<tt>libfoo.so</tt> library.  When shared libraries are loaded, the
system normally only checks a few standard locations such as
<tt>/usr/lib</tt> and <tt>/usr/local/lib</tt>.   To fix this problem,
there are several things you can do.  First, you can recompile your extension
module with extra path information. For example, on Linux you can do this:
</p>

<div class="code">
<pre>
$ gcc -shared example.o example_wrap.o -L/home/beazley/projects/lib -lfoo \
      -Xlinker -rpath /home/beazley/projects/lib \
      -o example.so
</pre>
</div>

<p>
Alternatively, you can set the <tt>LD_LIBRARY_PATH</tt> environment variable to
include the directory with your shared libraries. 
If setting <tt>LD_LIBRARY_PATH</tt>, be aware that setting this variable can introduce
a noticeable performance impact on all other applications that you run.
To set it only for Tcl, you might want to do this instead:
</p>

<div class="code">
<pre>
$ env LD_LIBRARY_PATH=/home/beazley/projects/lib tclsh
</pre>
</div>

<p>
Finally, you can use a command such as <tt>ldconfig</tt> to add additional search paths
to the default system configuration (this requires root access and you will need to read
the man pages). 
</p>

360
<H3><a name="Tcl_nn7"></a>40.1.5 Compilation of C++ extensions</H3>
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376


<p>
Compilation of C++ extensions has traditionally been a tricky problem.
Since the Tcl interpreter is written in C, you need to take steps to
make sure C++ is properly initialized and that modules are compiled
correctly.
</p>

<p>
On most machines, C++ extension modules should be linked using the C++
compiler.  For example:
</p>

<div class="code"><pre>
% swig -c++ -tcl example.i
377 378
% g++ -fPIC -c example.cxx
% g++ -fPIC -c example_wrap.cxx -I/usr/local/include
379 380 381 382 383 384 385 386 387 388 389
% g++ -shared example.o example_wrap.o -o example.so
</pre></div>

<p>
In addition to this, you may need to include additional library
files to make it work.  For example, if you are using the Sun C++ compiler on
Solaris, you often need to add an extra library <tt>-lCrun</tt> like this:
</p>

<div class="code"><pre>
% swig -c++ -tcl example.i
390 391
% CC -KPIC -c example.cxx
% CC -KPIC -c example_wrap.cxx -I/usr/local/include
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
% CC -G example.o example_wrap.o -L/opt/SUNWspro/lib -o example.so -lCrun
</pre></div>

<p>
Of course, the extra libraries to use are completely non-portable---you will 
probably need to do some experimentation.
</p>

<p>
Sometimes people have suggested that it is necessary to relink the
Tcl interpreter using the C++ compiler to make C++ extension modules work.
In the experience of this author, this has never actually appeared to be
necessary.   Relinking the interpreter with C++ really only includes the 
special run-time libraries described above---as long as you link your extension 
modules with these libraries, it should not be necessary to rebuild Tcl.
</p>

<p>
If you aren't entirely sure about the linking of a C++ extension, you
might look at an existing C++ program.  On many Unix machines, the
<tt>ldd</tt> command will list library dependencies.  This should give
you some clues about what you might have to include when you link your
extension module. For example:
</p>

<div class="code">
<pre>
$ ldd swig
        libstdc++-libc6.1-1.so.2 =&gt; /usr/lib/libstdc++-libc6.1-1.so.2 (0x40019000)
        libm.so.6 =&gt; /lib/libm.so.6 (0x4005b000)
        libc.so.6 =&gt; /lib/libc.so.6 (0x40077000)
        /lib/ld-linux.so.2 =&gt; /lib/ld-linux.so.2 (0x40000000)
$
</pre>
</div>

<p>
As a final complication, a major weakness of C++ is that it does not
define any sort of standard for binary linking of libraries.  This
means that C++ code compiled by different compilers will not link
together properly as libraries nor is the memory layout of classes and
data structures implemented in any kind of portable manner.  In a
monolithic C++ program, this problem may be unnoticed.  However, in Tcl, it
is possible for different extension modules to be compiled with
different C++ compilers.  As long as these modules are self-contained,
this probably won't matter.  However, if these modules start sharing data,
you will need to take steps to avoid segmentation faults and other
erratic program behavior.   If working with lots of software components, you
might want to investigate using a more formal standard such as COM.
</p>

443
<H3><a name="Tcl_nn8"></a>40.1.6 Compiling for 64-bit platforms</H3>
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469


<p>
On platforms that support 64-bit applications (Solaris, Irix, etc.),
special care is required when building extension modules.  On these
machines, 64-bit applications are compiled and linked using a different
set of compiler/linker options.  In addition, it is not generally possible to mix 
32-bit and 64-bit code together in the same application.
</p>

<p>
To utilize 64-bits, the Tcl executable will need to be recompiled
as a 64-bit application.  In addition, all libraries, wrapper code,
and every other part of your application will need to be compiled for
64-bits.  If you plan to use other third-party extension modules, they
will also have to be recompiled as 64-bit extensions.
</p>

<p>
If you are wrapping commercial software for which you have no source
code, you will be forced to use the same linking standard as used by
that software.  This may prevent the use of 64-bit extensions.  It may
also introduce problems on platforms that support more than one
linking standard (e.g., -o32 and -n32 on Irix).
</p>

470
<H3><a name="Tcl_nn9"></a>40.1.7 Setting a package prefix</H3>
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488


<p>
To avoid namespace problems, you can instruct SWIG to append a package
prefix to all of your functions and variables. This is done using the
-prefix option as follows :
</p>

<div class="code"><pre>
swig -tcl -prefix Foo example.i
</pre></div>

<p>
If you have a function "<tt>bar</tt>" in the SWIG file, the prefix
option will append the prefix to the name when creating a command and
call it "<tt>Foo_bar</tt>".
</p>

489
<H3><a name="Tcl_nn10"></a>40.1.8 Using namespaces</H3>
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506


<p>
Alternatively, you can have SWIG install your module into a Tcl
namespace by specifying the <tt>-namespace</tt> option :
</p>

<div class="code"><pre>
swig -tcl -namespace example.i
</pre></div>

<p>
By default, the name of the namespace will be the same as the module
name, but you can override it using the <tt>-prefix</tt> option.
</p>

<p>
507
When the <tt>-namespace</tt> option is used, objects in the module
508 509 510
are always accessed with the namespace name such as <tt>Foo::bar</tt>.
</p>

511
<H2><a name="Tcl_nn11"></a>40.2 Building Tcl/Tk Extensions under Windows 95/NT</H2>
512 513 514 515 516 517 518 519 520 521


<p>
Building a SWIG extension to Tcl/Tk under Windows 95/NT is roughly
similar to the process used with Unix.  Normally, you will want to
produce a DLL that can be loaded into tclsh or wish.  This section
covers the process of using SWIG with Microsoft Visual C++.
although the procedure may be similar with other compilers.
</p>

522
<H3><a name="Tcl_nn12"></a>40.2.1 Running SWIG from Developer Studio</H3>
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579


<p>
If you are developing your application within Microsoft developer
studio, SWIG can be invoked as a custom build option.  The process
roughly follows these steps :
</p>

<ul>
<li>Open up a new workspace and use the AppWizard to select a DLL project.

<li>Add both the SWIG interface file (the .i file), any supporting C
files, and the name of the wrapper file that will be created by SWIG
(ie. <tt>example_wrap.c</tt>).  Note : If using C++, choose a
different suffix for the wrapper file such as
<tt>example_wrap.cxx</tt>. Don't worry if the wrapper file doesn't
exist yet--Developer studio will keep a reference to it around.

<li>Select the SWIG interface file and go to the settings menu.  Under
settings, select the "Custom Build" option.

<li>Enter "SWIG" in the description field.

<li>Enter "<tt>swig -tcl -o $(ProjDir)\$(InputName)_wrap.c
$(InputPath)</tt>" in the "Build command(s) field"

<li>Enter "<tt>$(ProjDir)\$(InputName)_wrap.c</tt>" in the "Output files(s) field".

<li>Next, select the settings for the entire project and go to
"C++:Preprocessor". Add the include directories for your Tcl
installation under "Additional include directories".

<li>Finally, select the settings for the entire project and go to
"Link Options".  Add the Tcl library file to your link libraries.  For
example "<tt>tcl80.lib</tt>".  Also, set the name of the output file
to match the name of your Tcl module (ie. example.dll).

<li>Build your project.
</ul>

<p>
Now, assuming all went well, SWIG will be automatically invoked when
you build your project.  Any changes made to the interface file will
result in SWIG being automatically invoked to produce a new version of
the wrapper file.  To run your new Tcl extension, simply run
<tt>tclsh</tt> or <tt>wish</tt> and use the <tt>load</tt> command.
For example :
</p>

<div class="code"><pre>
MSDOS &gt; tclsh80
% load example.dll
% fact 4
24
%
</pre></div>

580
<H3><a name="Tcl_nn13"></a>40.2.2 Using NMAKE</H3>
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642


<p>
Alternatively, SWIG extensions can be built by writing a Makefile for
NMAKE.  To do this, make sure the environment variables for MSVC++ are
available and the MSVC++ tools are in your path.  Now, just write a
short Makefile like this :
</p>

<div class="code"><pre>
# Makefile for building various SWIG generated extensions

SRCS          = example.c
IFILE         = example
INTERFACE     = $(IFILE).i
WRAPFILE      = $(IFILE)_wrap.c

# Location of the Visual C++ tools (32 bit assumed)

TOOLS         = c:\msdev
TARGET        = example.dll
CC            = $(TOOLS)\bin\cl.exe
LINK          = $(TOOLS)\bin\link.exe
INCLUDE32     = -I$(TOOLS)\include
MACHINE       = IX86

# C Library needed to build a DLL

DLLIBC        = msvcrt.lib oldnames.lib  

# Windows libraries that are apparently needed
WINLIB        = kernel32.lib advapi32.lib user32.lib gdi32.lib comdlg32.lib 
winspool.lib

# Libraries common to all DLLs
LIBS          = $(DLLIBC) $(WINLIB) 

# Linker options
LOPT      = -debug:full -debugtype:cv /NODEFAULTLIB /RELEASE /NOLOGO /
MACHINE:$(MACHINE) -entry:_DllMainCRTStartup@12 -dll

# C compiler flags

CFLAGS    = /Z7 /Od /c /nologo
TCL_INCLUDES  = -Id:\tcl8.0a2\generic -Id:\tcl8.0a2\win
TCLLIB        = d:\tcl8.0a2\win\tcl80.lib

tcl::
	..\..\swig -tcl -o $(WRAPFILE) $(INTERFACE)
	$(CC) $(CFLAGS) $(TCL_INCLUDES) $(SRCS) $(WRAPFILE)
	set LIB=$(TOOLS)\lib
	$(LINK) $(LOPT) -out:example.dll $(LIBS) $(TCLLIB) example.obj example_wrap.obj

</pre></div>

<p>
To build the extension, run NMAKE (you may need to run vcvars32
first).  This is a pretty minimal Makefile, but hopefully its enough
to get you started.  With a little practice, you'll be making lots of
Tcl extensions.
</p>

643
<H2><a name="Tcl_nn14"></a>40.3 A tour of basic C/C++ wrapping</H2>
644 645 646 647 648 649 650 651 652 653


<p>
By default, SWIG tries to build a very natural Tcl interface to your
C/C++ code.  Functions are wrapped as functions, classes are wrapped
in an interface that mimics the style of Tk widgets and [incr Tcl]
classes.  This section briefly covers the essential aspects of this
wrapping.
</p>

654
<H3><a name="Tcl_nn15"></a>40.3.1 Modules</H3>
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687


<p>
The SWIG <tt>%module</tt> directive specifies the name of the Tcl
module. If you specify `<tt>%module example</tt>', then everything is
compiled into an extension module <tt>example.so</tt>. When choosing a
module name, make sure you don't use the same name as a built-in
Tcl command.
</p>

<p>
One pitfall to watch out for is module names involving numbers.  If
you specify a module name like <tt>%module md5</tt>, you'll find that the
load command no longer seems to work:
</p>

<div class="code">
<pre>
% load ./md5.so
couldn't find procedure Md_Init
</pre>
</div>

<p>
To fix this, supply an extra argument to <tt>load</tt> like this:
</p>

<div class="code">
<pre>
% load ./md5.so md5
</pre>
</div>

688
<H3><a name="Tcl_nn16"></a>40.3.2 Functions</H3>
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712


<p>
Global functions are wrapped as new Tcl built-in commands.  For example,
</p>

<div class="code"><pre>
%module example
int fact(int n);
</pre></div>

<p>
creates a built-in function <tt>fact</tt> that works exactly
like you think it does:
</p>

<div class="code"><pre>
% load ./example.so
% fact 4
24
% set x [fact 6]
%
</pre></div>

713
<H3><a name="Tcl_nn17"></a>40.3.3 Global variables</H3>
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792


<p>
C/C++ global variables are wrapped by Tcl global variables.  For example:
</p>

<div class="code"><pre>
// SWIG interface file with global variables
%module example
...
%inline %{
extern double density;
%}
...
</pre></div>

<p>
Now look at the Tcl interface:
</p>

<div class="code"><pre>
% puts $density          # Output value of C global variable
1.0
% set density 0.95       # Change value
</pre></div>

<p>
If you make an error in variable assignment, you will get an
error message.  For example:
</p>

<div class="code"><pre>
% set density "hello"
can't set "density": Type error. expected a double.
%
</pre></div>

<p>
If a variable is declared as <tt>const</tt>, it is wrapped as a
read-only variable.  Attempts to modify its value will result in an
error.
</p>

<p>
To make ordinary variables read-only, you can use the <tt>%immutable</tt> directive. For example:
</p>

<div class="code">
<pre>
%{
extern char *path;
%}
%immutable;
extern char *path;
%mutable;
</pre>
</div>

<p>
The <tt>%immutable</tt> directive stays in effect until it is explicitly disabled or cleared using
<tt>%mutable</tt>.
See the <a href="SWIG.html#SWIG_readonly_variables">Creating read-only variables</a> section for further details.
</p>

<p>
If you just want to make a specific variable immutable, supply a declaration name.  For example:
</p>

<div class="code">
<pre>
%{
extern char *path;
%}
%immutable path;
...
extern char *path;      // Read-only (due to %immutable)
</pre>
</div>

793
<H3><a name="Tcl_nn18"></a>40.3.4 Constants and enums</H3>
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876


<p>
C/C++ constants are installed as global Tcl variables containing the
appropriate value.  To create a constant, use <tt>#define</tt>, <tt>enum</tt>, or the
<tt>%constant</tt> directive.  For example:
</p>

<div class="code">
<pre>
#define PI 3.14159
#define VERSION "1.0"

enum Beverage { ALE, LAGER, STOUT, PILSNER };

%constant int FOO = 42;
%constant const char *path = "/usr/local";
</pre>
</div>

<p>
For enums, make sure that the definition of the enumeration actually appears in a header
file or in the wrapper file somehow---if you just stick an enum in a SWIG interface without
also telling the C compiler about it, the wrapper code won't compile.
</p>

<p>
Note:  declarations declared as <tt>const</tt> are wrapped as read-only variables and
will be accessed using the <tt>cvar</tt> object described in the previous section.  They
are not wrapped as constants.   For further discussion about this, see the <a href="SWIG.html#SWIG">SWIG Basics</a> chapter.
</p>

<p>
Constants are not guaranteed to remain constant in Tcl---the value
of the constant could be accidentally reassigned.You will just have to be careful.
</p>

<p>
A peculiarity of installing constants as variables is that it is necessary to use the Tcl <tt>global</tt> statement to
access constants in procedure bodies.  For example:
</p>

<div class="code">
<pre>
proc blah {} {
   global FOO
   bar $FOO
}
</pre>
</div>

<p>
If a program relies on a lot of constants, this can be extremely annoying.  To fix the problem, consider using the
following typemap rule:
</p>

<div class="code">
<pre>
%apply int CONSTANT { int x };
#define FOO 42
...
void bar(int x);
</pre>
</div>

<p>
When applied to an input argument, the <tt>CONSTANT</tt> rule allows a constant to be passed to a function using
its actual value or a symbolic identifier name.  For example:
</p>

<div class="code">
<pre>
proc blah {} {
   bar FOO
}
</pre>
</div>

<p>
When an identifier name is given, it is used to perform an implicit hash-table lookup of the value during argument 
conversion.  This allows the <tt>global</tt> statement to be omitted.
</p>

877
<H3><a name="Tcl_nn19"></a>40.3.5 Pointers</H3>
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972


<p>
C/C++ pointers are fully supported by SWIG.  Furthermore, SWIG has no problem working with
incomplete type information.  Here is a rather simple interface:
</p>

<div class="code">
<pre>
%module example

FILE *fopen(const char *filename, const char *mode);
int fputs(const char *, FILE *);
int fclose(FILE *);
</pre>
</div>

<p>
When wrapped, you will be able to use the functions in a natural way from Tcl. 
For example:
</p>

<div class="code">
<pre>
% load ./example.so
% set f [fopen junk w]
% fputs "Hello World\n" $f
% fclose $f
</pre>
</div>

<p>
If this makes you uneasy, rest assured that there is no
deep magic involved.  Underneath the covers, pointers to C/C++ objects are
simply represented as opaque values--normally an encoded character
string like this:
</p>

<div class="code"><pre>
% puts $f
_c0671108_p_FILE
% 
</pre></div>

<p>
This pointer value can be freely passed around to different C functions that
expect to receive an object of type <tt>FILE *</tt>.  The only thing you can't do is 
dereference the pointer from Tcl.
</p>

<p>
The NULL pointer is represented by the string <tt>NULL</tt>.
</p>

<p>
As much as you might be inclined to modify a pointer value directly
from Tcl, don't.  The hexadecimal encoding is not necessarily the
same as the logical memory address of the underlying object.  Instead
it is the raw byte encoding of the pointer value.  The encoding will
vary depending on the native byte-ordering of the platform (i.e.,
big-endian vs. little-endian).  Similarly, don't try to manually cast
a pointer to a new type by simply replacing the type-string.  This
may not work like you expect and it is particularly dangerous when
casting C++ objects. If you need to cast a pointer or
change its value, consider writing some helper functions instead.  For
example:
</p>

<div class="code">
<pre>
%inline %{
/* C-style cast */
Bar *FooToBar(Foo *f) {
   return (Bar *) f;
}

/* C++-style cast */
Foo *BarToFoo(Bar *b) {
   return dynamic_cast&lt;Foo*&gt;(b);
}

Foo *IncrFoo(Foo *f, int i) {
    return f+i;
}
%}
</pre>
</div>

<p>
Also, if working with C++, you should always try
to use the new C++ style casts.  For example, in the above code, the
C-style cast may return a bogus result whereas as the C++-style cast will return
<tt>None</tt> if the conversion can't be performed.
</p>

973
<H3><a name="Tcl_nn20"></a>40.3.6 Structures</H3>
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254


<p>
If you wrap a C structure, it is wrapped by a Tcl interface that somewhat resembles a Tk widget.
This provides a very natural interface.  For example,
</p>

<div class="code"><pre>
struct Vector {
	double x,y,z;
};

</pre></div>

<p>
is used as follows:
</p>

<div class="code"><pre>
% Vector v
% v configure -x 3.5 -y 7.2
% puts "[v cget -x] [v cget -y] [v cget -z]"
3.5 7.2 0.0
% 
</pre></div>

<p>
Similar access is provided for unions and the data members of C++ classes.
</p>

<p>
In the above example, <tt>v</tt> is a name that's used for the object.  However,
underneath the covers, there's a pointer to a raw C structure.  This can be obtained
by looking at the <tt>-this</tt> attribute.  For example:
</p>

<div class="code">
<pre>
% puts [v cget -this]
_88e31408_p_Vector
</pre>
</div>

<p>
Further details about the relationship between the Tcl and the underlying C structure
are covered a little later.
</p>

<p>
<tt>const</tt> members of a structure are read-only. Data members
can also be forced to be read-only using the <tt>%immutable</tt> directive. For example:
</p>

<div class="code">
<pre>
struct Foo {
   ...
   %immutable;
   int x;        /* Read-only members */
   char *name;
   %mutable;
   ...
};
</pre>
</div>

<p>
When <tt>char *</tt> members of a structure are wrapped, the contents are assumed to be
dynamically allocated using <tt>malloc</tt> or <tt>new</tt> (depending on whether or not
SWIG is run with the -c++ option).   When the structure member is set, the old contents will be 
released and a new value created.   If this is not the behavior you want, you will have to use
a typemap (described later).
</p>

<p>
If a structure contains arrays, access to those arrays is managed through pointers.  For
example, consider this:
</p>

<div class="code">
<pre>
struct Bar {
    int  x[16];
};
</pre>
</div>

<p>
If accessed in Tcl, you will see behavior like this:
</p>

<div class="code">
<pre>
% Bar b
% puts [b cget -x]
_801861a4_p_int
% 
</pre>
</div>

<p>
This pointer can be passed around to functions that expect to receive
an <tt>int *</tt> (just like C).   You can also set the value of an array member using
another pointer.  For example:
</p>

<div class="code">
<pre>
% Bar c
% c configure -x [b cget -x]   # Copy contents of b.x to c.x
</pre>
</div>

<p>
For array assignment, SWIG copies the entire contents of the array starting with the data pointed
to by <tt>b.x</tt>.   In this example, 16 integers would be copied.  Like C, SWIG makes
no assumptions about bounds checking---if you pass a bad pointer, you may get a segmentation
fault or access violation.
</p>

<p>
When a member of a structure is itself a structure, it is handled as a
pointer.  For example, suppose you have two structures like this:
</p>

<div class="code">
<pre>
struct Foo {
   int a;
};

struct Bar {
   Foo f;
};
</pre>
</div>

<p>
Now, suppose that you access the <tt>f</tt> attribute of <tt>Bar</tt> like this:
</p>

<div class="code">
<pre>
% Bar b
% set x [b cget -f]
</pre>
</div>

<p>
In this case, <tt>x</tt> is a pointer that points to the <tt>Foo</tt> that is inside <tt>b</tt>.
This is the same value as generated by this C code:
</p>

<div class="code">
<pre>
Bar b;
Foo *x = &amp;b-&gt;f;       /* Points inside b */
</pre>
</div>

<p>
However, one peculiarity of accessing a substructure like this is that the returned
value does work quite like you might expect.  For example:
</p>

<div class="code">
<pre>
% Bar b
% set x [b cget -f]
% x cget -a
invalid command name "x"
</pre>
</div>

<p>
This is because the returned value was not created in a normal way from the interpreter (x is 
not a command object). To make it function normally, just
evaluate the variable like this:
</p>

<div class="code">
<pre>
% Bar b
% set x [b cget -f]
% $x cget -a
0
%
</pre>
</div>

<p>
In this example, <tt>x</tt> points inside the original structure.  This means that modifications
work just like you would expect.  For example:
</p>

<div class="code">
<pre>

% Bar b
% set x [b cget -f]
% $x configure -a 3            # Modifies contents of f (inside b)
% [b cget -f] -configure -a 3  # Same thing
</pre>
</div>

<p>
In many of these structure examples, a simple name like "v" or "b" has been given
to wrapped structures.  If necessary, this name can be passed to functions
that expect to receive an object.  For example, if you have a function like this,
</p>

<div class="code">
<pre>
void blah(Foo *f);
</pre>
</div>

<p>
you can call the function in Tcl as follows:
</p>

<div class="code">
<pre>
% Foo x            # Create a Foo object 
% blah x           # Pass the object to a function
</pre>
</div>

<p>
It is also possible to call the function using the raw pointer value. For
instance:
</p>

<div class="code">
<pre>
% blah [x cget -this]   # Pass object to a function
</pre>
</div>

<p>
It is also possible to create and use objects using variables.  For example:
</p>

<div class="code">
<pre>
% set b [Bar]            # Create a Bar
% $b cget -f             # Member access
% puts $b
_108fea88_p_Bar
%
</pre>
</div>

<p>
Finally, to destroy objects created from Tcl, you can either let the object
name go out of scope or you can explicitly delete the object.  For example:
</p>

<div class="code">
<pre>
% Foo f                 # Create object f
% rename f ""
</pre>
</div>

<p>
or
</p>

<div class="code">
<pre>
% Foo f                 # Create object f
% f -delete
</pre>
</div>

<p>
Note: Tcl only destroys the underlying object if it has ownership.  See the
memory management section that appears shortly.
</p>

1255
<H3><a name="Tcl_nn21"></a>40.3.7 C++ classes</H3>
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285


<p>
C++ classes are wrapped as an extension of structure wrapping. For example, if you have this class,
</p>

<div class="code"><pre>
class List {
public:
  List();
  ~List();
  int  search(char *item);
  void insert(char *item);
  void remove(char *item);
  char *get(int n);
  int  length;
};
</pre></div>

<p>
you can use it in Tcl like this:
</p>

<div class="code"><pre>
% List x
% x insert Ale
% x insert Stout
% x insert Lager
% x get 1
Stout
1286
% puts [x cget -length]
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
3
%
</pre></div>

<p>
Class data members are accessed in the same manner as C structures.  
</p>

<p>
Static class members are accessed as global functions or variables.
To illustrate, suppose you have a class like this:
</p>

<div class="code">
<pre>
class Spam {
public:
   static void foo();
   static int bar;

};
</pre>
</div>

<p>
In Tcl, the static member is accessed as follows:
</p>

<div class="code">
<pre>
% Spam_foo        # Spam::foo()
% puts $Spam_bar  # Spam::bar
</pre>
</div>

1322
<H3><a name="Tcl_nn22"></a>40.3.8 C++ inheritance</H3>
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370


<p>
SWIG is fully aware of issues related to C++ inheritance.  Therefore, if you have
classes like this
</p>

<div class="code">
<pre>
class Foo {
...
};

class Bar : public Foo {
...
};
</pre>
</div>

<p>
An object of type <tt>Bar</tt> can be used where a <tt>Foo</tt> is expected.  For
example, if you have this function:
</p>

<div class="code">
<pre>
void spam(Foo *f);
</pre>
</div>

<p>
then the function <tt>spam()</tt> accepts a <tt>Foo *</tt> or a pointer to any class derived from <tt>Foo</tt>.
For instance:
</p>

<div class="code">
<pre>
% Foo f      # Create a Foo
% Bar b      # Create a Bar
% spam f     # OK
% spam b     # OK
</pre>
</div>

<p>
It is safe to use multiple inheritance with SWIG.
</p>

1371
<H3><a name="Tcl_nn23"></a>40.3.9 Pointers, references, values, and arrays</H3>
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424


<p>
In C++, there are many different ways a function might receive
and manipulate objects.  For example:
</p>

<div class="code">
<pre>
void spam1(Foo *x);      // Pass by pointer
void spam2(Foo &amp;x);      // Pass by reference
void spam3(Foo x);       // Pass by value
void spam4(Foo x[]);     // Array of objects
</pre>
</div>

<p>
In Tcl, there is no detailed distinction like this.
Because of this, SWIG unifies all of these types
together in the wrapper code.  For instance, if you actually had the
above functions, it is perfectly legal to do this:
</p>

<div class="code">
<pre>
% Foo f             # Create a Foo
% spam1 f           # Ok. Pointer
% spam2 f           # Ok. Reference
% spam3 f           # Ok. Value.
% spam4 f           # Ok. Array (1 element)
</pre>
</div>

<p>
Similar behavior occurs for return values.  For example, if you had
functions like this,
</p>

<div class="code">
<pre>
Foo *spam5();
Foo &amp;spam6();
Foo  spam7();
</pre>
</div>

<p>
then all three functions will return a pointer to some <tt>Foo</tt> object.
Since the third function (spam7) returns a value, newly allocated memory is used 
to hold the result and a pointer is returned (Tcl will release this memory 
when the return value is garbage collected).
</p>

1425
<H3><a name="Tcl_nn24"></a>40.3.10 C++ overloaded functions</H3>
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547


<p>
C++ overloaded functions, methods, and constructors are mostly supported by SWIG.  For example,
if you have two functions like this:
</p>

<div class="code">
<pre>
void foo(int);
void foo(char *c);
</pre>
</div>

<p>
You can use them in Tcl in a straightforward manner:
</p>

<div class="code">
<pre>
% foo 3            # foo(int)
% foo Hello        # foo(char *c)
</pre>
</div>

<p>
Similarly, if you have a class like this,
</p>

<div class="code">
<pre>
class Foo {
public:
    Foo();
    Foo(const Foo &amp;);
    ...
};
</pre>
</div>

<p>
you can write Tcl code like this:
</p>

<div class="code">
<pre>
% Foo f                # Create a Foo
% Foo g f              # Copy f
</pre>
</div>

<p>
Overloading support is not quite as flexible as in C++. Sometimes there are methods that SWIG
can't disambiguate. For example:
</p>

<div class="code">
<pre>
void spam(int);
void spam(short);
</pre>
</div>

<p>
or
</p>

<div class="code">
<pre>
void foo(Bar *b);
void foo(Bar &amp;b);
</pre>
</div>

<p>
If declarations such as these appear, you will get a warning message like this:
</p>

<div class="code">
<pre>
example.i:12: Warning 509: Overloaded method spam(short) effectively ignored,
example.i:11: Warning 509: as it is shadowed by spam(int).
</pre>
</div>

<p>
To fix this, you either need to ignore or rename one of the methods.  For example:
</p>

<div class="code">
<pre>
%rename(spam_short) spam(short);
...
void spam(int);    
void spam(short);   // Accessed as spam_short
</pre>
</div>

<p>
or
</p>

<div class="code">
<pre>
%ignore spam(short);
...
void spam(int);    
void spam(short);   // Ignored
</pre>
</div>

<p>
SWIG resolves overloaded functions and methods using a disambiguation scheme that ranks and sorts
declarations according to a set of type-precedence rules.    The order in which declarations appear
in the input does not matter except in situations where ambiguity arises--in this case, the
first declaration takes precedence.
</p>

<p>
Please refer to the "SWIG and C++" chapter for more information about overloading. 
</p>

1548
<H3><a name="Tcl_nn25"></a>40.3.11 C++ operators</H3>
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649


<p>
Certain C++ overloaded operators can be handled automatically by SWIG.  For example,
consider a class like this:
</p>

<div class="code">
<pre>
class Complex {
private:
  double rpart, ipart;
public:
  Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { }
  Complex(const Complex &amp;c) : rpart(c.rpart), ipart(c.ipart) { }
  Complex &amp;operator=(const Complex &amp;c);
  Complex operator+(const Complex &amp;c) const;
  Complex operator-(const Complex &amp;c) const;
  Complex operator*(const Complex &amp;c) const;
  Complex operator-() const;
  
  double re() const { return rpart; }
  double im() const { return ipart; }
};
</pre>
</div>

<p>
When wrapped, it works like this:
</p>

<div class="code">
<pre>
% Complex c 3 4
% Complex d 7 8
% set e [c + d]
% $e re
10.0
% $e im
12.0
</pre>
</div>

<p>
It should be stressed that operators in SWIG have no relationship to operators
in Tcl.  In fact, the only thing that's happening here is that an operator like
<tt>operator +</tt> has been renamed to a method <tt>+</tt>.    Therefore, the
statement <tt>[c + d]</tt> is really just invoking the <tt>+</tt> method on <tt>c</tt>.
When more than operator is defined (with different arguments), the standard
method overloading facilities are used.  Here is a rather odd looking example:
</p>

<div class="code">
<pre>
% Complex c 3 4
% Complex d 7 8
% set e [c - d]       # operator-(const Complex &amp;)
% puts "[$e re] [$e im]"
10.0 12.0
% set f [c -]         # operator-()
% puts "[$f re] [$f im]"
-3.0 -4.0
%
</pre>
</div>

<p>
One restriction with operator overloading support is that SWIG is not
able to fully handle operators that aren't defined as part of the class.
For example, if you had code like this
</p>

<div class="code">
<pre>
class Complex {
...
friend Complex operator+(double, const Complex &amp;c);
...
};
</pre>
</div>

<p>
then SWIG doesn't know what to do with the friend function--in fact,
it simply ignores it and issues a warning.   You can still wrap the operator,
but you may have to encapsulate it in a special function.  For example:
</p>

<div class="code">
<pre>
%rename(Complex_add_dc) operator+(double, const Complex &amp;);
...
Complex operator+(double, const Complex &amp;c);
</pre>
</div>

<p>
There are ways to make this operator appear as part of the class using the <tt>%extend</tt> directive.
Keep reading.
</p>

1650
<H3><a name="Tcl_nn26"></a>40.3.12 C++ namespaces</H3>
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713


<p>
SWIG is aware of C++ namespaces, but namespace names do not appear in
the module nor do namespaces result in a module that is broken up into
submodules or packages.  For example, if you have a file like this,
</p>

<div class="code">
<pre>
%module example

namespace foo {
   int fact(int n);
   struct Vector {
       double x,y,z;
   };
};
</pre>
</div>

<p>
it works in Tcl as follows:
</p>

<div class="code">
<pre>
% load ./example.so
% fact 3
6
% Vector v
% v configure -x 3.4
</pre>
</div>

<p>
If your program has more than one namespace, name conflicts (if any) can be resolved using <tt>%rename</tt>
For example:
</p>

<div class="code">
<pre>
%rename(Bar_spam) Bar::spam;

namespace Foo {
    int spam();
}

namespace Bar {
    int spam();
}
</pre>
</div>

<p>
If you have more than one namespace and your want to keep their
symbols separate, consider wrapping them as separate SWIG modules.
For example, make the module name the same as the namespace and create
extension modules for each namespace separately.  If your program
utilizes thousands of small deeply nested namespaces each with
identical symbol names, well, then you get what you deserve.
</p>

1714
<H3><a name="Tcl_nn27"></a>40.3.13 C++ templates</H3>
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765


<p>
C++ templates don't present a huge problem for SWIG.  However, in order
to create wrappers, you have to tell SWIG to create wrappers for a particular
template instantiation.  To do this, you use the <tt>%template</tt> directive.
For example:
</p>

<div class="code">
<pre>
%module example
%{
#include "pair.h"
%}

template&lt;class T1, class T2&gt;
struct pair {
   typedef T1 first_type;
   typedef T2 second_type;
   T1 first;
   T2 second;
   pair();
   pair(const T1&amp;, const T2&amp;);
  ~pair();
};

%template(pairii) pair&lt;int,int&gt;;
</pre>
</div>

<p>
In Tcl:
</p>

<div class="code">
<pre>
% pairii p 3 4
% p cget -first
3
% p cget -second
4
</pre>
</div>

<p>
Obviously, there is more to template wrapping than shown in this example.
More details can be found in the <a href="SWIGPlus.html#SWIGPlus">SWIG and C++</a> chapter.   Some more complicated
examples will appear later.
</p>

1766
<H3><a name="Tcl_nn28"></a>40.3.14 C++ Smart Pointers</H3>
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849


<p>
In certain C++ programs, it is common to use classes that have been wrapped by
so-called "smart pointers."   Generally, this involves the use of a template class
that implements <tt>operator-&gt;()</tt> like this:
</p>

<div class="code">
<pre>
template&lt;class T&gt; class SmartPtr {
   ...
   T *operator-&gt;();
   ...
}
</pre>
</div>

<p>
Then, if you have a class like this,
</p>

<div class="code">
<pre>
class Foo {
public:
     int x;
     int bar();
};
</pre>
</div>

<p>
A smart pointer would be used in C++ as follows:
</p>

<div class="code">
<pre>
SmartPtr&lt;Foo&gt; p = CreateFoo();   // Created somehow (not shown)
...
p-&gt;x = 3;                        // Foo::x
int y = p-&gt;bar();                // Foo::bar
</pre>
</div>

<p>
To wrap this in Tcl, simply tell SWIG about the <tt>SmartPtr</tt> class and the low-level
<tt>Foo</tt> object.  Make sure you instantiate <tt>SmartPtr</tt> using <tt>%template</tt> if necessary.
For example:
</p>

<div class="code">
<pre>
%module example
...
%template(SmartPtrFoo) SmartPtr&lt;Foo&gt;;
...
</pre>
</div>

<p>
Now, in Tcl, everything should just "work":
</p>

<div class="code">
<pre>
% set p [CreateFoo]                  # Create a smart-pointer somehow
% $p configure -x 3                  # Foo::x
% $p bar                             # Foo::bar
</pre>
</div>

<p>
If you ever need to access the underlying pointer returned by <tt>operator-&gt;()</tt> itself,
simply use the <tt>__deref__()</tt> method.  For example:
</p>

<div class="code">
<pre>
% set f [$p __deref__]    # Returns underlying Foo *
</pre>
</div>

1850
<H2><a name="Tcl_nn29"></a>40.4 Further details on the Tcl class interface</H2>
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862


<p>
In the previous section, a high-level view of Tcl wrapping was
presented.  A key component of this wrapping is that structures and
classes are wrapped by Tcl class-like objects. This provides a very
natural Tcl interface and allows SWIG to support a number of
advanced features such as operator overloading.   However, a number
of low-level details were omitted.  This section provides a brief overview
of how the proxy classes work.
</p>

1863
<H3><a name="Tcl_nn30"></a>40.4.1 Proxy classes</H3>
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927


<p>
In the <a href="SWIG.html#SWIG">"SWIG basics"</a> and <a href="SWIGPlus.html#SWIGPlus">"SWIG and C++"</a> chapters,
details of low-level structure and class wrapping are described.  To summarize those chapters, if you
have a class like this
</p>

<div class="code">
<pre>
class Foo {
public:
     int x;
     int spam(int);
     ...
</pre>
</div>

<p>
then SWIG transforms it into a set of low-level procedural wrappers. For example:
</p>

<div class="code">
<pre>
Foo *new_Foo() {
    return new Foo();
}
void delete_Foo(Foo *f) {
    delete f;
}
int Foo_x_get(Foo *f) {
    return f-&gt;x;
}
void Foo_x_set(Foo *f, int value) {
    f-&gt;x = value;
}
int Foo_spam(Foo *f, int arg1) {
    return f-&gt;spam(arg1);
}
</pre>
</div>

<p>
These wrappers are actually found in the Tcl extension module.  For example, you can certainly do this:
</p>

<div class="code">
<pre>
% load ./example.so
% set f [new_Foo]
% Foo_x_get $f
0
% Foo_spam $f 3
1
%
</pre>
</div>

<p>
However, in addition to this, the classname <tt>Foo</tt> is used as an object constructor
function.   This allows objects to be encapsulated objects that look a lot like Tk widgets
as shown in the last section.
</p>

1928
<H3><a name="Tcl_nn31"></a>40.4.2 Memory management</H3>
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115


<p>
Associated with each wrapped object, is an ownership flag <tt>thisown</tt>   The value of this
flag determines who is responsible for deleting the underlying C++ object.  If set to 1,
the Tcl interpreter destroys the C++ object when the proxy class is 
garbage collected.   If set to 0 (or if the attribute is missing), then the destruction
of the proxy class has no effect on the C++ object.
</p>

<p>
When an object is created by a constructor or returned by value, Tcl automatically takes
ownership of the result.  For example:
</p>

<div class="code">
<pre>
class Foo {
public:
    Foo();
    Foo bar();
};
</pre>
</div>

<p>
In Tcl:
</p>

<div class="code">
<pre>
% Foo f
% f cget -thisown
1
% set g [f bar]
% $g cget -thisown
1
</pre>
</div>

<p>
On the other hand, when pointers are returned to Tcl, there is often no way to know where
they came from.  Therefore, the ownership is set to zero.  For example:
</p>

<div class="code">
<pre>
class Foo {
public:
    ...
    Foo *spam();
    ...
};
</pre>
</div>

<br>

<div class="code">
<pre>
% Foo f
% set s [f spam]
% $s cget -thisown
0
% 
</pre>
</div>

<p>
This behavior is especially important for classes that act as
containers.  For example, if a method returns a pointer to an object
that is contained inside another object, you definitely don't want
Tcl to assume ownership and destroy it!
</p>

<p>
Related to containers, ownership issues can arise whenever an object is assigned to a member
or global variable.  For example, consider this interface:
</p>

<div class="code">
<pre>
%module example

struct Foo {
    int  value;
    Foo  *next;
};

Foo *head = 0;
</pre>
</div>

<p>
When wrapped in Tcl, careful observation will reveal that ownership changes whenever an object
is assigned to a global variable.  For example:
</p>

<div class="code">
<pre>
% Foo f
% f cget -thisown
1
% set head f
% f cget -thisown
0
</pre>
</div>

<p>
In this case, C is now holding a reference to the object---you probably don't want Tcl to destroy it.
Similarly, this occurs for members.  For example:
</p>

<div class="code">
<pre>
% Foo f
% Foo g
% f cget -thisown
1
% g cget -thisown
1
% f configure -next g
% g cget -thisown 
0
%
</pre>
</div>

<p>
For the most part, memory management issues remain hidden.  However,
there are occasionally situations where you might have to manually
change the ownership of an object.  For instance, consider code like this:
</p>

<div class="code">
<pre>
class Node {
   Object *value;
public:
   void set_value(Object *v) { value = v; }
   ...
};
</pre>
</div>

<p>
Now, consider the following Tcl code:
</p>

<div class="code">
<pre>
% Object v                 # Create an object
% Node n                   # Create a node
% n setvalue v             # Set value
% v cget -thisown
1
% 
</pre>
</div>

<p>
In this case, the object <tt>n</tt> is holding a reference to
<tt>v</tt> internally.  However, SWIG has no way to know that this
has occurred.  Therefore, Tcl still thinks that it has ownership of the
object.  Should the proxy object be destroyed, then the C++ destructor
will be invoked and <tt>n</tt> will be holding a stale-pointer.  If
you're lucky, you will only get a segmentation fault.
</p>

<p>
To work around this, it is always possible to flip the ownership flag. For example,
</p>

<div class="code">
<pre>
% v -disown              # Give ownership to C/C++
% v -acquire             # Acquire ownership
</pre>
</div>

<p>
It is also possible to deal with situations like this using
typemaps--an advanced topic discussed later.
</p>


2116
<H2><a name="Tcl_nn32"></a>40.5 Input and output parameters</H2>
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303


<p>
A common problem in some C programs is handling parameters passed as simple pointers.  For
example:
</p>

<div class="code">
<pre>
void add(int x, int y, int *result) {
   *result = x + y;
}
</pre>
</div>

<p>
or perhaps
</p>

<div class="code">
<pre>
int sub(int *x, int *y) {
   return *x+*y;
}
</pre>
</div>

<p>
The easiest way to handle these situations is to use the <tt>typemaps.i</tt> file.  For example:
</p>

<div class="code">
<pre>
%module example
%include "typemaps.i"

void add(int, int, int *OUTPUT);
int  sub(int *INPUT, int *INPUT);
</pre>
</div>

<p>
In Tcl, this allows you to pass simple values instead of pointer.  For example:
</p>

<div class="code">
<pre>
set a [add 3 4]
puts $a
7
</pre>
</div>

<p>
Notice how the <tt>INPUT</tt> parameters allow integer values to be passed instead of pointers
and how the <tt>OUTPUT</tt> parameter creates a return result.
</p>

<p>
If you don't want to use the names <tt>INPUT</tt> or <tt>OUTPUT</tt>, use the <tt>%apply</tt>
directive.  For example:
</p>

<div class="code">
<pre>
%module example
%include "typemaps.i"

%apply int *OUTPUT { int *result };
%apply int *INPUT  { int *x, int *y};

void add(int x, int y, int *result);
int  sub(int *x, int *y);
</pre>
</div>

<p>
If a function mutates one of its parameters like this,
</p>

<div class="code">
<pre>
void negate(int *x) {
   *x = -(*x);
}
</pre>
</div>

<p>
you can use <tt>INOUT</tt> like this:
</p>

<div class="code">
<pre>
%include "typemaps.i"
...
void negate(int *INOUT);
</pre>
</div>

<p>
In Tcl, a mutated parameter shows up as a return value.  For example:
</p>

<div class="code">
<pre>
set a [negate 3]
puts $a
-3
</pre>
</div>

<p>
The most common use of these special typemap rules is to handle functions that
return more than one value.   For example, sometimes a function returns a result
as well as a special error code:
</p>

<div class="code">
<pre>
/* send message, return number of bytes sent, along with success code */
int send_message(char *text, int len, int *success);
</pre>
</div>

<p>
To wrap such a function, simply use the <tt>OUTPUT</tt> rule above. For example:
</p>

<div class="code">
<pre>
%module example
%include "typemaps.i"
%apply int *OUTPUT { int *success };
...
int send_message(char *text, int *success);
</pre>
</div>

<p>
When used in Tcl, the function will return multiple values as a list.  
</p>

<div class="code">
<pre>
set r [send_message "Hello World"]
set bytes [lindex $r 0]
set success [lindex $r 1]
</pre>
</div>

<p>
Another common use of multiple return values are in query functions.  For example:
</p>

<div class="code">
<pre>
void get_dimensions(Matrix *m, int *rows, int *columns);
</pre>
</div>

<p>
To wrap this, you might use the following:
</p>

<div class="code">
<pre>
%module example
%include "typemaps.i"
%apply int *OUTPUT { int *rows, int *columns };
...
void get_dimensions(Matrix *m, int *rows, *columns);
</pre>
</div>

<p>
Now, in Perl:
</p>

<div class="code">
<pre>
set dim [get_dimensions $m]
set r  [lindex $dim 0]
set c  [lindex $dim 1]
</pre>
</div>

2304
<H2><a name="Tcl_nn33"></a>40.6 Exception handling </H2>
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367


<p>
The <tt>%exception</tt> directive can be used to create a user-definable
exception handler in charge of converting exceptions in your C/C++
program into Tcl exceptions.  The chapter on customization features
contains more details, but suppose you extended the array example into
a C++ class like the following :
</p>

<div class="code"><pre>
class RangeError {};   // Used for an exception

class DoubleArray {
  private:
    int n;
    double *ptr;
  public:
    // Create a new array of fixed size
    DoubleArray(int size) {
      ptr = new double[size];
      n = size;
    }
    // Destroy an array
    ~DoubleArray() {
       delete ptr;
    }
    // Return the length of the array
    int   length() {
      return n;
    }

    // Get an item from the array and perform bounds checking.
    double getitem(int i) {
      if ((i &gt;= 0) &amp;&amp; (i &lt; n))
        return ptr[i];
      else
        throw RangeError();
    }

    // Set an item in the array and perform bounds checking.
    void setitem(int i, double val) {
      if ((i &gt;= 0) &amp;&amp; (i &lt; n))
        ptr[i] = val;
      else {
        throw RangeError();
      }
    }
  };
</pre></div>

<p>
The functions associated with this class can throw a C++ range
exception for an out-of-bounds array access.  We can catch this in our
Tcl extension by specifying the following in an interface file :
</p>

<div class="code"><pre>
%exception {
  try {
    $action                // Gets substituted by actual function call
  }
  catch (RangeError) {
2368
    Tcl_SetResult(interp, (char *)"Array index out-of-bounds", TCL_STATIC);
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
    return TCL_ERROR;
  }
}
</pre></div>

<p>
As shown, the exception handling code will be added to every wrapper function.
Since this is somewhat inefficient.  You might consider refining the 
exception handler to only apply to specific methods like this:
</p>

<div class="code">
<pre>
%exception getitem {
  try {
    $action
  }
  catch (RangeError) {
2387
    Tcl_SetResult(interp, (char *)"Array index out-of-bounds", TCL_STATIC);
2388 2389 2390 2391 2392 2393 2394 2395 2396
    return TCL_ERROR;
  }
}

%exception setitem {
  try {
    $action
  }
  catch (RangeError) {
2397
    Tcl_SetResult(interp, (char *)"Array index out-of-bounds", TCL_STATIC);
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
    return TCL_ERROR;
  }
}
</pre>
</div>

<p>
In this case, the exception handler is only attached to methods and functions
named <tt>getitem</tt> and <tt>setitem</tt>.
</p>

<p>
If you had a lot of different methods, you can avoid extra typing by using a macro.
For example:
</p>

<div class="code">
<pre>
%define RANGE_ERROR
{
  try {
    $action
  }
  catch (RangeError) {
2422
    Tcl_SetResult(interp, (char *)"Array index out-of-bounds", TCL_STATIC);
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
    return TCL_ERROR;
  }
}
%enddef

%exception getitem RANGE_ERROR;
%exception setitem RANGE_ERROR;
</pre>
</div>

<p>
Since SWIG's exception handling is user-definable, you are not limited to C++ exception handling.
See the chapter on "<a href="Customization.html#Customization">Customization Features</a>" for more examples.
</p>

2438
<H2><a name="Tcl_nn34"></a>40.7 Typemaps</H2>
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454


<p>
This section describes how you can modify SWIG's default wrapping behavior
for various C/C++ datatypes using the <tt>%typemap</tt> directive.   This
is an advanced topic that assumes familiarity with the Tcl C API as well
as the material in the "<a href="Typemaps.html#Typemaps">Typemaps</a>" chapter.
</p>

<p>
Before proceeding, it should be stressed that typemaps are not a required 
part of using SWIG---the default wrapping behavior is enough in most cases.
Typemaps are only used if you want to change some aspect of the primitive
C-Tcl interface.
</p>

2455
<H3><a name="Tcl_nn35"></a>40.7.1 What is a typemap?</H3>
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571


<p>
A typemap is nothing more than a code generation rule that is attached to 
a specific C datatype.   For example, to convert integers from Tcl to C,
you might define a typemap like this:
</p>

<div class="code"><pre>
%module example

%typemap(in) int {
        if (Tcl_GetIntFromObj(interp,$input,&amp;$1) == TCL_ERROR) return TCL_ERROR;
	printf("Received an integer : %d\n",$1);
}
%inline %{
extern int fact(int n);
%}
</pre></div>

<p>
Typemaps are always associated with some specific aspect of code generation.
In this case, the "in" method refers to the conversion of input arguments
to C/C++.  The datatype <tt>int</tt> is the datatype to which the typemap
will be applied.  The supplied C code is used to convert values.  In this
code a number of special variable prefaced by a <tt>$</tt> are used.  The
<tt>$1</tt> variable is placeholder for a local variable of type <tt>int</tt>.
The <tt>$input</tt> variable is the input object of type <tt>Tcl_Obj *</tt>.
</p>

<p>
When this example is compiled into a Tcl module, it operates as follows:
</p>

<div class="code"><pre>
% load ./example.so
% fact 6
Received an integer : 6
720
</pre></div>

<p>
In this example, the typemap is applied to all occurrences of the <tt>int</tt> datatype.
You can refine this by supplying an optional parameter name.  For example:
</p>

<div class="code"><pre>
%module example

%typemap(in) int n {
        if (Tcl_GetIntFromObj(interp,$input,&amp;$1) == TCL_ERROR) return TCL_ERROR;
	printf("n = %d\n",$1);
}
%inline %{
extern int fact(int n);
%}
</pre></div>

<p>
In this case, the typemap code is only attached to arguments that exactly match <tt>int n</tt>.
</p>

<p>
The application of a typemap to specific datatypes and argument names involves
more than simple text-matching--typemaps are fully integrated into the
SWIG type-system.   When you define a typemap for <tt>int</tt>, that typemap
applies to <tt>int</tt> and qualified variations such as <tt>const int</tt>.  In addition,
the typemap system follows <tt>typedef</tt> declarations.  For example:
</p>

<div class="code">
<pre>
%typemap(in) int n {
        if (Tcl_GetIntFromObj(interp,$input,&amp;$1) == TCL_ERROR) return TCL_ERROR;
	printf("n = %d\n",$1);
}
%inline %{
typedef int Integer;
extern int fact(Integer n);    // Above typemap is applied
%}
</pre>
</div>

<p>
However, the matching of <tt>typedef</tt> only occurs in one direction.  If you
defined a typemap for <tt>Integer</tt>, it is not applied to arguments of
type <tt>int</tt>.
</p>

<p>
Typemaps can also be defined for groups of consecutive arguments.  For example:
</p>

<div class="code">
<pre>
%typemap(in) (char *str, int len) {
    $1 = Tcl_GetStringFromObj($input,&amp;$2);
};

int count(char c, char *str, int len);
</pre>
</div>

<p>
When a multi-argument typemap is defined, the arguments are always handled as a single
Tcl object.  This allows the function to be used like this (notice how the length
parameter is omitted):
</p>

<div class="code">
<pre>
% count e "Hello World"
1
</pre>
</div>

2572
<H3><a name="Tcl_nn36"></a>40.7.2 Tcl typemaps</H3>
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709


<p>
The previous section illustrated an "in" typemap for converting Tcl objects to C.
A variety of different typemap methods are defined by the Tcl module.  For example,
to convert a C integer back into a Tcl object, you might define an "out" typemap
like this:
</p>

<div class="code">
<pre>
%typemap(out) int {
    Tcl_SetObjResult(interp,Tcl_NewIntObj($1));
}
</pre>
</div>

<p>
The following list details all of the typemap methods that can be used by the Tcl module:
</p>

<p>
<tt>%typemap(in)</tt>
</p>

<div class="indent">
Converts Tcl objects to input function arguments
</div>

<p>
<tt>%typemap(out)</tt>
</p>

<div class="indent">
Converts return value of a C function to a Tcl object
</div>

<p>
<tt>%typemap(varin)</tt>
</p>

<div class="indent">
Assigns a C global variable from a Tcl object
</div>

<p>
<tt>%typemap(varout)</tt>
</p>

<div class="indent">
Returns a C global variable as a Tcl object
</div>

<p>
<tt>%typemap(freearg)</tt>
</p>

<div class="indent">
Cleans up a function argument (if necessary)
</div>

<p>
<tt>%typemap(argout)</tt>
</p>

<div class="indent">
Output argument processing
</div>

<p>
<tt>%typemap(ret)</tt>
</p>

<div class="indent">
Cleanup of function return values
</div>

<p>
<tt>%typemap(consttab)</tt>
</p>

<div class="indent">
Creation of Tcl constants (constant table)
</div>

<p>
<tt>%typemap(constcode)</tt> 
</p>

<div class="indent">
Creation of Tcl constants (init function)
</div>

<p>
<tt>%typemap(memberin)</tt>
</p>

<div class="indent">
Setting of structure/class member data
</div>

<p>
<tt>%typemap(globalin)</tt>
</p>

<div class="indent">
Setting of C global variables
</div>

<p>
<tt>%typemap(check)</tt>
</p>

<div class="indent">
Checks function input values.
</div>

<p>
<tt>%typemap(default)</tt>
</p>

<div class="indent">
Set a default value for an argument (making it optional).
</div>

<p>
<tt>%typemap(arginit)</tt>
</p>

<div class="indent">
Initialize an argument to a value before any conversions occur.
</div>

<p>
Examples of these methods will appear shortly.
</p>

2710
<H3><a name="Tcl_nn37"></a>40.7.3 Typemap variables</H3>
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780


<p>
Within typemap code, a number of special variables prefaced with a <tt>$</tt> may appear.
A full list of variables can be found in the "<a href="Typemaps.html#Typemaps">Typemaps</a>" chapter.
This is a list of the most common variables:
</p>

<p>
<tt>$1</tt>
</p>

<div class="indent">
A C local variable corresponding to the actual type specified in the
<tt>%typemap</tt> directive.  For input values, this is a C local variable
that's supposed to hold an argument value.  For output values, this is
the raw result that's supposed to be returned to Tcl.
</div>

<p>
<tt>$input</tt>
</p>

<div class="indent">
 A <tt>Tcl_Obj *</tt> holding a raw Tcl object with an argument or variable value.
</div>

<p>
<tt>$result</tt>
</p>

<div class="indent">
A <tt>Tcl_Obj *</tt> that holds the result to be returned to Tcl.
</div>

<p>
<tt>$1_name</tt>
</p>

<div class="indent">
The parameter name that was matched. 
</div>

<p>
<tt>$1_type</tt>
</p>

<div class="indent">
The actual C datatype matched by the typemap.
</div>

<p>
<tt>$1_ltype</tt>
</p>

<div class="indent">
An assignable version of the datatype matched by the typemap (a type that can appear on the left-hand-side of
a C assignment operation).  This type is stripped of qualifiers and may be an altered version of <tt>$1_type</tt>.
All arguments and local variables in wrapper functions are declared using this type so that their values can be
properly assigned.
</div>

<p>
<tt>$symname</tt>
</p>

<div class="indent">
The Tcl name of the wrapper function being created.
</div>

2781
<H3><a name="Tcl_nn38"></a>40.7.4 Converting  a Tcl list to a char ** </H3>
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842


<p>
A common problem in many C programs is the processing of command line
arguments, which are usually passed in an array of NULL terminated
strings.  The following SWIG interface file allows a Tcl list to be
used as a <tt>char **</tt> object.
</p>

<div class="code"><pre>
%module argv

// This tells SWIG to treat char ** as a special case
%typemap(in) char ** {
     Tcl_Obj **listobjv;
     int       nitems;
     int       i;
     if (Tcl_ListObjGetElements(interp, $input, &amp;nitems, &amp;listobjv) == TCL_ERROR) {
        return TCL_ERROR;
     }
     $1 = (char **) malloc((nitems+1)*sizeof(char *));
     for (i = 0; i &lt; nitems; i++) {
        $1[i] = Tcl_GetStringFromObj(listobjv[i],0);
     }
     $1[i] = 0;
}

// This gives SWIG some cleanup code that will get called after the function call
%typemap(freearg) char ** {
     if ($1) {
        free($1);
     }
}

// Now a test functions
%inline %{
int print_args(char **argv) {
    int i = 0;
    while (argv[i]) {
         printf("argv[%d] = %s\n", i,argv[i]);
         i++;
    }
    return i;
}
%}
%include "tclsh.i"

</pre></div>

<p>
In Tcl:
</p>

<div class="code"><pre>
% print_args {John Guido Larry}
argv[0] = John
argv[1] = Guido
argv[2] = Larry
3
</pre></div>

2843
<H3><a name="Tcl_nn39"></a>40.7.5 Returning values in arguments</H3>
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884


<p>
The "argout" typemap can be used to return a value originating from a
function argument. For example :
</p>

<div class="code"><pre>
// A typemap defining how to return an argument by appending it to the result
%typemap(argout) double *outvalue {
     Tcl_Obj *o = Tcl_NewDoubleObj($1);
     Tcl_ListObjAppendElement(interp,$result,o);
}

// A typemap telling SWIG to ignore an argument for input
// However, we still need to pass a pointer to the C function
%typemap(in,numinputs=0) double *outvalue (double temp) {
     $1 = &amp;temp;
}

// Now a function returning two values
int mypow(double a, double b, double *outvalue) {
        if ((a &lt; 0) || (b &lt; 0)) return -1;
        *outvalue = pow(a,b);
        return 0;
};
</pre></div>

<p>
When wrapped, SWIG matches the <tt>argout</tt> typemap to the
"<tt>double *outvalue</tt>" argument. The numinputs=0 specification tells SWIG
to simply ignore this argument when generating wrapper code.  As a
result, a Tcl function using these typemaps will work like this :
</p>

<div class="code"><pre>
% mypow 2 3     # Returns two values, a status value and the result
0 8
%
</pre></div>

2885
<H3><a name="Tcl_nn40"></a>40.7.6 Useful functions</H3>
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960


<p>
The following tables provide some functions that may be useful in
writing Tcl typemaps. 
</p>

<p>
<b>Integers</b>
</p>

<div class="code">
<pre>
Tcl_Obj   *Tcl_NewIntObj(int Value);
void       Tcl_SetIntObj(Tcl_Obj *obj, int Value);
int        Tcl_GetIntFromObj(Tcl_Interp *, Tcl_Obj *obj, int *ip);
</pre>
</div>

<p>
<b>Floating Point</b>
</p>

<div class="code">
<pre>
Tcl_Obj  *Tcl_NewDoubleObj(double Value);
void      Tcl_SetDoubleObj(Tcl_Obj *obj, double value);
int       Tcl_GetDoubleFromObj(Tcl_Interp *, Tcl_Obj *o, double *dp);
</pre>
</div>

<p>
<b>Strings</b>
</p>

<div class="code">
<pre>
Tcl_Obj  *Tcl_NewStringObj(char *str, int len);
char     *Tcl_GetStringFromObj(Tcl_Obj *obj, int *len);
void      Tcl_AppendToObj(Tcl_Obj *obj, char *str, int len);
</pre>
</div>

<p>
<b>Lists</b>
</p>

<div class="code">
<pre>
Tcl_Obj  *Tcl_NewListObj(int objc, Tcl_Obj *objv);
int       Tcl_ListObjAppendList(Tcl_Interp *, Tcl_Obj *listPtr, Tcl_Obj *elemListPtr);
int       Tcl_ListObjAppendElement(Tcl_Interp *, Tcl_Obj *listPtr, Tcl_Obj *element);
int       Tcl_ListObjGetElements(Tcl_Interp *, Tcl_Obj *listPtr, int *objcPtr,
                                 Tcl_Obj ***objvPtr);
int       Tcl_ListObjLength(Tcl_Interp *, Tcl_Obj *listPtr, int *intPtr);
int       Tcl_ListObjIndex(Tcl_Interp *, Tcl_Obj *listPtr, int index,
                           Tcl_Obj_Obj **objptr);
int       Tcl_ListObjReplace(Tcl_Interp *, Tcl_Obj *listPtr, int first, int count,
                             int objc, Tcl_Obj *objv);
</pre>
</div>

<p>
<b>Objects</b>
</p>

<div class="code">
<pre>
Tcl_Obj *Tcl_DuplicateObj(Tcl_Obj *obj);
void     Tcl_IncrRefCount(Tcl_Obj *obj);
void     Tcl_DecrRefCount(Tcl_Obj *obj);
int      Tcl_IsShared(Tcl_Obj *obj);
</pre>
</div>

2961
<H3><a name="Tcl_nn41"></a>40.7.7 Standard  typemaps</H3>
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038


<p>
The following typemaps show how to convert a few common kinds of
objects between Tcl and C (and to give a better idea of how typemaps
work)
</p>


<p>
<b>Integer conversion</b>
</p>

<div class="code">
<pre>
%typemap(in) int, short, long {
   int temp;
   if (Tcl_GetIntFromObj(interp, $input, &amp;temp) == TCL_ERROR)
      return TCL_ERROR;
   $1 = ($1_ltype) temp;
}
</pre>
</div>

<br>

<div class="code">
<pre>
%typemap(out) int, short, long {
   Tcl_SetIntObj($result,(int) $1);
}
</pre>
</div>

<p>
<b>Floating point conversion</b>
</p>

<div class="code">
<pre>
%typemap(in) float, double {
   double temp;
   if (Tcl_GetDoubleFromObj(interp, $input, &amp;temp) == TCL_ERROR)
       return TCL_ERROR;
   $1 = ($1_ltype) temp;
}
</pre>
</div>

<br>

<div class="code">
<pre>
%typemap(out) float, double {
   Tcl_SetDoubleObj($result, $1);
}
</pre>
</div>

<p>
<b>String Conversion</b>
</p>

<div class="code">
<pre>
%typemap(in) char * {
   int len;
   $1 = Tcl_GetStringFromObj(interp, &amp;len);
   }
}
</pre>
</div>

<br>

<div class="code">
<pre>
3039 3040
%typemap(out,noblock=1,fragment="SWIG_FromCharPtr") char *, const char * {
  Tcl_SetObjResult(interp,SWIG_FromCharPtr((const char *)$1));
3041
}
3042

3043 3044 3045
</pre>
</div>

3046
<H3><a name="Tcl_nn42"></a>40.7.8 Pointer handling</H3>
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121


<p>
SWIG pointers are mapped into Tcl strings containing the
hexadecimal value and type.  The following functions can be used to
create and read pointer values.
</p>

<p>
<tt>
int SWIG_ConvertPtr(Tcl_Obj *obj, void **ptr, swig_type_info *ty, int flags)</tt>
</p>

<div class="indent">
Converts a Tcl object <tt>obj</tt> to a C pointer.  The result of the conversion is placed
into the pointer located at <tt>ptr</tt>.  <tt>ty</tt> is a SWIG type descriptor structure.
<tt>flags</tt> is used to handle error checking and other aspects of conversion.  It is currently
reserved for future expansion. Returns 0 on success and -1 on error.
</div>

<p>
<tt>
Tcl_Obj *SWIG_NewPointerObj(void *ptr, swig_type_info *ty, int flags)</tt>
</p>

<div class="indent">
Creates a new Tcl pointer object.  <tt>ptr</tt> is the pointer to convert, <tt>ty</tt> is the SWIG type descriptor structure that
describes the type, and <tt>own</tt> is a flag reserved for future expansion.
</div>

<p>
Both of these functions require the use of a special SWIG
type-descriptor structure.  This structure contains information about
the mangled name of the datatype, type-equivalence information, as
well as information about converting pointer values under C++
inheritance.   For a type of <tt>Foo *</tt>, the type descriptor structure
is usually accessed as follows:
</p>

<div class="indent">
<pre>
Foo *f;
if (SWIG_ConvertPtr($input, (void **) &amp;f, SWIGTYPE_p_Foo, 0) == -1) return NULL;

Tcl_Obj *;
obj = SWIG_NewPointerObj(f, SWIGTYPE_p_Foo, 0);
</pre>
</div>

<p>
In a typemap, the type descriptor should always be accessed using the special typemap
variable <tt>$1_descriptor</tt>.  For example:
</p>

<div class="indent">
<pre>
%typemap(in) Foo * {
   if ((SWIG_ConvertPtr($input,(void **) &amp;$1, $1_descriptor,0)) == -1) return NULL;
}
</pre>
</div>

<p>
If necessary, the descriptor for any type can be obtained using the <tt>$descriptor()</tt> macro in a typemap.
For example:
</p>

<div class="indent">
<pre>
%typemap(in) Foo * {
   if ((SWIG_ConvertPtr($input,(void **) &amp;$1, $descriptor(Foo *), 0)) == -1) return NULL;
}
</pre>
</div>

3122
<H2><a name="Tcl_nn43"></a>40.8 Turning a SWIG module into a Tcl Package.</H2>
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193


<p>
Tcl 7.4 introduced the idea of an extension package.  By default, SWIG
generates all of the code necessary to create a package. To set the package version,
simply use the <tt>-pkgversion</tt> option. For example:
</p>

<div class="code">
<pre>
% swig -tcl -pkgversion 2.3 example.i
</pre>
</div>

<p>
After building the SWIG generated module, you need to execute
the "<tt>pkg_mkIndex</tt>" command inside tclsh.  For example :
</p>

<div class="code"><pre>
unix &gt; tclsh
% pkg_mkIndex . example.so
% exit
</pre></div>

<p>
This creates a file "<tt>pkgIndex.tcl</tt>" with information about the
package.  To use your package, you now need to move it to its own
subdirectory which has the same name as the package. For example :
</p>

<div class="code"><pre>
./example/
	   pkgIndex.tcl           # The file created by pkg_mkIndex
	   example.so             # The SWIG generated module
</pre></div>

<p>
Finally, assuming that you're not entirely confused at this point,
make sure that the example subdirectory is visible from the
directories contained in either the <tt>tcl_library</tt> or
<tt>auto_path</tt> variables.  At this point you're ready to use the
package as follows :
</p>

<div class="code"><pre>
unix &gt; tclsh
% package require example
% fact 4
24
%
</pre></div>

<p>
If  you're working with an example in the current directory and this doesn't work, do this instead :
</p>

<div class="code"><pre>
unix &gt; tclsh
% lappend auto_path .
% package require example
% fact 4
24
</pre></div>

<p>
As a final note, most SWIG examples do not yet use the
<tt>package</tt> commands. For simple extensions it may be easier just
to use the <tt>load</tt> command instead.
</p>

3194
<H2><a name="Tcl_nn44"></a>40.9 Building new kinds of Tcl interfaces (in Tcl)</H2>
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292


<p>
One of the most interesting aspects of Tcl and SWIG is that you can
create entirely new kinds of Tcl interfaces in Tcl using the low-level
SWIG accessor functions.  For example, suppose you had a library of
helper functions to access arrays :
</p>

<div class="code"><pre>
/* File : array.i */
%module array

%inline %{
double *new_double(int size) {
        return (double *) malloc(size*sizeof(double));
}
void delete_double(double *a) {
        free(a);
}
double get_double(double *a, int index) {
        return a[index];
}
void set_double(double *a, int index, double val) {
        a[index] = val;
}
int *new_int(int size) {
        return (int *) malloc(size*sizeof(int));
}
void delete_int(int *a) {
        free(a);
}
int get_int(int *a, int index) {
        return a[index];
}
int set_int(int *a, int index, int val) {
        a[index] = val;
}
%}

</pre></div>

<p>
While these could be called directly, we could also write a Tcl script
like this :
</p>

<div class="code"><pre>
proc Array {type size} {
    set ptr [new_$type $size]
    set code {
        set method [lindex $args 0]
        set parms [concat $ptr [lrange $args 1 end]]
        switch $method {
            get {return [eval "get_$type $parms"]}
            set {return [eval "set_$type $parms"]}
            delete {eval "delete_$type $ptr; rename $ptr {}"}
        }
    }
    # Create a procedure
    uplevel "proc $ptr args {set ptr $ptr; set type $type;$code}"
    return $ptr
}
</pre></div>

<p>
Our script allows easy array access as follows :
</p>

<div class="code"><pre>
set a [Array double 100]                   ;# Create a double [100]
for {set i 0} {$i &lt; 100} {incr i 1} {      ;# Clear the array
	$a set $i 0.0
}
$a set 3 3.1455                            ;# Set an individual element
set b [$a get 10]                          ;# Retrieve an element

set ia [Array int 50]                      ;# Create an int[50]
for {set i 0} {$i &lt; 50} {incr i 1} {       ;# Clear it
	$ia set $i 0
}
$ia set 3 7                                ;# Set an individual element
set ib [$ia get 10]                        ;# Get an individual element

$a delete                                  ;# Destroy a
$ia delete                                 ;# Destroy ia
</pre></div>

<p>
The cool thing about this approach is that it makes a common interface
for two different types of arrays.  In fact, if we were to add more C
datatypes to our wrapper file, the Tcl code would work with those as
well--without modification.  If an unsupported datatype was requested,
the Tcl code would simply return with an error so there is very little
danger of blowing something up (although it is easily accomplished
with an out of bounds array access).
</p>

3293
<H3><a name="Tcl_nn45"></a>40.9.1 Proxy classes</H3>
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413


<p>
A similar approach can be applied to proxy classes (also known as
shadow classes).  The following
example is provided by Erik Bierwagen and Paul Saxe.  To use it, run
SWIG with the <tt>-noobject</tt> option (which disables the builtin
object oriented interface).  When running Tcl, simply source this
file.  Now, objects can be used in a more or less natural fashion.
</p>

<div class="code"><pre>
# swig_c++.tcl
# Provides a simple object oriented interface using
# SWIG's low level interface.
#

proc new {objectType handle_r args} {
    # Creates a new SWIG object of the given type,
    # returning a handle in the variable "handle_r".
    #
    # Also creates a procedure for the object and a trace on
    # the handle variable that deletes the object when the
    # handle variable is overwritten or unset
    upvar $handle_r handle
    #
    # Create the new object
    #
    eval set handle \[new_$objectType $args\]
    #
    # Set up the object procedure
    #
    proc $handle {cmd args} "eval ${objectType}_\$cmd $handle \$args"
    #
    # And the trace ...
    #
    uplevel trace variable $handle_r uw "{deleteObject $objectType $handle}"
    #
    # Return the handle so that 'new' can be used as an argument to a procedure
    #
    return $handle
}

proc deleteObject {objectType handle name element op} {
    #
    # Check that the object handle has a reasonable form
    #
    if {![regexp {_[0-9a-f]*_(.+)_p} $handle]} {
        error "deleteObject: not a valid object handle: $handle"
    }
    #
    # Remove the object procedure
    #
    catch {rename $handle {}}
    #
    # Delete the object
    #
    delete_$objectType $handle
}

proc delete {handle_r} {
    #
    # A synonym for unset that is more familiar to C++ programmers
    #
    uplevel unset $handle_r
}
</pre></div>

<p>
To use this file, we simply source it and execute commands such as
"new" and "delete" to manipulate objects.  For example :
</p>

<div class="code"><pre>
// list.i
%module List
%{
#include "list.h"
%}

// Very simple C++ example

class List {
public:
  List();  // Create a new list
  ~List(); // Destroy a list
  int  search(char *value);
  void insert(char *);  // Insert a new item into the list
  void remove(char *);  // Remove item from list
  char *get(int n);     // Get the nth item in the list
  int  length;          // The current length of the list
static void print(List *l);  // Print out the contents of the list
};
</pre></div>

<p>
Now a Tcl script using the interface...
</p>

<div class="code"><pre>
load ./list.so list       ; # Load the module
source swig_c++.tcl       ; # Source the object file

new List l
$l insert Dave
$l insert John
$l insert Guido
$l remove Dave
puts $l length_get

delete l
</pre></div>

<p>
The cool thing about this example is that it works with any C++ object
wrapped by SWIG and requires no special compilation.  Proof that a
short, but clever Tcl script can be combined with SWIG to do many
interesting things.
</p>

3414
<H2><a name="Tcl_nn46"></a>40.10 Tcl/Tk Stubs</H2>
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437


<p>
For background information about the Tcl Stubs feature, see
<a href="http://www.tcl.tk/doc/howto/stubs.html">http://www.tcl.tk/doc/howto/stubs.html</a>.
</p>

<p>
As of SWIG 1.3.10, the generated C/C++ wrapper will use the Tcl Stubs
feature if compiled with <tt>-DUSE_TCL_STUBS</tt>.
</p>

<p>
As of SWIG 1.3.40, the generated C/C++ wrapper will use the Tk Stubs
feature if compiled with <tt>-DUSE_TK_STUBS</tt>.  Also, you can override
the minimum version to support which is passed to <tt>Tcl_InitStubs()</tt>
and <tt>Tk_InitStubs()</tt> with <tt>-DSWIG_TCL_STUBS_VERSION="8.3"</tt>
or the version being compiled with using
<tt>-DSWIG_TCL_STUBS_VERSION=TCL_VERSION</tt>.
</p>

</body>
</html>