Initial revision

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@444 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-05-17 02:21:07 +00:00
commit 45b51a07f8
176 changed files with 95839 additions and 0 deletions

View file

@ -0,0 +1,51 @@
<html>
<head>
<title>About this manual</title>
</head>
<body bgcolor="#ffffff">
<h1>About This Manual</h1><p>
The HTML version of the SWIG Users Manual is a direct translation
of the printed version which is produced using Framemaker 5. The
conversion process was roughly as follows :
<ul>
<li> Raw HTML was produced using Framemaker 5 and Quadralay WebWorks Lite.
<li> Tables and figures were converted into GIF images.
<li> All of this output was fed into a magic Python script that cleaned
up the HTML source and merged the GIF figures into the text.
<li> A table of contents and alphabetized topic index were produced from
HTML heading tags by the same script.
</ul>
While the conversion process is mostly complete, there are a few things
to keep in mind :
<ul>
<li> Some sections of preformatted text may have weird formatting
problems.
<li> Framemaker tables were converted into GIF images instead
of HTML tables--this is a little weird, but the easiest
approach for now.
<li> There may be a few minor formatting problems throughout
due to minor "glitches" that slipped through the conversion process
(although I've tried to correct as many as these as possible).
<li> The printed version of the SWIG manual is more than
300 pages long--resulting in about 670 Kbytes of HTML. The HTML version
is broken up into chapters. Each chapter is fairly well-contained, but
some may contain as many as 50 pages of printed text.
</ul>
Please report any problems with the documentation to beazley@cs.utah.edu.
<hr>
<address>
Last Modified : August 3, 1997</address>
</body>
</html>

View file

@ -0,0 +1,305 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Advanced Topics</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1>11 Advanced Topics</h1><p><ul>
<li> <a href="#n1">Creating multi-module packages</a>
<li> <a href="#n2">Dynamic Loading of C++ modules</a>
<li> <a href="#n3">Inside the SWIG type-checker</a>
</ul>
<a name="n1"></a><h2> Creating multi-module packages</h2>
SWIG can be used to create packages consisting of many different modules. However, there are some technical aspects of doing this and techniques for managing the problem.<p>
<a name="n4"></a><h3> Runtime support (and potential problems)</h3>
All SWIG generated modules rely upon a small collection of functions that are used during run-time. These functions are primarily used for pointer type-checking, exception handling, and so on. When you run SWIG, these functions are included in the wrapper file (and declared as static). If you create a system consisting of many modules, each one will have an identical copy of these runtime libraries :<p>
<center><img src="ch11.1.png"></center><p>
<p>
This duplication of runtime libraries is usually harmless since there are no namespace conflicts and memory overhead is minimal. However, there is serious problem related to the fact that modules do not share type-information. This is particularly a problem when working with C++ (as described next).<p>
<a name="n5"></a><h3> Why doesn't C++ inheritance work between modules?</h3>
Consider for a moment the following two interface files :<p>
<p>
<blockquote><pre>// File : a.i
%module a
// Here is a base class
class a {
public:
a();
~a();
void foo(double);
};
// File : b.i
%module b
// Here is a derived class
%extern a.i // Gets definition of base class
class b : public a {
public:
bar();
};
</pre></blockquote>
When compiled into two separate modules, the code does not work properly. In fact, you get a type error such as the following :<p>
<p>
<blockquote><pre>[beazley@guinness shadow]$ python
Python 1.4 (Jan 16 1997) [GCC 2.7.2]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
&gt;&gt;&gt; from a import *
&gt;&gt;&gt; from b import *
&gt;&gt;&gt; # Create a new "b"
&gt;&gt;&gt; b = new_b()
&gt;&gt;&gt; # Call a function in the base class
...
&gt;&gt;&gt; a_foo(b,3)
Traceback (innermost last):
File "&lt;stdin&gt;", line 1, in ?
TypeError: Type error in argument 1 of a_foo. Expected _a_p.
&gt;&gt;&gt;
</pre></blockquote>
<p>
However, from our class definitions we know that "b" is an "a" by inheritance and there should be no type-error. This problem is directly due to the lack of type-sharing between modules. If we look closely at the module modules created here, they look like this :<p>
<center><img src="ch11.2.png"></center><p>
<p>
The type information listed shows the acceptable values for various C datatypes. In the "a" module, we see that "a" can only accept instances of itself. In the "b" module, we see that "a" can accept both "a" and "b" instances--which is correct given that a "b" is an "a" by inheritance.<p>
<p>
Unfortunately, this problem is inherent in the method by which SWIG makes modules. When we made the "a" module, we had no idea what derived classes might be used at a later time. However, it's impossible to produce the proper type information until after we know all of the derived classes. A nice problem to be sure, but one that can be fixed by making all modules share a single copy of the SWIG run-time library.<p>
<a name="n6"></a><h3> The SWIG runtime library</h3>
To reduce overhead and to fix type-handling problems, it is possible to share the SWIG run-time functions between multiple modules. This requires the use of the SWIG runtime library which is optionally built during SWIG installation. To use the runtime libraries, follow these steps :<p>
<p>
1. Build the SWIG run-time libraries. The <tt>SWIG1.1/Runtime</tt> directory contains a makefile for doing this. If successfully built, you will end up with 6 files that are usually installed in <tt>/usr/local/lib</tt>.<p>
<p>
<blockquote><pre>libswigtcl.a # Tcl library (static)
libswigtcl.so # Tcl library (shared)
libswigpl.a # Perl library (static)
libswigpl.so # Perl library (shared)
libswigpy.a # Python library (static)
libswigpy.so # Python library (shared)
</pre></blockquote>
<p>
Note that certain libraries may be missing due to missing packages or unsupported features (like dynamic loading) on your machine.<p>
<p>
2. Compile all SWIG modules using the <tt>-c</tt> option. For example :<p>
<p>
<blockquote><pre>% swig -c -python a.i
% swig -c -python b.i
</pre></blockquote>
The <tt>-c</tt> option tells SWIG to omit runtime support. It's now up to you to provide it separately--which we will do using our libraries.<p>
<p>
3. Build SWIG modules by linking against the appropriate runtime libraries.<p>
<p>
<blockquote><pre>% swig -c -python a.i
% swig -c -python b.i
% gcc -c a_wrap.c b_wrap.c -I/usr/local/include
% ld -shared a_wrap.o b_wrap.o -lswigpy -o a.so
</pre></blockquote>
or if building a new executable (static linking)<p>
<p>
<blockquote><pre>% swig -c -tcl -ltclsh.i a.i
% gcc a_wrap.c -I/usr/local/include -L/usr/local/lib -ltcl -lswigtcl -lm -o mytclsh
</pre></blockquote>
<p>
When completed you should now end up with a collection of modules like this :<p>
<center><img src="ch11.3.png"></center><p>
<p>
<p>
In this configuration, the runtime library manages all datatypes and other information between modules. This management process is dynamic in nature--when new modules are loaded, they contribute information to the run-time system. In the C++ world, one could incrementally load classes as needed. As this process occurs, type information is updated and base-classes learn about derived classes as needed.<p>
<a name="n7"></a><h3> A few dynamic loading gotchas</h3>
When working with dynamic loading, it is critical to check that only one copy of the run-time library is being loaded into the system. When working with <tt>.a</tt> library files, problems can sometimes occur so there are a few approaches to the problem.<p>
<p>
1. Rebuild the scripting language executable with the SWIG runtime library attached to it. This is actually, fairly easy to do using SWIG. For example :<p>
<p>
<blockquote><pre>%module mytclsh
%{
static void *__embedfunc(void *a) { return a};
%}
void *__embedfunc(void *);
%include tclsh.i
</pre></blockquote>
<p>
Now, run SWIG and compile as follows :<p>
<p>
<blockquote><pre>% swig -c -tcl mytclsh.i
% gcc mytclsh_wrap.c -I/usr/local/include -L/usr/local/lib -ltcl -lswigtcl -ldl -lm \
-o tclsh
</pre></blockquote>
This produces a new executable "<tt>tclsh</tt>" that contains a copy of the SWIG runtime library. The weird <tt>__embedfunc()</tt> function is needed to force the functions in the runtime library to be included in the final executable. <p>
<p>
To make new dynamically loadable SWIG modules, simply compile as follows :<p>
<p>
<blockquote><pre>% swig -c -tcl example.i
% gcc -c example_wrap.c -I/usr/local/include
% ld -shared example_wrap.o -o example.so
</pre></blockquote>
Linking against the <tt>swigtcl</tt> library is no longer necessary as all of the functions are now included in the <tt>tclsh</tt> executable and will be resolved when your module is loaded.<p>
<p>
2. Using shared library versions of the runtime library<p>
<p>
If supported on your machine, the runtime libraries will be built as shared libraries (indicated by a <tt>.so</tt>, <tt>.sl</tt>, or .<tt>dll</tt> suffix). To compile using the runtime libraries, you link process should look something like this :<p>
<blockquote><pre>
% ld -shared swigtcl_wrap.o -o libswigtcl.so # Irix
% gcc -shared swigtcl_wrap.o -o libswigtcl.so # Linux
% ld -G swigtcl_wrap.o -o libswigtcl.so # Solaris
</pre></blockquote>
In order for the <tt>libswigtcl.so</tt> library to work, it needs to be placed in a location where the dynamic loader can find it. Typically this is a system library directory (ie. <tt>/usr/local/lib</tt> or <tt>/usr/lib</tt>). <p>
<p>
When running with the shared libary version, you may get error messages such as the following <p>
<p>
<blockquote><pre>Unable to locate libswigtcl.so
</pre></blockquote>
This indicates that the loader was unable to find the shared libary at run-time. To find shared libaries, the loader looks through a collection of predetermined paths. If the <tt>libswigtcl.so</tt> file is not in any of these directories, it results in an error. On most machines, you can change the loader search path by changing the Unix environment variable <tt>LD_LIBRARY_PATH</tt>. For example :<p>
<p>
<blockquote><pre>% setenv LD_LIBRARY_PATH .:/home/beazley/packages/lib
</pre></blockquote>
A somewhat better approach is to link your module with the proper path encoded. This is typically done using the `-rpath' or `-R' option to your linker (see the man page). For example :<p>
<p>
<blockquote><pre>% ld -shared example_wrap.o example.o -rpath /home/beazley/packages/lib \
-L/home/beazley/packages/lib -lswigtcl.so -o example.so
</pre></blockquote>
The <tt>-rpath</tt> option encodes the location of shared libraries into your modules and gets around having to set the <tt>LD_LIBRARY_PATH</tt> variable. <p>
<p>
If all else fails, pull up the man pages for your linker and start playing around. <p>
<a name="n2"></a><h2> Dynamic Loading of C++ modules</h2>
Dynamic loading of C++ modules presents a special problem for many systems. This is because C++ modules often need additional supporting code for proper initialization and operation. Static constructors are also a bit of a problem.<p>
<p>
While the process of building C++ modules is, by no means, and exact science, here are a few rules of thumb to follow :<p>
<p>
<ul>
<li>Don't use static constructors if at all possible (not always avoidable).
<li>Try linking your module with the C++ compiler using a command like `c++ -shared'. This often solves alot of problems.
<li>Sometimes it is necessary to link against special libraries. For example, modules compiled with g++ often need to be linked against the <tt>libgcc.a</tt>, <tt>libg++.a</tt>, and <tt>libstdc++.a</tt> libraries.
<li>Read the compiler and linker man pages over and over until you have them memorized (this may not help in some cases however).
<li>Search articles on Usenet, particularly in <tt>comp.lang.tcl</tt>, <tt>comp.lang.perl</tt>, and <tt>comp.lang.python</tt>. Building C++ modules is a common problem.
</ul>
<p>
The SWIG distribution contains some additional documentation about C++ modules in the Doc directory as well.<p>
<a name="n3"></a><h2> Inside the SWIG type-checker</h2>
The SWIG runtime type-checker plays a critical role in the correct operation of SWIG modules. It not only checks the validity of pointer types, but also manages C++ inheritance, and performs proper type-casting of pointers when necessary. This section provides some insight into what it does, how it works, and why it is the way it is.<p>
<a name="n8"></a><h3> Type equivalence</h3>
SWIG uses a name-based approach to managing pointer datatypes. For example, if you are using a pointer like "<tt>double *</tt>", the type-checker will look for a particular string representation of that datatype such as "<tt>_double_p</tt>". If no match is found, a type-error is reported.<p>
<p>
However, the matching process is complicated by the fact that datatypes may use a variety of different names. For example, the following declarations<p>
<p>
<blockquote><pre>typedef double Real;
typedef Real * RealPtr;
typedef double Float;
</pre></blockquote>
define two sets of equivalent types :<p>
<p>
<blockquote><pre>{double, Real, Float}
{RealPtr, Real *}
</pre></blockquote>
All of the types in each set are freely interchangable and the type-checker knows about the relationships by managing a table of equivalences such as the following :<p>
<blockquote><pre>
double =&gt; { Real, Float }
Real =&gt; { double, Float }
Float =&gt; { double, Real }
RealPtr =&gt; { Real * }
Real * =&gt; { RealPtr }
</pre></blockquote>
<p>
When you declare a function such as the following :<p>
<p>
<blockquote><pre>void foo(Real *a);
</pre></blockquote>
SWIG first checks to see if the argument passed is a "<tt>Real *</tt>". If not, it checks to see if it is any of the other equivalent types (<tt>double *</tt>, <tt>RealPtr</tt>, <tt>Float *</tt>). If so, the value is accepted and no error occurs.<p>
<p>
Derived versions of the various datatypes are also legal. For example, if you had a function like this,<p>
<p>
<blockquote><pre>void bar(Float ***a);
</pre></blockquote>
The type-checker will accept pointers of type <tt>double ***</tt> and <tt>Real ***.</tt> However, the type-checker does not always capture the full-range of possibilities. For example, a datatype of `<tt>RealPtr **</tt>' is equivalent to a `<tt>Float ***</tt>' but would be flagged as a type error. If you encounter this kind of problem, you can manually force SWIG to make an equivalence as follows:<p>
<p>
<blockquote><pre>// Tell the type checker that `Float_ppp' and `RealPtr_pp' are equivalent.
%init %{
SWIG_RegisterMapping("Float_ppp","RealPtr_pp",0);
%}
</pre></blockquote>
Doing this should hardly ever be necessary (I have never encountered a case where this was necessary), but if all else fails, you can force the run-time type checker into doing what you want.<p>
<p>
Type-equivalence of C++ classes is handled in a similar manner, but is encoded in a manner to support inheritance. For example, consider the following classes hierarchy :<p>
<p>
<blockquote><pre>class A { };
class B : public A { };
class C : public B { };
class D {};
class E : public C, public D {};
</pre></blockquote>
The type-checker encodes this into the following sets :<p>
<p>
<blockquote><pre>A =&gt; { B, C, E } "B isa A, C isa A, E isa A"
B =&gt; { C, E } "C isa B, E isa B"
C =&gt; { E } "E isa C"
D =&gt; { E } "E isa D"
E =&gt; { }
</pre></blockquote>
The encoding reflects the class hierarchy. For example, any object of type "A" will also accept objects of type B,C, and E because these are all derived from A. However, it is not legal to go the other way. For example, a function operating on a object from class E will not accept an object from class A.<p>
<a name="n9"></a><h3> Type casting</h3>
When working with C++ classes, SWIG needs to perform proper typecasting between derived and base classes. This is particularly important when working with multiple inheritance. To do this, conversion functions are created such as the following :<p>
<p>
<blockquote><pre>void *EtoA(void *ptr) {
E *in = (E *) ptr;
A *out = (A *) in; // Cast using C++
return (void *) out;
}
</pre></blockquote>
All pointers are internally represented as void *, but conversion functions are always invoked when pointer values are converted between base and derived classes in a C++ class hierarchy.<p>
<a name="n10"></a><h3> Why a name based approach?</h3>
SWIG uses a name-based approach to type-checking for a number of reasons :<p>
<p>
<ul>
<li>One of SWIG's main uses is code development and debugging. In this environment, the type name of an object turns out to be a useful piece of information in tracking down problems.
<li>In languages like Perl, the name of a datatype is used to determine things like packages and classes. By using datatype names we get a natural mapping between C and Perl.
<li>I believe using the original names of datatypes is more intuitive than munging them into something completely different.
</ul>
<p>
An alternative to a name based scheme would be to generate type-signatures based on the structure of a datatype. Such a scheme would result in perfect type-checking, but I think it would also result in a very confusing scripting language module. For this reason, I see SWIG sticking with the name-based approach--at least for the foreseeable future. <p>
<a name="n11"></a><h3> Performance of the type-checker</h3>
The type-checker performs the following steps when matching a datatype :<p>
<p>
<dl>
<dt>1. Check a pointer against the type supplied in the original C declaration. If there is a perfect match, we're done.
<dt>2. Check the supplied pointer against a cache of recently used datatypes.
<dt>3. Search for a match against the full list of equivalent datatypes.
<dt>4. If not found, report an error.
</dl>
<p>
Most well-structured C codes will find an exact match on the first attempt, providing the best possible performance. For C++ codes, it is quite common to be passing various objects of a common base-class around between functions. When base-class functions are invoked, it almost always results in a miscompare (because the type-checker is looking for the base-type). In this case, we drop down to a small cache of recently used datatypes. If we've used a pointer of the same type recently, it will be in the cache and we can match against it. For tight loops, this results in about 10-15% overhead over finding a match on the first try. Finally, as a last resort, we need to search the internal pointer tables for a match. This involves a combination of hash table lookup and linear search. If a match is found, it is placed into the cache and the result returned. If not, we finally report a type-mismatch.<p>
<p>
As a rule of thumb, C++ programs require somewhat more processing than C programs, but this seems to be avoidable. Also, keep in mind that performance penalties in the type-checker don't necessarily translate into big penalties in the overall application. Performance is most greatly affected by the efficiency of the target scripting language and the types of operations your C code is performing.<p>
<p>
<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:47:13 1997</address>
</body>
</html>

View file

@ -0,0 +1,184 @@
<html>
<head>
<title> SWIG 1.1 Users Manual - Table of Contents </title>
</head>
<body bgcolor="#ffffff">
<h1> SWIG 1.1 Users Manual </h1>
<ul>
<li> <a href="Copyright.html">Copyright</a>
<li> <a href="About.html">About this manual</a>
<li> <a href="Index.html">Alphabetical Topic Index</a>
</ul>
<h2><a href="Preface.html">Preface</a></h2><p><ul>
<li> <a href="Preface.html#n1">Introduction</a>
<li> <a href="Preface.html#n2">SWIG resources</a>
<li> <a href="Preface.html#n3">About this manual</a>
<li> <a href="Preface.html#n4">Credits</a>
<li> <a href="Preface.html#n5">What's new?</a>
<li> <a href="Preface.html#n6">Bug reports</a>
<li> <a href="Preface.html#n7">SWIG is free</a>
</ul>
<h2><a href="Introduction.html">1 Introduction</a></h2><p><ul>
<li> <a href="Introduction.html#n1">What is SWIG?</a>
<li> <a href="Introduction.html#n2">Life before SWIG</a>
<li> <a href="Introduction.html#n3">Life after SWIG</a>
<li> <a href="Introduction.html#n4">The SWIG package</a>
<li> <a href="Introduction.html#n5">A SWIG example</a>
<li> <a href="Introduction.html#n6">C syntax, but not a C compiler</a>
<li> <a href="Introduction.html#n7">Non-intrusive interface building</a>
<li> <a href="Introduction.html#n8">Hands off code generation</a>
<li> <a href="Introduction.html#n9">Event driven C programming</a>
<li> <a href="Introduction.html#n10">Automatic documentation generation</a>
<li> <a href="Introduction.html#n11">Summary</a>
<li> <a href="Introduction.html#n12">SWIG for Windows and Macintosh</a>
</ul>
<h2><a href="Scripting.html">2 Scripting Languages</a></h2><p><ul>
<li> <a href="Scripting.html#n1">The two language view of the world</a>
<li> <a href="Scripting.html#n2">How does a scripting language talk to C?</a>
<li> <a href="Scripting.html#n3">Building scripting language extensions</a>
<li> <a href="Scripting.html#n4">Shared libraries and dynamic loading</a>
</ul>
<h2><a href="SWIG.html">3 SWIG Basics</a></h2><p><ul>
<li> <a href="SWIG.html#n1">Running SWIG</a>
<li> <a href="SWIG.html#n2">Simple C functions, variables, and constants</a>
<li> <a href="SWIG.html#n3">Pointers and complex objects</a>
<li> <a href="SWIG.html#n4">Getting down to business</a>
<li> <a href="SWIG.html#n5">Structures, unions, and object oriented C programming</a>
<li> <a href="SWIG.html#n6">C++ support</a>
<li> <a href="SWIG.html#n7">Objective-C</a>
<li> <a href="SWIG.html#n8">Conditional compilation</a>
<li> <a href="SWIG.html#n9">Code Insertion</a>
<li> <a href="SWIG.html#n10">A general interface building strategy</a>
</ul>
<h2><a href="Library.html">4 Multiple files and the SWIG library</a></h2><p><ul>
<li> <a href="Library.html#n1">The %include directive</a>
<li> <a href="Library.html#n2">The %extern directive</a>
<li> <a href="Library.html#n3">The %import directive</a>
<li> <a href="Library.html#n4">Including files on the command line</a>
<li> <a href="Library.html#n5">The SWIG library</a>
<li> <a href="Library.html#n6">Library example</a>
<li> <a href="Library.html#n7">Creating Library Files</a>
<li> <a href="Library.html#n8">Working with library files</a>
<li> <a href="Library.html#n9">Static initialization of multiple modules</a>
<li> <a href="Library.html#n10">More about the SWIG library</a>
</ul>
<h2><a href="Documentation.html">5 Documentation System</a></h2><p><ul>
<li> <a href="Documentation.html#n1">Introduction</a>
<li> <a href="Documentation.html#n2">How it works</a>
<li> <a href="Documentation.html#n3">Choosing a documentation format</a>
<li> <a href="Documentation.html#n4">Function usage and argument names</a>
<li> <a href="Documentation.html#n5">Titles, sections, and subsections</a>
<li> <a href="Documentation.html#n6">Formatting</a>
<li> <a href="Documentation.html#n7">Adding Additional Text</a>
<li> <a href="Documentation.html#n8">Disabling all documentation</a>
<li> <a href="Documentation.html#n9">An Example</a>
<li> <a href="Documentation.html#n10">ASCII Documentation</a>
<li> <a href="Documentation.html#n11">HTML Documentation</a>
<li> <a href="Documentation.html#n12">LaTeX Documentation</a>
<li> <a href="Documentation.html#n13">C++ Support</a>
<li> <a href="Documentation.html#n14">The Final Word?</a>
</ul>
<h2><a href="Typemaps.html">6 Pointers, Constraints, and Typemaps</a></h2><p><ul>
<li> <a href="Typemaps.html#n1">Introduction</a>
<li> <a href="Typemaps.html#n2">The SWIG Pointer Library</a>
<li> <a href="Typemaps.html#n3">Introduction to typemaps</a>
<li> <a href="Typemaps.html#n4">Managing input and output parameters</a>
<li> <a href="Typemaps.html#n5">Applying constraints to input values</a>
<li> <a href="Typemaps.html#n6">Writing new typemaps</a>
<li> <a href="Typemaps.html#n7">Common typemap methods</a>
<li> <a href="Typemaps.html#n8">Writing typemap code</a>
<li> <a href="Typemaps.html#n9">Typemaps for handling arrays</a>
<li> <a href="Typemaps.html#n10">Typemaps and the SWIG Library</a>
<li> <a href="Typemaps.html#n11">Implementing constraints with typemaps</a>
<li> <a href="Typemaps.html#n12">Typemap examples</a>
<li> <a href="Typemaps.html#n13">How to break everything with a typemap</a>
<li> <a href="Typemaps.html#n14">Typemaps and the future</a>
</ul>
<h2><a href="Exceptions.html">7 Exception Handling</a></h2><p><ul>
<li> <a href="Exceptions.html#n1">The %except directive</a>
<li> <a href="Exceptions.html#n2">Handling exceptions in C code</a>
<li> <a href="Exceptions.html#n3">Exception handling with longjmp()</a>
<li> <a href="Exceptions.html#n4">Handling C++ exceptions</a>
<li> <a href="Exceptions.html#n5">Defining different exception handlers</a>
<li> <a href="Exceptions.html#n6">Using The SWIG exception library</a>
<li> <a href="Exceptions.html#n7">Debugging and other interesting uses for %except</a>
<li> <a href="Exceptions.html#n8">More Examples</a>
</ul>
<h2><a href="Perl5.html">8 SWIG and Perl5</a></h2><p><ul>
<li> <a href="Perl5.html#n1">Preliminaries</a>
<li> <a href="Perl5.html#n2">Building Perl Extensions under Windows 95/NT</a>
<li> <a href="Perl5.html#n3">Modules, packages, and classes</a>
<li> <a href="Perl5.html#n4">Basic Perl interface</a>
<li> <a href="Perl5.html#n5">A simple Perl example</a>
<li> <a href="Perl5.html#n6">Accessing arrays and other strange objects</a>
<li> <a href="Perl5.html#n7">Implementing methods in Perl</a>
<li> <a href="Perl5.html#n8">Shadow classes</a>
<li> <a href="Perl5.html#n9">Getting serious</a>
<li> <a href="Perl5.html#n10">Wrapping C libraries and other packages</a>
<li> <a href="Perl5.html#n11">Building a Perl5 interface to MATLAB</a>
<li> <a href="Perl5.html#n12">Handling output values (the easy way)</a>
<li> <a href="Perl5.html#n13">Exception handling</a>
<li> <a href="Perl5.html#n14">Remapping datatypes with typemaps</a>
<li> <a href="Perl5.html#n15">The gory details on shadow classes</a>
<li> <a href="Perl5.html#n16">Where to go from here?</a>
</ul>
<h2><a href="Python.html">9 SWIG and Python</a></h2><p><ul>
<li> <a href="Python.html#n1">Preliminaries</a>
<li> <a href="Python.html#n2">Building Python Extensions under Windows 95/NT</a>
<li> <a href="Python.html#n3">The low-level Python/C interface</a>
<li> <a href="Python.html#n4">Python shadow classes</a>
<li> <a href="Python.html#n5">About the Examples</a>
<li> <a href="Python.html#n6">Solving a simple heat-equation</a>
<li> <a href="Python.html#n7">Wrapping a C library</a>
<li> <a href="Python.html#n8">Putting it all together</a>
<li> <a href="Python.html#n9">Exception handling</a>
<li> <a href="Python.html#n10">Remapping C datatypes with typemaps</a>
<li> <a href="Python.html#n11">Implementing C callback functions in Python</a>
<li> <a href="Python.html#n12">Other odds and ends</a>
<li> <a href="Python.html#n13">The gory details of shadow classes</a>
</ul>
<h2><a href="Tcl.html">10 SWIG and Tcl</a></h2><p><ul>
<li> <a href="Tcl.html#n1">Preliminaries</a>
<li> <a href="Tcl.html#n2">Building Tcl/Tk Extensions under Windows 95/NT</a>
<li> <a href="Tcl.html#n3">Basic Tcl Interface</a>
<li> <a href="Tcl.html#n4">The object oriented interface</a>
<li> <a href="Tcl.html#n5">About the examples</a>
<li> <a href="Tcl.html#n6">Binary trees in Tcl</a>
<li> <a href="Tcl.html#n7">Building C/C++ data structures with Tk</a>
<li> <a href="Tcl.html#n8">Accessing arrays</a>
<li> <a href="Tcl.html#n9">Building a simple OpenGL module</a>
<li> <a href="Tcl.html#n10">Exception handling</a>
<li> <a href="Tcl.html#n11">Typemaps</a>
<li> <a href="Tcl.html#n12">Configuration management with SWIG</a>
<li> <a href="Tcl.html#n13">Building new kinds of Tcl interfaces (in Tcl)</a>
<li> <a href="Tcl.html#n14">Extending the Tcl Netscape Plugin</a>
<li> <a href="Tcl.html#n15">Tcl8.0 features</a>
</ul>
<h2><a href="Advanced.html">11 Advanced Topics</a></h2><p><ul>
<li> <a href="Advanced.html#n1">Creating multi-module packages</a>
<li> <a href="Advanced.html#n2">Dynamic Loading of C++ modules</a>
<li> <a href="Advanced.html#n3">Inside the SWIG type-checker</a>
</ul>
<h2><a href="Extending.html">12 Extending SWIG</a></h2><p><ul>
<li> <a href="Extending.html#n1">Introduction</a>
<li> <a href="Extending.html#n2">Compiling a SWIG extension</a>
<li> <a href="Extending.html#n3">SWIG output</a>
<li> <a href="Extending.html#n4">The Language class (simple version)</a>
<li> <a href="Extending.html#n5">A tour of SWIG datatypes</a>
<li> <a href="Extending.html#n6">Typemaps (from C)</a>
<li> <a href="Extending.html#n7">File management</a>
<li> <a href="Extending.html#n8">Naming Services</a>
<li> <a href="Extending.html#n9">Code Generation Functions</a>
<li> <a href="Extending.html#n10">Writing a Real Language Module</a>
<li> <a href="Extending.html#n11">C++ Processing</a>
<li> <a href="Extending.html#n12">Documentation Processing</a>
<li> <a href="Extending.html#n13">The Future of SWIG</a>
</ul>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:47:18 1997 </address>
</body>
</html>

View file

@ -0,0 +1,70 @@
<HTML>
<HEAD>
<title> SWIG Documentation Copyright</title>
</head>
<body BGCOLOR="#FFFFFF">
<h2> SWIG Users Manual </h2>
<b>
Version 1.1<br>
June, 1997 <br>
</b>
<p>
Copyright(C) 1996, 1997<br>
All Rights Reserved<br>
<br>
David M. Beazley<br>
Department of Computer Science <br>
University of Utah <br>
Salt Lake City, Utah 84112 </br>
<tt>beazley@cs.utah.edu</tt>
<p>
This document may be freely distributed in whole or part provided this
copyright notice is retained. Commercial distribution of this document
is prohibited without the express written consent of the author.
<hr>
SWIG 1.1 is Copyright (C) 1995-1997 by the University of Utah and the
Univerity of California and distributed under the following license.
<p>
This software is copyrighted by the University of Utah and the Regents
of the University of California. The following terms apply to all
files associated with the software unless explicitly disclaimed
in individual files.
<p>
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that
(1) The above copyright notice and the following two paragraphs
appear in all copies of the source code and (2) redistributions
including binaries reproduces these notices in the supporting
documentation. Substantial modifications to this software may be
copyrighted by their authors and need not follow the licensing terms
described here, provided that the new terms are clearly indicated in
all files where they apply.
<p>
IN NO EVENT SHALL THE AUTHOR, THE UNIVERSITY OF CALIFORNIA, THE
UNIVERSITY OF UTAH OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
<p>
THE AUTHOR, THE UNIVERSITY OF CALIFORNIA, AND THE UNIVERSITY OF UTAH
SPECIFICALLY DISCLAIM ANY WARRANTIES,INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE,
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
<hr>
<address><a href="http://www.cs.utah.edu/~beazley"> beazley@cs.utah.edu </a> <br>
Last Modified, August 3, 1997</address>
</body>
</html>

View file

@ -0,0 +1,652 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Documentation System</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1>5 Documentation System</h1><p><ul>
<li> <a href="#n1">Introduction</a>
<li> <a href="#n2">How it works</a>
<li> <a href="#n3">Choosing a documentation format</a>
<li> <a href="#n4">Function usage and argument names</a>
<li> <a href="#n5">Titles, sections, and subsections</a>
<li> <a href="#n6">Formatting</a>
<li> <a href="#n7">Adding Additional Text</a>
<li> <a href="#n8">Disabling all documentation</a>
<li> <a href="#n9">An Example</a>
<li> <a href="#n10">ASCII Documentation</a>
<li> <a href="#n11">HTML Documentation</a>
<li> <a href="#n12">LaTeX Documentation</a>
<li> <a href="#n13">C++ Support</a>
<li> <a href="#n14">The Final Word?</a>
</ul>
<a name="n1"></a><h2> Introduction</h2>
While SWIG makes it easy to build interfaces, it is often difficult to keep track of all of the different functions, variables, constants, and other objects that have been wrapped. This especially becomes a problem when your interface starts to grow in size from a handful to several hundred functions. To address these concerns, SWIG can automatically generate documentation in a number of formats including ASCII, HTML, and LaTeX. The goal is that you could look at the documentation file to see what functions were wrapped and how they are used in the target scripting language.<p>
<p>
Usage documentation is generated for each declaration found in an interface file. This documentation is generated by the target language module so the Tcl module will follow Tcl syntax, the Perl module will use Perl syntax, and so on. In addition, C/C++ comments can be used to add descriptive text to each function. Comments can be processed in a number of different styles to suit personal preferences or to match the style used in a particular input file.<p>
<p>
Automatic documentation generation for C/C++ programs is a fairly formidable problem and SWIG was never intended to be a substitute for a full-blown documentation generator. However, I feel that is does a reasonable job of documenting scripting language interfaces. It seems to do just fine for many of SWIG's primary applications--rapid prototyping, debugging, and development.<p>
<a name="n2"></a><h2> How it works</h2>
For each declaration in an interface file, SWIG creates a "Documentation Entry." This entry contains three components; (1) a usage string, (2) a C information string, and (3) descriptive text. For example, suppose you have this declaration in an interface file :<p>
<p>
<blockquote><pre>int fact(int n);
/* This function computes a factorial */
</pre></blockquote>
The documentation entry produced by the SWIG ASCII module will look like this for Tcl:<p>
<p>
<blockquote><pre>fact n
[ returns int ]
This function computes a factorial
</pre></blockquote>
The first line shows how to call the function, the second line shows some additional information about the function (related to its C implementation), while the third line contains the comment text. The first two lines are automatically generated by SWIG and may be different for each language module. For example, the Perl5 module would generate the following output :<p>
<p>
<blockquote><pre>fact($n)
[ returns int ]
This function computes a factorial
</pre></blockquote>
<p>
Of course, this is only a simple example, more sophisticated things are possible.<p>
<a name="n3"></a><h2> Choosing a documentation format</h2>
The type of documentation is selected using the following command line options :<p>
<blockquote><pre>
-dascii Produce ASCII documentation
-dhtml Produce HTML documentation
-dlatex Produce LaTeX documentation
-dnone Produce no documentation
</pre></blockquote>
The various documentation modules are implemented in a manner similar to language modules so the exact choice may change in the future. With a little C++ hacking, it is also possible for you to add your own modules to SWIG. For example, with a bit of work you could turn all of the documentation into an online help command in your scripting language. <p>
<a name="n4"></a><h2> Function usage and argument names</h2>
The function usage string is produced to match the declaration given in the SWIG interface file. The names of arguments can be specified by using argument names. For example, the declarations<p>
<p>
<blockquote><pre>void insert_item(List *, char *);
char *lookup_item(char *name);
</pre></blockquote>
<p>
will produce the following documentation (for Python) :<p>
<p>
<blockquote><pre>insert_item(List *, char *)
[ returns void ]
lookup_item(name)
[ returns char * ]
</pre></blockquote>
When argument names are omitted, SWIG will use the C datatypes of the arguments in the documentation. If an argument name is specified, SWIG will use that in the documentation instead. Of course, it is up to each language module to create an appropriate usage string so your results may vary depending on how things have been implemented in each module.<p>
<a name="n5"></a><h2> Titles, sections, and subsections</h2>
The SWIG documentation system is hierarchical in nature and is organized into a collection of sections, subsections, subsubsections, and so on. The following SWIG directives can be used to organize an interface file :<p>
<p>
<ul>
<li><tt>%title "Title Text"</tt>. Set the documentation title (may only be used once)
<li><tt>%section "Section title"</tt>. Start a new section.
<li><tt>%subsection "Subsection title"</tt>. Create a new subsection.
<li><tt>%subsubsection "Subsubsection title"</tt>. Create a new subsubsection.
</ul>
<p>
The <tt>%title</tt> directive should be placed prior to the first declaration in an interface file and may only be used once (subsequent occurrences will simply be ignored). The section directives may be placed anywhere. However, <tt>%subsection</tt> can only be used after a <tt>%section</tt> directive and <tt>%subsubsection</tt> can only be used after a <tt>%subsection</tt> directive.<p>
<p>
With the organization directives, a SWIG interface file looks something like this :<p>
<p>
<blockquote><pre>%title "Example Interface File"
%module example
%{
#include "my_header.h"
%}
%section "Mathematical Functions"
... declarations ...
%section "Graphics"
%subsection "2D Plotting"
... Declarations ...
%subsection "3D Plotting"
%subsubsection "Viewing transformations"
... Declarations ...
%subsubsection "Lighting"
... Declarations ...
%subsubsection "Primitives"
... Declarations ...
%section "File I/O"
... Declarations ...
</pre></blockquote>
<a name="n6"></a><h2> Formatting</h2>
Documentation text can be sorted, chopped, sliced, and diced in a variety of ways. Formatting information is specified using a comma separated list of parameters after the <tt>%title</tt>, <tt>%section</tt>, <tt>%subsection</tt>, or <tt>%subsubsection</tt> directives. For example :<p>
<p>
<blockquote><pre>%title "My Documentation", sort, before, pre
</pre></blockquote>
This tells SWIG to sort all of the documentation, use comments that are before each declaration, and assume that text is preformatted. These formatting directives are applied to all children in the documentation tree--in this case, everything in an interface file.<p>
<p>
If formatting information is specified for a section like this<p>
<p>
<blockquote><pre>%subsection "3D Graphics", nosort, after
</pre></blockquote>
<p>
then the effect will only apply to that particular section (and all of its subsections). In this case, the formatting of the subsection would override any previous formatting, but these changes would only apply to this subsection. The next subsection could use its own formatting or that of its parent.<p>
<p>
Style parameters can also be specified using the <tt>%style</tt> and <tt>%localstyle</tt> parameters. The <tt>%style</tt> directive applies a new format to the current section and all of its parents. The <tt>%localstyle</tt> directive applies a new format to the current section. For example :<p>
<p>
<blockquote><pre>%style sort,before, skip=1 # Apply these formats everywhere
%localstyle sort # Apply this format to the current section
</pre></blockquote>
Use of these directives usually isn't required since it's easy enough to simply specify the information after each section.<p>
<a name="n15"></a><h3> Default Formatting</h3>
By default, SWIG will reformat comment text, produce documentation in the order encountered in an interface file (nosort), and annotate descriptions with a C information string. This behavior most closely matches that used in SWIG 1.0, although it is not an exact match due to differences in the old documentation system.<p>
<p>
When used in the default mode, comment text may contain documentation specific formatting markup. For example, you could embed LaTeX or HTML markup in comments to have precise control over the look of the final document.<p>
<a name="n16"></a><h3> Comment Formatting variables</h3>
The default formatting can be changed by changing one or more of the following formatting variables :<p>
<blockquote><pre>
after Use comments after a declaration (default)
before Use comments before a declaration
chop_top=nlines Comment chopping (preformatted)
chop_bottom=nlines Comment chopping (preformatted)
chop_left=nchar Comment chopping (preformatted)
chop_right=nchar Comment chopping (preformatted)
format Allow SWIG to reformat text (the default)
ignore Ignore comments
info Print C information text (default)
keep Keep comments (opposite of ignore)
noinfo Don't print C information text
nosort Don't sort documentation (default)
pre Assume text is preformatted
skip=nlines Number of blank lines between comment and declaration
sort Sort documentation
tabify Leave tabs intact
untabify Convert tabs to spaces (default)
</pre></blockquote>
<p>
More variables may be available depending on particular documentation modules. The use of these variables is described in the next few sections.<p>
<a name="n17"></a><h3> Sorting </h3>
Documentation can be sorted using the `<tt>sort</tt>' parameter. For example :<p>
<p>
<blockquote><pre>%title "My interface",sort
</pre></blockquote>
<p>
When used, all documentation entries, including sections will be alphabetically sorted. Sorting can be disabled in particular sections and subsection by specifying the `<tt>nosort</tt>' parameter in a section declaration. By default, SWIG does not sort documentation. As a general rule, it really only comes in handy if you have a really messy interface file.<p>
<p>
For backwards compatibility with earlier versions of SWIG, the following directives can be used to specify sorting. <p>
<p>
<blockquote><pre>%alpha Sort documentation alphabetically (obsolete)
%raw Keep documentation in order (obsolete)
</pre></blockquote>
These directives only apply globally and should near the beginning of file. Future support of these directives is not guaranteed and generally discouraged.<p>
<a name="n18"></a><h3> Comment placement and formatting</h3>
Comments may be placed before or after a declaration. This is specified using the `<tt>before</tt>' and `<tt>after</tt>' parameters. The space between a comment and a declaration can be set by changing the `<tt>skip</tt>' parameter. By default, <tt>skip=1</tt>, indicating that a comment and declaration must be on adjacent lines. Use of the <tt>skip</tt> parameter makes it possible for the documentation generator to ignore comments that are too far away and possibly unrelated to a declaration.<p>
<p>
By default, SWIG reformats the text found in a comment. However, in many cases, your file may have preformatted comments or comment blocks. To handle such comments correctly, you can use preformatted mode. This is specified using the `pre' parameter as follows :<p>
<p>
<blockquote><pre>%section "Preformatted Section",pre
%section "Reformatted Section", format
</pre></blockquote>
All declarations in this section will now be assumed to have preformatted comments. When using the preformat mode, a variety of other parameters are available as shown in the following diagram :<p>
<center><img src="ch5.1.png"></center>
.<p>
<p>
The chopping parameters can be used to strip out the text of block comments. For example, using <tt>chop_left=3</tt>, <tt>chop_top=1</tt>, <tt>chop_bottom=1</tt> on the above comment produces the following output :<p>
<blockquote><pre>
Plot2D_line x1 y1 x2 y2 color
[ returns void ]
void Plot2D_line(int x1, int y1, int x2, int y2, Pixel color)
Draws a line between the points (x1,y1) and (x2,y2) using the
the given color. The line is cropped to fit in the current
bounding box.
Uses the Bresenham line drawing algorithm.
</pre></blockquote>
<p>
The chopping parameters only apply if a comment is sufficiently large (i.e.. if the number of lines exceed <tt>chop_top</tt>+<tt>chop_bottom</tt>). Thus, in our example, a one line comment will be unaltered even though chopping has been set. By default, SWIG sets chop_left=3 and all others to zero. This setting removes the `<tt>/* </tt>` or `<tt>// </tt>` preceeding a comment.<p>
<a name="n19"></a><h3> Tabs and other annoyances</h3>
When using the preformatted mode, SWIG will automatically convert tabs to white space. This is done assuming that tabs are placed every 8 characters. The tabification mode can be selected using the `tabify' and `untabify' parameters :<p>
<p>
<blockquote><pre>%section "Untabified Section",untabify
%section "Leave those tabs alone", tabify
</pre></blockquote>
Tabs are simply ignored when comments are reformatted (well, actually, they're just copied into the output, but the target documentation method will ignore them).<p>
<a name="n20"></a><h3> Ignoring comments</h3>
To ignore the comments in a particular section, you can use the `ignore' parameter. For example :<p>
<blockquote><pre>
%section "No Comments", ignore
%section "Keep Comments", keep
</pre></blockquote>
The `<tt>keep</tt>' parameter is used to disable the effect of an ignore parameter (if set by a section's parent).<p>
<a name="n21"></a><h3> C Information</h3>
Normally, each declaration in a file will have a C information tag attached to it. This is usually enclosed in [ ] and contains the return type of a function along with other information. This text can disabled using the `noinfo' parameters and reenabled using the `info' parameter.<p>
<p>
<blockquote><pre>%section "No C Information", noinfo
%section "Print C Information", info
</pre></blockquote>
<a name="n7"></a><h2> Adding Additional Text</h2>
Additional documentation text can be added using the <tt>%text</tt> directive as shown :<p>
<p>
<blockquote><pre><tt>%text %{
</tt>
This is some additional documentation text.
%}
</pre></blockquote>
The <tt>%text</tt> directive is primarily used to add text that is not associated with any particular declaration. For example, you may want to provide a general description of a module before defining all of the functions. Any text can be placed inside the <tt>%{,%}</tt> block except for a `<tt>%}</tt>' that ends the block. For the purposes of sorting, text segments will always appear immediately after the previous declaration.<p>
<a name="n8"></a><h2> Disabling all documentation</h2>
All documentation can be suppressed for a portion of an interface file by using the <tt>%disabledoc</tt> and <tt>%enabledoc </tt>directives. These would be used as follows:<p>
<p>
<blockquote><pre>%disabledoc
... A a bunch of declarations with no documentation ...
%enabledoc
... Now declarations are documented again ...
</pre></blockquote>
These directives can be safely nested. Thus, the occurrence of these directives inside a <tt>%disabledoc </tt>section has no effect (only the outer-most occurrence is important).<p>
<p>
The primary use of these directives is for disabling the documentation on commonly used modules that you might use repeatedly (but don't want any documentation for). For example :<p>
<blockquote><pre>
%disabledoc
%include wish.i
%include array.i
%include timer.i
%enabledoc
</pre></blockquote>
<p>
<a name="n9"></a><h2> An Example</h2>
To illustrate the documentation system in action, here is some code from the SWIG library file `array.i'.<p>
<p>
<blockquote><pre>//
// array.i
// This SWIG library file provides access to C arrays.
%module carray
%section "SWIG C Array Module",info,after,pre,nosort,skip=1,chop_left=3,
chop_right=0,chop_top=0,chop_bottom=0
%text %{
%include array.i
This module provides scripting language access to various kinds of C/C++
arrays. For each datatype, a collection of four functions are created :
&lt;type&gt;_array(size) : Create a new array of given size
&lt;type&gt;_get(array, index) : Get an element from the array
&lt;type&gt;_set(array, index, value) : Set an element in the array
&lt;type&gt;_destroy(array) : Destroy an array
The functions in this library are only low-level accessor functions
designed to directly access C arrays. Like C, no bounds checking is
performed so use at your own peril.
%}
// -----------------------------------------------------------------------
// Integer array support
// -----------------------------------------------------------------------
%subsection "Integer Arrays"
/* The following functions provide access to integer arrays (mapped
onto the C 'int' datatype. */
%{
... Supporting C code ...
%}
int *int_array(int nitems);
/* Creates a new array of integers. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++. */
void int_destroy(int *array);
/* Destroys the given array. */
int int_get(int *array, int index);
/* Returns the value of array[index]. */
int int_set(int *array, int index, int value);
/* Sets array[index] = value. Returns value. */
// -----------------------------------------------------------------------
// Floating point
// -----------------------------------------------------------------------
%subsection "Floating Point Arrays"
/* The following functions provide access to arrays of floats and doubles. */
%{
.. Supporting C code ...
%}
double *double_array(int nitems);
/* Creates a new array of doubles. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++. */
void double_destroy(double *array);
/* Destroys the given array. */
double double_get(double *array, int index);
/* Returns the value of array[index]. */
double double_set(double *array, int index, double value);
/* Sets array[index] = value. Returns value. */
float *float_array(int nitems);
/* Creates a new array of floats. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++. */
void float_destroy(float *array);
/* Destroys the given array. */
float float_get(float *array, int index);
/* Returns the value of array[index]. */
float float_set(float *array, int index, float value);
/* Sets array[index] = value. Returns value. */
// -----------------------------------------------------------------------
// Character strings
// -----------------------------------------------------------------------
%subsection "String Arrays"
%text %{
The following functions provide support for the 'char **' datatype. This
is primarily used to handle argument lists and other similar structures that
need to be passed to a C/C++ function.
%}
#if defined(SWIGTCL)
%text %{
To convert from a Tcl list into a 'char **', the following code can be used :
# $list is a list
set args [string_array expr {[llength $list] + 1}]
set i 0
foreach a $list {
string_set $args $i $a
incr i 1
}
string_set $i ""
# $args is now a char ** type
%}
#elif defined(SWIGPERL)
%text %{
To convert from a Perl list into a 'char **', code similar to the following
can be used :
# @list is a list
my $l = scalar(@list);
my $args = string_array($l+1);
my $i = 0;
foreach $arg (@list) {
string_set($args,$i,$arg);
$i++;
}
string_set($args,$i,"");
(of course, there is always more than one way to do it)
%}
#elif defined(SWIGPYTHON)
%text %{
To convert from a Python list to a 'char **', code similar to the following
can be used :
# 'list' is a list
args = string_array(len(list)+1)
for i in range(0,len(list)):
string_set(args,i,list[i])
string_set(args,len(list),"")
%}
#endif
%{
... Supporting C code ...
%}
char **string_array(int nitems);
/* Creates a new array of strings. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++. Each element
of the array is set to NULL upon initialization. */
void string_destroy(char *array);
/* Destroys the given array. Each element of the array is assumed to be
a NULL-terminated string allocated with malloc() or new(). All of
these strings will be destroyed as well. (It is probably only safe to
use this function on an array created by string_array) */
char *string_get(char **array, int index);
/* Returns the value of array[index]. Returns a string of zero length
if the corresponding element is NULL. */
char *string_set(char **array, int index, char *value);
/* Sets array[index] = value. value is assumed to be a NULL-terminated
string. A string of zero length is mapped into a NULL value. When
setting the value, the value will be copied into a new string allocated
with malloc() or new(). Any previous value in the array will be
destroyed. */
</pre></blockquote>
<p>
In this file, all of the declarations are placed into a new section. We specify formatting information for our section. Since this is a general purpose library file, we have no idea what formatting our parent might be using so an explicit declaration makes sure we get it right. Each comment contains preformatted text describing each function. Finally, in the case of the string functions, we are using a combination of conditional compilation and documentation system directives to produce language-specific documentation. In this case, the documentation contains a usage example in the target scripting language.<p>
<p>
When processed through the ASCII module, this file will produce documentation similar to the following :<p>
<p>
<blockquote><pre>7. SWIG C Array Module
=======================
%include array.i
This module provides scripting language access to various kinds of C/C++
arrays. For each datatype, a collection of four functions are created :
&lt;type&gt;_array(size) : Create a new array of given size
&lt;type&gt;_get(array, index) : Get an element from the array
&lt;type&gt;_set(array, index, value) : Set an element in the array
&lt;type&gt;_destroy(array) : Destroy an array
The functions in this library are only low-level accessor functions
designed to directly access C arrays. Like C, no bounds checking is
performed so use at your own peril.
7.1. Integer Arrays
--------------------
The following functions provide access to integer arrays (mapped
onto the C 'int' datatype.
int_array(nitems)
[ returns int * ]
Creates a new array of integers. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++.
int_destroy(array)
[ returns void ]
Destroys the given array.
int_get(array,index)
[ returns int ]
Returns the value of array[index].
int_set(array,index,value)
[ returns int ]
Sets array[index] = value. Returns value.
7.2. Floating Point Arrays
---------------------------
The following functions provide access to arrays of floats and doubles.
double_array(nitems)
[ returns double * ]
Creates a new array of doubles. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++.
double_destroy(array)
[ returns void ]
Destroys the given array.
double_get(array,index)
[ returns double ]
Returns the value of array[index].
double_set(array,index,value)
[ returns double ]
Sets array[index] = value. Returns value.
float_array(nitems)
[ returns float * ]
Creates a new array of floats. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++.
float_destroy(array)
[ returns void ]
Destroys the given array.
float_get(array,index)
[ returns float ]
Returns the value of array[index].
float_set(array,index,value)
[ returns float ]
Sets array[index] = value. Returns value.
7.3. String Arrays
-------------------
The following functions provide support for the 'char **' datatype. This
is primarily used to handle argument lists and other similar structures that
need to be passed to a C/C++ function.
To convert from a Python list to a 'char **', code similar to the following
can be used :
# 'list' is a list
args = string_array(len(list)+1)
for i in range(0,len(list)):
string_set(args,i,list[i])
string_set(args,len(list),"")
string_array(nitems)
[ returns char ** ]
Creates a new array of strings. nitems specifies the number of elements.
The array is created using malloc() in C and new() in C++. Each element
of the array is set to NULL upon initialization.
string_destroy(array)
[ returns void ]
Destroys the given array. Each element of the array is assumed to be
a NULL-terminated string allocated with malloc() or new(). All of
these strings will be destroyed as well. (It is probably only safe to
use this function on an array created by string_array)
string_get(array,index)
[ returns char * ]
Returns the value of array[index]. Returns a string of zero length
if the corresponding element is NULL.
string_set(array,index,value)
[ returns char * ]
Sets array[index] = value. value is assumed to be a NULL-terminated
string. A string of zero length is mapped into a NULL value. When
setting the value, the value will be copied into a new string allocated
with malloc() or new(). Any previous value in the array will be
destroyed.
</pre></blockquote>
<a name="n10"></a><h2> ASCII Documentation</h2>
The ASCII module produces documentation in plaintext as shown in the previous example. Two formatting options are available (default values shown) :<p>
<p>
<blockquote><pre>ascii_indent = 8
ascii_columns = 70
</pre></blockquote>
`<tt>ascii_indent</tt>' specifies the number of characters to indent each function description. `<tt>ascii_columns</tt>' specifies the width of the output when reformatting text.<p>
<p>
When reformatting text, all extraneous white-space is stripped and text is filled to fit in the specified number of columns. The output text will be left-justified. A single newline is ignored, but multiple newlines can be used to start a new paragraph. The character sequence `\\' can be used to force a newline.<p>
<p>
Preformatted text is printed into the resulting output unmodified although it may be indented when used as part of a function description.<p>
<a name="n11"></a><h2> HTML Documentation</h2>
The HTML module produces documentation in HTML format (who would have guessed?). However, a number of style parameters are available (shown with default values)<p>
<blockquote><pre>
html_title = "&lt;H1&gt;:&lt;/H1&gt;"
html_contents = "&lt;H1&gt;:&lt;/H1&gt;"
html_section = "&lt;HR&gt;&lt;H2&gt;:&lt;/H2&gt;"
html_subsection = "&lt;H3&gt;:&lt;/H3&gt;"
html_subsubsection = "&lt;H4&gt;:&lt;/H4&gt;"
html_usage = "&lt;B&gt;&lt;TT&gt;:&lt;/TT&gt;&lt;/B&gt;"
html_descrip = "&lt;BLOCKQUOTE&gt;:&lt;/BLOCKQUOTE&gt;"
html_text = "&lt;P&gt;"
html_cinfo = ""
html_preformat = "&lt;PRE&gt;:&lt;/PRE&gt;"
html_body = "&lt;BODY bg_color=\"#ffffff\"&gt;:&lt;/BODY&gt;"
</pre></blockquote>
<p>
Any of these parameters can be changed, by simply specifying them after a <tt>%title</tt> or <tt>%section</tt> directive. However, the effects are applied globally so it probably makes sense to use the <tt>%style</tt> directive instead. For example :<p>
<p>
<blockquote><pre>%style html_contents="&lt;HR&gt;&lt;H1&gt;:&lt;/H1&gt;"
... Rest of declarations ...
</pre></blockquote>
Each tag uses a ":" to separate the start and end tags. Any text will be inserted in place of the ":". Since strings are specified in SWIG using quotes, any quotes that need to be inserted into a tag should be escaped using the "\" character.<p>
<p>
Sample HTML output is shown below :<p>
<p><a href="ch5.2.png">Sample Screenshot 1 </a><p>
<p>
<p>
Since our example used preformatted text, the output is very similar to the ASCII module. However, if you use the default mode, it is possible to insert HTML markup directly into your C comments for a more personalized document.<p>
<p>
For navigation within the document, SWIG also produces a table of contents with links to each section within the document. With a large interface, the contents may look something like this :<p>
<p><a href="ch5.3.png">Sample Screenshot 2 </a><p>
<p>
<p>
<a name="n12"></a><h2> LaTeX Documentation</h2>
The LaTeX module operates in a manner similar to the HTML module. The following style parameters are available (some knowledge of LaTeX is assumed).<p>
<blockquote><pre>
latex_parindent = "0.0in"
latex_textwidth = "6.5in"
latex_documentstyle = "[11pt]{article}"
latex_oddsidemargin = "0.0in"
latex_pagestyle = "\\pagestyle{headings}"
latex_title = "{\\Large \\bf :} \\\\\n"
latex_preformat = "{\\small \\begin{verbatim}:\\end{verbatim}}"
latex_usage = "{\\tt \\bf : }"
latex_descrip = "{\\\\\n \\makebox[0.5in]{} \begin{minipage}[t]{6in} : \n
\\end{minipage} \\\\";
latex_text = ":\\\\"
latex_cinfo = "{\\tt : }"
latex_section = "\\section{:}"
latex_subsection = "\\subsection{:}"
latex_subsubsection = "\\subsubsection{:}"
</pre></blockquote>
The style parameters, well, look downright ugly. Keep in mind that the strings used by SWIG have escape codes in them so it's necessary to represent the `\' character as `\\'. Thus, within SWIG your code will look something like this :<p>
<p>
<blockquote><pre>%style latex_section="\\newpage \n \\section{:}"
</pre></blockquote>
<p>
The default values should be sufficient for creating a readable LaTeX document in any case you don't want to worry changing the default style parameters.<p>
<a name="n13"></a><h2> C++ Support</h2>
C++ classes are encapsulated in a new subsection of the current section. This subsection contains descriptions of all of the member functions and variables. Since language modules are responsible for creating the documentation, the use of shadow classes will result in documentation describing the resulting shadow classes, not the lower level interface to the code.<p>
<p>
While it's not entirely clear that this is the best way to document C++ code, it is a start (and it's better than no documentation).<p>
<a name="n14"></a><h2> The Final Word?</h2>
Early versions of SWIG used a fairly primitive documentation system, that could best be described as "barely usable." The system described here represents an almost total rewrite of the documentation system. While it is, by no means, a perfect solution, I think it is a step in the right direction. The SWIG library is now entirely self-documenting and is a good source of documentation examples. As always suggestions and improvements are welcome.<p>
<p>
<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:46:55 1997</address>
</body>
</html>

View file

@ -0,0 +1,295 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Exception Handling</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1>7 Exception Handling</h1><p><ul>
<li> <a href="#n1">The %except directive</a>
<li> <a href="#n2">Handling exceptions in C code</a>
<li> <a href="#n3">Exception handling with longjmp()</a>
<li> <a href="#n4">Handling C++ exceptions</a>
<li> <a href="#n5">Defining different exception handlers</a>
<li> <a href="#n6">Using The SWIG exception library</a>
<li> <a href="#n7">Debugging and other interesting uses for %except</a>
<li> <a href="#n8">More Examples</a>
</ul>
In some cases, it is desirable to catch errors that occur in C functions and propagate them up to the scripting language interface (ie. raise an exception). By default, SWIG does nothing, but you can create a user-definable exception handler using the <tt>%except</tt> directive.<p>
<a name="n1"></a><h2> The %except directive</h2>
The <tt>%except</tt> directive allows you to define an exception handler. It works something like this :<p>
<p>
<blockquote><pre>%except(python) {
try {
$function
}
catch (RangeError) {
PyErr_SetString(PyExc_IndexError,"index out-of-bounds");
return NULL;
}
}
</pre></blockquote>
<p>
As an argument, you need to specify the target language. The exception handling C/C++ code is then enclosed in braces. The symbol <tt>$function </tt>is replaced with the real C/C++ function call that SWIG would be ordinarily make in the wrapper code. The C code you specify inside the <tt>%except</tt> directive can be anything you like including custom C code and C++ exceptions.<p>
<p>
To delete an exception handler, simply use the <tt>%except</tt> directive with no code. For example :<p>
<p>
<blockquote><pre>%except(python); // Deletes any previously defined handler
</pre></blockquote>
Exceptions can be redefined as necessary. The scope of an exception handler is from the point of definition to the end of the file, the definition of a new exception handler, or until the handler is deleted.<p>
<a name="n2"></a><h2> Handling exceptions in C code</h2>
C has no formal mechanism for handling exceptions so there are many possibilities. The first approach is to simply provide some functions for setting and checking an error code. For example :<p>
<p>
<blockquote><pre>
/* File : except.c */
static char error_message[256];
static int error_status = 0;
void throw_exception(char *msg) {
strncpy(error_message,msg,256);
error_status = 1;
}
void clear_exception() {
error_status = 0;
}
char *check_exception() {
if (error_status) return error_message;
else return NULL;
}
</pre></blockquote>
To work, functions will need to explicitly call <tt>throw_exception()</tt> to indicate an error occurred. For example :<p>
<p>
<blockquote><pre>double inv(double x) {
if (x != 0) return 1.0/x;
else {
throw_exception("Division by zero");
return 0;
}
}
</pre></blockquote>
To catch the exception, you can write a simple exception handler such as the following (shown for Perl5) :<p>
<p>
<blockquote><pre>%except(perl5) {
char *err;
clear_exception();
$function
if ((err = check_exception())) {
croak(err);
}
}
</pre></blockquote>
<p>
Now, when an error occurs, it will be translated into a Perl error. The downside to this approach is that it isn't particularly clean and it assumes that your C code is a willing participant in generating error messages. (This isn't going to magically add exceptions to a code that doesn't have them).<p>
<a name="n3"></a><h2> Exception handling with longjmp()</h2>
Exception handling can also be added to C code using the <tt>&lt;setjmp.h&gt;</tt> library. This usually isn't documented very well (at least not in any of my C books). In any case, here's one implementation that uses the C preprocessor :<p>
<blockquote><pre>
/* File : except.c
Just the declaration of a few global variables we're going to use */
#include &lt;setjmp.h&gt;
jmp_buf exception_buffer;
int exception_status;
/* File : except.h */
#include &lt;setjmp.h&gt;
extern jmp_buf exception_buffer;
extern int exception_status;
#define try if ((exception_status = setjmp(exception_buffer)) == 0)
#define catch(val) else if (exception_status == val)
#define throw(val) longjmp(exception_buffer,val)
#define finally else
/* Exception codes */
#define RangeError 1
#define DivisionByZero 2
#define OutOfMemory 3
</pre></blockquote>
Now, within a C program, you can do the following :<p>
<p>
<blockquote><pre>double inv(double x) {
if (x) return 1.0/x;
else {throw(DivisionByZero);
}
</pre></blockquote>
Finally, to create a SWIG exception handler, write the following :<p>
<p>
<blockquote><pre>%{
#include "except.h"
%}
%except(perl5) {
try {
$function
} catch(RangeError) {
croak("Range Error");
} catch(DivisionByZero) {
croak("Division by zero");
} catch(OutOfMemory) {
croak("Out of memory");
} finally {
croak("Unknown exception");
}
}
</pre></blockquote>
<p>
At this point, you're saying this sure looks alot like C++ and you'd be right (C++ exceptions are often implemented in a similar manner). As always, the usual disclaimers apply--your mileage may vary. <p>
<a name="n4"></a><h2> Handling C++ exceptions</h2>
Handling C++ exceptions is almost completely trivial (well, all except for the actual C++ part). A typical SWIG exception handler will look like this :<p>
<p>
<blockquote><pre>%except(perl5) {
try {
$function
} catch(RangeError) {
croak("Range Error");
} catch(DivisionByZero) {
croak("Division by zero");
} catch(OutOfMemory) {
croak("Out of memory");
} catch(...) {
croak("Unknown exception");
}
}
</pre></blockquote>
The exception types need to be declared as classes elsewhere, possibly in a header file :<p>
<p>
<blockquote><pre>class RangeError {};
class DivisionByZero {};
class OutOfMemory {};
</pre></blockquote>
Newer versions of the SWIG parser should ignore exceptions specified in function declarations. For example :<p>
<p>
<blockquote><pre>double inv(double) throw(DivisionByZero);
</pre></blockquote>
<a name="n5"></a><h2> Defining different exception handlers</h2>
By default, the <tt>%except</tt> directive creates an exception handler that is used for all wrapper functions that follow it. Creating one universal exception handler for all functions may be unwieldy and promote excessive code bloat since the handler will be inlined into each wrapper function created. For this reason, the exception handler can be redefined at any point in an interface file. Thus, a more efficient use of exception handling may work like this :<p>
<p>
<blockquote><pre>%except(python) {
... your exception handler ...
}
/* Define critical operations that can throw exceptions here */
%except(python); // Clear the exception handler
/* Define non-critical operations that don't throw exceptions */
</pre></blockquote>
<a name="n9"></a><h3> Applying exception handlers to specific datatypes.</h3>
An alternative approach to using the <tt>%except</tt> directive is to use the "except" typemap. This allows you to attach an error handler to specific datatypes and function name. The typemap is applied to the return value of a function. For example :<p>
<p>
<blockquote><pre>%typemap(python,except) void * {
$function
if (!$source) {
PyExc_SetString(PyExc_MemoryError,"Out of memory in $name");
return NULL;
}
}
void *malloc(int size);
</pre></blockquote>
When applied, this will automatically check the return value of <tt>malloc()</tt> and raise an exception if it's invalid. For example :<p>
<p>
<blockquote><pre>Python 1.4 (Jan 16 1997) [GCC 2.7.2]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
&gt;&gt;&gt; from example import *
&gt;&gt;&gt; a = malloc(2048)
&gt;&gt;&gt; b = malloc(1500000000)
Traceback (innermost last):
File "&lt;stdin&gt;", line 1, in ?
MemoryError: Out of memory in malloc
&gt;&gt;&gt;
</pre></blockquote>
<p>
Since typemaps can be named, you can define an exception handler for a specific function as follows :<p>
<p>
<blockquote><pre>%typemap(python,except) void *malloc {
...
}
</pre></blockquote>
This will only be applied to the <tt>malloc()</tt> function returning <tt>void *</tt>. While you probably wouldn't want to write a different exception handler for every function, it is possible to have a high degree of control if you need it. When typemaps are used, they override any exception handler defined with <tt>%except</tt>.<p>
<a name="n6"></a><h2> Using The SWIG exception library</h2>
The <tt>exception.i</tt> library file provides support for creating language independent exceptions in your interfaces. To use it, simply put an "<tt>%include exception.i</tt>" in your interface file. This creates a function<tt> SWIG_exception()</tt> that can be used to raise scripting language exceptions in a portable manner. For example :<p>
<p>
<blockquote><pre>// Language independent exception handler
%include exception.i
%except {
try {
$function
} catch(RangeError) {
SWIG_exception(SWIG_ValueError, "Range Error");
} catch(DivisionByZero) {
SWIG_exception(SWIG_DivisionByZero, "Division by zero");
} catch(OutOfMemory) {
SWIG_exception(SWIG_MemoryError, "Out of memory");
} catch(...) {
SWIG_exception(SWIG_RuntimeError,"Unknown exception");
}
}
</pre></blockquote>
As arguments, <tt>SWIG_exception()</tt> takes an error type code (an integer) and an error message string. The currently supported error types are :<p>
<p>
<blockquote><pre>SWIG_MemoryError
SWIG_IOError
SWIG_RuntimeError
SWIG_IndexError
SWIG_TypeError
SWIG_DivisionByZero
SWIG_OverflowError
SWIG_SyntaxError
SWIG_ValueError
SWIG_SystemError
SWIG_UnknownError
</pre></blockquote>
<p>
Since the <tt>SWIG_exception()</tt> function is defined at the C-level it can be used elsewhere in SWIG. This includes typemaps and helper functions. The exception library provides a language-independent exception handling mechanism, so many of SWIG's library files now rely upon the library as well.<p>
<a name="n7"></a><h2> Debugging and other interesting uses for %except</h2>
Since the <tt>%except </tt>directive allows you to encapsulate the actual C function call, it can also be used for debugging and tracing operations. For example :<p>
<p>
<blockquote><pre>%except(tcl) {
printf("Entering function : $name\n");
$function
printf("Leaving function : $name\n");
}
</pre></blockquote>
allows you to follow the function calls in order to see where an application might be crashing. <p>
<p>
Exception handlers can also be chained. For example :<p>
<p>
<blockquote><pre>%except(tcl) {
printf("Entering function : $name\n");
$except
printf("Leaving function : $name\n");
}
</pre></blockquote>
Any previously defined exception handler will be inserted in place of the "<tt>$except</tt>" symbol. As a result, you can attach debugging code to existing handlers if necessary. However, it should be noted that this must be done before any C/C++ declarations are made (as exception handlers are applied immediately to all functions that follow them).<p>
<a name="n8"></a><h2> More Examples</h2>
By now, you know most of the exception basics. See the SWIG Examples directory for more examples and ideas. Further chapters show how to generate exceptions in specific scripting languages.<p>
<p>
<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:46:57 1997</address>
</body>
</html>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,559 @@
<html>
<head>
<title> SWIG 1.1 Users Manual - Topic Index </title>
</head>
<body bgcolor="#ffffff">
<h1> Alphabetical Topic Index </h1>
<h2> - A - </h2>
<blockquote>
<a href="Python.html#n53">A complete application</a> (9 SWIG and Python)<br>
<a href="Advanced.html#n7">A few dynamic loading gotchas</a> (11 Advanced Topics)<br>
<a href="Tcl.html#n49">A few helper functions</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n10">A general interface building strategy</a> (3 SWIG Basics)<br>
<a href="Python.html#n46">A mathematical function plotter</a> (9 SWIG and Python)<br>
<a href="Extending.html#n40">A Quick Intermission</a> (12 Extending SWIG)<br>
<a href="Python.html#n29">A simple example</a> (9 SWIG and Python)<br>
<a href="Typemaps.html#n16">A simple example</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Perl5.html#n5">A simple Perl example</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n67">A simple shadow class</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n31">A simple SWIG interface file</a> (8 SWIG and Perl5)<br>
<a href="Perl5.html#n37">A simple typemap example</a> (8 SWIG and Perl5)<br>
<a href="Introduction.html#n5">A SWIG example</a> (1 Introduction)<br>
<a href="Extending.html#n5">A tour of SWIG datatypes</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n30">A Typemap Implementation</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Tcl.html#n5">About the examples</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n5">About the Examples</a> (9 SWIG and Python)<br>
<a href="Preface.html#n3">About this manual</a> (Preface)<br>
<a href="Python.html#n37">Accessing array data</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n43">Accessing array structure members</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n62">Accessing array structure members</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n8">Accessing arrays</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n6">Accessing arrays and other strange objects</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n36">Accessing member data</a> (10 SWIG and Tcl)<br>
<a href="Documentation.html#n7">Adding Additional Text</a> (5 Documentation System)<br>
<a href="SWIG.html#n40">Adding member functions to C structures</a> (3 SWIG Basics)<br>
<a href="Python.html#n66">Adding native Python functions to a SWIG module</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n55">Adding new methods</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n71">Adding new methods</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n17">Additional SWIG options</a> (10 SWIG and Tcl)<br>
<a href="Documentation.html#n9">An Example</a> (5 Documentation System)<br>
<a href="Tcl.html#n50">An OpenGL package</a> (10 SWIG and Tcl)<br>
<a href="Typemaps.html#n5">Applying constraints to input values</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Typemaps.html#n27">Applying constraints to new datatypes</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Exceptions.html#n9">Applying exception handlers to specific datatypes.</a> (7 Exception Handling)<br>
<a href="SWIG.html#n38">Array members</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n30">Arrays</a> (3 SWIG Basics)<br>
<a href="Documentation.html#n10">ASCII Documentation</a> (5 Documentation System)<br>
<a href="Python.html#n31">Automated shadow class generation</a> (9 SWIG and Python)<br>
<a href="Introduction.html#n10">Automatic documentation generation</a> (1 Introduction)<br></blockquote>
<h2> - B - </h2>
<blockquote>
<a href="Perl5.html#n4">Basic Perl interface</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n3">Basic Tcl Interface</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n6">Binary trees in Tcl</a> (10 SWIG and Tcl)<br>
<a href="Preface.html#n6">Bug reports</a> (Preface)<br>
<a href="Tcl.html#n42">Building a C data structure in Tcl</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n20">Building a dynamic module with MakeMaker</a> (8 SWIG and Perl5)<br>
<a href="Perl5.html#n11">Building a Perl5 interface to MATLAB</a> (8 SWIG and Perl5)<br>
<a href="Introduction.html#n15">Building a Perl5 module</a> (1 Introduction)<br>
<a href="Introduction.html#n16">Building a Python module</a> (1 Introduction)<br>
<a href="Python.html#n45">Building a simple 2D imaging class</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n9">Building a simple OpenGL module</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n21">Building a static version of Perl</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n44">Building an object oriented C interface</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n7">Building C/C++ data structures with Tk</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n52">Building large multi-module systems</a> (9 SWIG and Python)<br>
<a href="Introduction.html#n19">Building libraries and modules</a> (1 Introduction)<br>
<a href="Tcl.html#n13">Building new kinds of Tcl interfaces (in Tcl)</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n2">Building Perl Extensions under Windows 95/NT</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n2">Building Python Extensions under Windows 95/NT</a> (9 SWIG and Python)<br>
<a href="Scripting.html#n3">Building scripting language extensions</a> (2 Scripting Languages)<br>
<a href="Tcl.html#n2">Building Tcl/Tk Extensions under Windows 95/NT</a> (10 SWIG and Tcl)<br></blockquote>
<h2> - C - </h2>
<blockquote>
<a href="SWIG.html#n39">C constructors and destructors</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n40">C files</a> (10 SWIG and Tcl)<br>
<a href="Documentation.html#n21">C Information</a> (5 Documentation System)<br>
<a href="SWIG.html#n14">C Preprocessor directives</a> (3 SWIG Basics)<br>
<a href="Introduction.html#n6">C syntax, but not a C compiler</a> (1 Introduction)<br>
<a href="Tcl.html#n32">C++ Classes</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n28">C++ Classes</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n44">C++ example</a> (3 SWIG Basics)<br>
<a href="Extending.html#n11">C++ Processing</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n6">C++ support</a> (3 SWIG Basics)<br>
<a href="Documentation.html#n13">C++ Support</a> (5 Documentation System)<br>
<a href="SWIG.html#n68">Categories</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n37">Changing member data</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n19">Character Strings</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n37">Character strings and structures</a> (3 SWIG Basics)<br>
<a href="Library.html#n17">Checking in library files</a> (4 Multiple files and the SWIG library)<br>
<a href="Library.html#n15">Checking out library files</a> (4 Multiple files and the SWIG library)<br>
<a href="Documentation.html#n3">Choosing a documentation format</a> (5 Documentation System)<br>
<a href="SWIG.html#n62">Class methods</a> (3 SWIG Basics)<br>
<a href="Extending.html#n35">Cleanup</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n77">Code blocks</a> (3 SWIG Basics)<br>
<a href="Extending.html#n9">Code Generation Functions</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n9">Code Insertion</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n66">Combining Tcl/Tk Extensions</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n30">Command Line Options and Basic Initialization</a> (12 Extending SWIG)<br>
<a href="Documentation.html#n16">Comment Formatting variables</a> (5 Documentation System)<br>
<a href="Documentation.html#n18">Comment placement and formatting</a> (5 Documentation System)<br>
<a href="SWIG.html#n13">Comments</a> (3 SWIG Basics)<br>
<a href="Typemaps.html#n7">Common typemap methods</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Tcl.html#n22">Compilation problems</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n22">Compilation problems and compiling with C++</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n19">Compilation problems and compiling with C++</a> (9 SWIG and Python)<br>
<a href="Extending.html#n20">Compiling</a> (12 Extending SWIG)<br>
<a href="Perl5.html#n19">Compiling a dynamic module</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n16">Compiling a dynamic module</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n19">Compiling a dynamic module (Unix)</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n2">Compiling a SWIG extension</a> (12 Extending SWIG)<br>
<a href="Python.html#n32">Compiling modules with shadow classes</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n8">Conditional compilation</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n12">Configuration management with SWIG</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n39">Constants</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n21">Constants</a> (3 SWIG Basics)<br>
<a href="Python.html#n25">Constants</a> (9 SWIG and Python)<br>
<a href="Scripting.html#n9">Constants</a> (2 Scripting Languages)<br>
<a href="Tcl.html#n29">Constants</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n27">Constants</a> (8 SWIG and Perl5)<br>
<a href="Typemaps.html#n26">Constraint methods</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="SWIG.html#n60">Constructors and destructors</a> (3 SWIG Basics)<br>
<a href="Python.html#n72">Constructors and Destructors</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n45">Constructors and destructors</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n57">Converting a Tcl list to a char **</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n58">Converting Python list to a char **</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n41">Converting a Perl5 array to a char **</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n59">Converting a Python file object to a FILE *</a> (9 SWIG and Python)<br>
<a href="Typemaps.html#n34">Copying a typemap</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Tcl.html#n65">Creating a new package initialization library</a> (10 SWIG and Tcl)<br>
<a href="Typemaps.html#n32">Creating a new typemap</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Extending.html#n47">Creating a usage string</a> (12 Extending SWIG)<br>
<a href="Extending.html#n37">Creating a Wrapper Function</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n17">Creating arrays</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Extending.html#n36">Creating Commands</a> (12 Extending SWIG)<br>
<a href="Library.html#n7">Creating Library Files</a> (4 Multiple files and the SWIG library)<br>
<a href="Typemaps.html#n37">Creating local variables</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Advanced.html#n1">Creating multi-module packages</a> (11 Advanced Topics)<br>
<a href="Tcl.html#n33">Creating new objects</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n31">Creating read-only variables</a> (3 SWIG Basics)<br>
<a href="Preface.html#n4">Credits</a> (Preface)<br>
<a href="Introduction.html#n22">Cross platform woes</a> (1 Introduction)<br></blockquote>
<h2> - D - </h2>
<blockquote>
<a href="Exceptions.html#n7">Debugging and other interesting uses for %except</a> (7 Exception Handling)<br>
<a href="Documentation.html#n15">Default Formatting</a> (5 Documentation System)<br>
<a href="SWIG.html#n34">Default/optional arguments</a> (3 SWIG Basics)<br>
<a href="Exceptions.html#n5">Defining different exception handlers</a> (7 Exception Handling)<br>
<a href="SWIG.html#n73">Defining symbols</a> (3 SWIG Basics)<br>
<a href="Typemaps.html#n33">Deleting a typemap</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Tcl.html#n35">Deleting objects</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n24">Derived types, structs, and classes</a> (3 SWIG Basics)<br>
<a href="Documentation.html#n8">Disabling all documentation</a> (5 Documentation System)<br>
<a href="Extending.html#n46">Documentation entries</a> (12 Extending SWIG)<br>
<a href="Introduction.html#n18">Documentation generation</a> (1 Introduction)<br>
<a href="Extending.html#n12">Documentation Processing</a> (12 Extending SWIG)<br>
<a href="Tcl.html#n68">Dynamic loading</a> (10 SWIG and Tcl)<br>
<a href="Advanced.html#n2">Dynamic Loading of C++ modules</a> (11 Advanced Topics)<br></blockquote>
<h2> - E - </h2>
<blockquote>
<a href="Extending.html#n32">Emitting headers and support code</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n50">Enums and constants</a> (3 SWIG Basics)<br>
<a href="Introduction.html#n9">Event driven C programming</a> (1 Introduction)<br>
<a href="Python.html#n9">Exception handling</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n13">Exception handling</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n10">Exception handling</a> (10 SWIG and Tcl)<br>
<a href="Exceptions.html#n3">Exception handling with longjmp()</a> (7 Exception Handling)<br>
<a href="Python.html#n44">Extending and fixing the gd module</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n14">Extending the Tcl Netscape Plugin</a> (10 SWIG and Tcl)<br></blockquote>
<h2> - F - </h2>
<blockquote>
<a href="Extending.html#n7">File management</a> (12 Extending SWIG)<br>
<a href="Extending.html#n34">Final Initialization</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n18">Floating Point</a> (3 SWIG Basics)<br>
<a href="Documentation.html#n6">Formatting</a> (5 Documentation System)<br>
<a href="Python.html#n48">From C to SWIG to Python</a> (9 SWIG and Python)<br>
<a href="Extending.html#n22">Function Parameters</a> (12 Extending SWIG)<br>
<a href="Documentation.html#n4">Function usage and argument names</a> (5 Documentation System)<br>
<a href="Tcl.html#n27">Functions</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n23">Functions</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n25">Functions</a> (8 SWIG and Perl5)<br></blockquote>
<h2> - G - </h2>
<blockquote>
<a href="SWIG.html#n4">Getting down to business</a> (3 SWIG Basics)<br>
<a href="Python.html#n39">Getting even more serious about array access</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n9">Getting serious</a> (8 SWIG and Perl5)<br>
<a href="Perl5.html#n18">Getting the right header files</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n15">Getting the right header files</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n84">Getting the right header files</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n18">Getting the right header files and libraries</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n28">Global variables</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n26">Global variables</a> (8 SWIG and Perl5)<br>
<a href="Perl5.html#n36">Graphical Web-Statistics in Perl5</a> (8 SWIG and Perl5)<br>
<a href="Perl5.html#n30">Graphs</a> (8 SWIG and Perl5)<br></blockquote>
<h2> - H - </h2>
<blockquote>
<a href="Exceptions.html#n4">Handling C++ exceptions</a> (7 Exception Handling)<br>
<a href="Exceptions.html#n2">Handling exceptions in C code</a> (7 Exception Handling)<br>
<a href="Perl5.html#n12">Handling output values (the easy way)</a> (8 SWIG and Perl5)<br>
<a href="Introduction.html#n8">Hands off code generation</a> (1 Introduction)<br>
<a href="Extending.html#n24">Hash Tables</a> (12 Extending SWIG)<br>
<a href="Extending.html#n45">Hints</a> (12 Extending SWIG)<br>
<a href="Extending.html#n43">How C++ processing works</a> (12 Extending SWIG)<br>
<a href="Scripting.html#n2">How does a scripting language talk to C?</a> (2 Scripting Languages)<br>
<a href="Documentation.html#n2">How it works</a> (5 Documentation System)<br>
<a href="Extending.html#n28">How many typemaps are there?</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n88">How to avoid creating the interface from hell</a> (3 SWIG Basics)<br>
<a href="Preface.html#n10">How to avoid reading the manual</a> (Preface)<br>
<a href="Typemaps.html#n13">How to break everything with a typemap</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="SWIG.html#n87">How to cope with C++</a> (3 SWIG Basics)<br>
<a href="Introduction.html#n23">How to survive this manual</a> (1 Introduction)<br>
<a href="Documentation.html#n11">HTML Documentation</a> (5 Documentation System)<br></blockquote>
<h2> - I - </h2>
<blockquote>
<a href="Documentation.html#n20">Ignoring comments</a> (5 Documentation System)<br>
<a href="SWIG.html#n69">Implementations and Protocols</a> (3 SWIG Basics)<br>
<a href="Python.html#n11">Implementing C callback functions in Python</a> (9 SWIG and Python)<br>
<a href="Typemaps.html#n11">Implementing constraints with typemaps</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Tcl.html#n43">Implementing methods in C</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n7">Implementing methods in Perl</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n40">Implementing special Python methods in C</a> (9 SWIG and Python)<br>
<a href="Library.html#n4">Including files on the command line</a> (4 Multiple files and the SWIG library)<br>
<a href="Perl5.html#n54">Inheritance</a> (8 SWIG and Perl5)<br>
<a href="SWIG.html#n52">Inheritance</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n66">Inheritance</a> (3 SWIG Basics)<br>
<a href="Python.html#n77">Inheritance and shadow classes</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n79">Initialization blocks</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n78">Inlined code blocks</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n11">Input format</a> (3 SWIG Basics)<br>
<a href="Typemaps.html#n21">Input Methods</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Typemaps.html#n23">Input/Output Methods</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Advanced.html#n3">Inside the SWIG type-checker</a> (11 Advanced Topics)<br>
<a href="SWIG.html#n61">Instance methods</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n17">Integers</a> (3 SWIG Basics)<br>
<a href="Typemaps.html#n1">Introduction</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Documentation.html#n1">Introduction</a> (5 Documentation System)<br>
<a href="Preface.html#n1">Introduction</a> (Preface)<br>
<a href="Extending.html#n1">Introduction</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n3">Introduction to typemaps</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Tcl.html#n34">Invoking member functions</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n55">Iterators</a> (8 SWIG and Perl5)<br></blockquote>
<h2> - L - </h2>
<blockquote>
<a href="Extending.html#n44">Language extensions</a> (12 Extending SWIG)<br>
<a href="Documentation.html#n12">LaTeX Documentation</a> (5 Documentation System)<br>
<a href="Library.html#n6">Library example</a> (4 Multiple files and the SWIG library)<br>
<a href="Introduction.html#n3">Life after SWIG</a> (1 Introduction)<br>
<a href="Introduction.html#n2">Life before SWIG</a> (1 Introduction)<br>
<a href="SWIG.html#n16">Limitations in the Parser (and various things to keep in mind)</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n67">Limitations to this approach</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n29">Linking to complex variables</a> (3 SWIG Basics)<br>
<a href="Scripting.html#n13">Linking with shared libraries</a> (2 Scripting Languages)<br></blockquote>
<h2> - M - </h2>
<blockquote>
<a href="Tcl.html#n41">Making a quick a dirty Tcl module</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n35">Making a quick and dirty Python module</a> (9 SWIG and Python)<br>
<a href="Library.html#n12">malloc.i</a> (4 Multiple files and the SWIG library)<br>
<a href="Typemaps.html#n4">Managing input and output parameters</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Typemaps.html#n29">Managing special data-types with helper functions</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Extending.html#n38">Manipulating Global Variables</a> (12 Extending SWIG)<br>
<a href="Tcl.html#n60">Mapping C structures into Tcl Lists</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n61">Mapping Python tuples into small arrays</a> (9 SWIG and Python)<br>
<a href="Python.html#n73">Member data</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n48">Member data</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n63">Member data</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n46">Member functions</a> (3 SWIG Basics)<br>
<a href="Python.html#n49">Merging modules</a> (9 SWIG and Python)<br>
<a href="Python.html#n78">Methods that return new objects</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n49">Module and package names</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n68">Module names</a> (9 SWIG and Python)<br>
<a href="Python.html#n22">Modules</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n3">Modules, packages, and classes</a> (8 SWIG and Perl5)<br>
<a href="Library.html#n10">More about the SWIG library</a> (4 Multiple files and the SWIG library)<br>
<a href="Exceptions.html#n8">More Examples</a> (7 Exception Handling)<br>
<a href="Typemaps.html#n28">Motivations for using typemaps</a> (6 Pointers, Constraints, and Typemaps)<br></blockquote>
<h2> - N - </h2>
<blockquote>
<a href="Tcl.html#n56">Name based type conversion</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n57">Name based type conversion</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n40">Name based type conversion</a> (8 SWIG and Perl5)<br>
<a href="Extending.html#n8">Naming Services</a> (12 Extending SWIG)<br>
<a href="Python.html#n76">Nested objects</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n52">Nested Objects</a> (8 SWIG and Perl5)<br>
<a href="SWIG.html#n41">Nested structures</a> (3 SWIG Basics)<br>
<a href="Introduction.html#n7">Non-intrusive interface building</a> (1 Introduction)<br></blockquote>
<h2> - O - </h2>
<blockquote>
<a href="Perl5.html#n51">Object Ownership</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n71">Object ownership</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n7">Objective-C</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n59">Objective-C Example</a> (3 SWIG Basics)<br>
<a href="Preface.html#n9">Organization of this manual</a> (Preface)<br>
<a href="SWIG.html#n72">Other issues</a> (3 SWIG Basics)<br>
<a href="Python.html#n12">Other odds and ends</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n42">Other things to note about structures</a> (3 SWIG Basics)<br>
<a href="Typemaps.html#n22">Output Methods</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="SWIG.html#n33">Overriding call by reference</a> (3 SWIG Basics)<br></blockquote>
<h2> - P - </h2>
<blockquote>
<a href="Typemaps.html#n18">Packing a data structure</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="SWIG.html#n56">Partial class definitions</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n27">Passing complex datatypes by value</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n39">Performance concerns and disabling the object oriented interface</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n79">Performance concerns and hints</a> (9 SWIG and Python)<br>
<a href="Advanced.html#n11">Performance of the type-checker</a> (11 Advanced Topics)<br>
<a href="Perl5.html#n38">Perl5 typemaps</a> (8 SWIG and Perl5)<br>
<a href="Library.html#n13">Placing the files in the library</a> (4 Multiple files and the SWIG library)<br>
<a href="Python.html#n47">Plotting an unstructured mesh</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n47">Pointer handling</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n63">Pointer handling</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n65">Pointer handling</a> (9 SWIG and Python)<br>
<a href="Typemaps.html#n15">Pointer Library Functions</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Perl5.html#n28">Pointers</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n30">Pointers</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n26">Pointers</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n3">Pointers and complex objects</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n35">Pointers to functions</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n75">Predefined Symbols</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n1">Preliminaries</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n1">Preliminaries</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n1">Preliminaries</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n81">Preparing a C program for SWIG</a> (3 SWIG Basics)<br>
<a href="Python.html#n42">Preparing a module</a> (9 SWIG and Python)<br>
<a href="Preface.html#n8">Prerequisites</a> (Preface)<br>
<a href="Extending.html#n14">Prerequisites</a> (12 Extending SWIG)<br>
<a href="Python.html#n74">Printing</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n52">Problems with the OpenGL interface</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n49">Protection</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n64">Protection</a> (3 SWIG Basics)<br>
<a href="Python.html#n8">Putting it all together</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n35">Putting it all together</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n4">Python shadow classes</a> (9 SWIG and Python)<br>
<a href="Python.html#n55">Python typemaps</a> (9 SWIG and Python)<br></blockquote>
<h2> - R - </h2>
<blockquote>
<a href="Python.html#n17">Rebuilding the Python interpreter (aka. static linking)</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n51">References</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n67">Referring to other classes</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n38">Relationship with pointers</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n10">Remapping C datatypes with typemaps</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n58">Remapping constants</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n14">Remapping datatypes with typemaps</a> (8 SWIG and Perl5)<br>
<a href="SWIG.html#n70">Renaming</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n54">Renaming</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n32">Renaming declarations</a> (3 SWIG Basics)<br>
<a href="Extending.html#n18">Required C++ compiler</a> (12 Extending SWIG)<br>
<a href="Extending.html#n17">Required files</a> (12 Extending SWIG)<br>
<a href="Tcl.html#n45">Required files</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n28">Return by value</a> (3 SWIG Basics)<br>
<a href="Perl5.html#n48">Return values</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n59">Returning values in arguments</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n23">Run time pointer type checking</a> (3 SWIG Basics)<br>
<a href="Perl5.html#n17">Running SWIG</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n14">Running SWIG</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n1">Running SWIG</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n16">Running SWIG</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n25">Running SWIG from Developer Studio</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n20">Running SWIG from Developer Studio</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n23">Running SWIG from Developer Studio</a> (8 SWIG and Perl5)<br>
<a href="Advanced.html#n4">Runtime support (and potential problems)</a> (11 Advanced Topics)<br></blockquote>
<h2> - S - </h2>
<blockquote>
<a href="Perl5.html#n32">Sample Perl Script</a> (8 SWIG and Perl5)<br>
<a href="Typemaps.html#n36">Scope</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Extending.html#n33">Setting a module name</a> (12 Extending SWIG)<br>
<a href="Tcl.html#n23">Setting a package prefix</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n8">Shadow classes</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n70">Shadow classes</a> (10 SWIG and Tcl)<br>
<a href="Scripting.html#n11">Shadow classes</a> (2 Scripting Languages)<br>
<a href="Perl5.html#n53">Shadow Functions</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n75">Shadow Functions</a> (9 SWIG and Python)<br>
<a href="Scripting.html#n4">Shared libraries and dynamic loading</a> (2 Scripting Languages)<br>
<a href="Introduction.html#n17">Shortcuts</a> (1 Introduction)<br>
<a href="SWIG.html#n2">Simple C functions, variables, and constants</a> (3 SWIG Basics)<br>
<a href="Typemaps.html#n25">Simple constraint example</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="SWIG.html#n22">Simple pointers</a> (3 SWIG Basics)<br>
<a href="Python.html#n6">Solving a simple heat-equation</a> (9 SWIG and Python)<br>
<a href="Documentation.html#n17">Sorting</a> (5 Documentation System)<br>
<a href="Typemaps.html#n38">Special variables</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Python.html#n64">Standard typemaps</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n62">Standard typemaps</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n46">Standard typemaps</a> (8 SWIG and Perl5)<br>
<a href="Extending.html#n31">Starting the parser</a> (12 Extending SWIG)<br>
<a href="Library.html#n9">Static initialization of multiple modules</a> (4 Multiple files and the SWIG library)<br>
<a href="Scripting.html#n12">Static linking</a> (2 Scripting Languages)<br>
<a href="Tcl.html#n21">Static linking</a> (10 SWIG and Tcl)<br>
<a href="SWIG.html#n47">Static members</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n31">Structures</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n27">Structures</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n29">Structures and C++ classes</a> (8 SWIG and Perl5)<br>
<a href="Scripting.html#n10">Structures and classes</a> (2 Scripting Languages)<br>
<a href="SWIG.html#n5">Structures, unions, and object oriented C programming</a> (3 SWIG Basics)<br>
<a href="Introduction.html#n11">Summary</a> (1 Introduction)<br>
<a href="Python.html#n41">Summary (so far)</a> (9 SWIG and Python)<br>
<a href="SWIG.html#n43">Supported C++ features</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n15">SWIG Directives</a> (3 SWIG Basics)<br>
<a href="Introduction.html#n12">SWIG for Windows and Macintosh</a> (1 Introduction)<br>
<a href="Introduction.html#n13">SWIG interface file</a> (1 Introduction)<br>
<a href="Preface.html#n7">SWIG is free</a> (Preface)<br>
<a href="Introduction.html#n21">SWIG on the Power Macintosh</a> (1 Introduction)<br>
<a href="Introduction.html#n20">SWIG on Windows 95/NT</a> (1 Introduction)<br>
<a href="Extending.html#n15">SWIG Organization</a> (12 Extending SWIG)<br>
<a href="Extending.html#n3">SWIG output</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n12">SWIG Output</a> (3 SWIG Basics)<br>
<a href="Preface.html#n2">SWIG resources</a> (Preface)<br>
<a href="SWIG.html#n57">SWIG, C++, and the Legislation of Morality</a> (3 SWIG Basics)<br></blockquote>
<h2> - T - </h2>
<blockquote>
<a href="Documentation.html#n19">Tabs and other annoyances</a> (5 Documentation System)<br>
<a href="Tcl.html#n54">Tcl typemaps</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n15">Tcl8.0 features</a> (10 SWIG and Tcl)<br>
<a href="Library.html#n11">tclsh.i</a> (4 Multiple files and the SWIG library)<br>
<a href="SWIG.html#n53">Templates</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n74">The #if directive</a> (3 SWIG Basics)<br>
<a href="Exceptions.html#n1">The %except directive</a> (7 Exception Handling)<br>
<a href="Library.html#n2">The %extern directive</a> (4 Multiple files and the SWIG library)<br>
<a href="Library.html#n3">The %import directive</a> (4 Multiple files and the SWIG library)<br>
<a href="Library.html#n1">The %include directive</a> (4 Multiple files and the SWIG library)<br>
<a href="Python.html#n34">The C++ code</a> (9 SWIG and Python)<br>
<a href="Extending.html#n21">The DataType class</a> (12 Extending SWIG)<br>
<a href="Documentation.html#n14">The Final Word?</a> (5 Documentation System)<br>
<a href="SWIG.html#n58">The future of C++ and SWIG</a> (3 SWIG Basics)<br>
<a href="Extending.html#n13">The Future of SWIG</a> (12 Extending SWIG)<br>
<a href="Python.html#n13">The gory details of shadow classes</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n15">The gory details on shadow classes</a> (8 SWIG and Perl5)<br>
<a href="Extending.html#n29">The header file</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n19">The idea (in a nutshell)</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Extending.html#n4">The Language class (simple version)</a> (12 Extending SWIG)<br>
<a href="Python.html#n3">The low-level Python/C interface</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n33">The MATLAB engine interface</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n4">The object oriented interface</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n16">The organization of this chapter</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n76">The output of SWIG</a> (3 SWIG Basics)<br>
<a href="Tcl.html#n71">The step-by-step process for making a plugin extension.</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n23">The String Class</a> (12 Extending SWIG)<br>
<a href="Introduction.html#n14">The swig command</a> (1 Introduction)<br>
<a href="SWIG.html#n82">The SWIG interface file</a> (3 SWIG Basics)<br>
<a href="Library.html#n5">The SWIG library</a> (4 Multiple files and the SWIG library)<br>
<a href="Extending.html#n42">The SWIG library and installation issues</a> (12 Extending SWIG)<br>
<a href="Introduction.html#n4">The SWIG package</a> (1 Introduction)<br>
<a href="Typemaps.html#n2">The SWIG Pointer Library</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Advanced.html#n6">The SWIG runtime library</a> (11 Advanced Topics)<br>
<a href="Python.html#n70">The this pointer</a> (9 SWIG and Python)<br>
<a href="Scripting.html#n1">The two language view of the world</a> (2 Scripting Languages)<br>
<a href="Extending.html#n26">The typemap C API.</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n65">The use of id</a> (3 SWIG Basics)<br>
<a href="Library.html#n16">The world's fastest way to write a Makefile</a> (4 Multiple files and the SWIG library)<br>
<a href="Extending.html#n25">The WrapperFunction class</a> (12 Extending SWIG)<br>
<a href="Documentation.html#n5">Titles, sections, and subsections</a> (5 Documentation System)<br>
<a href="Tcl.html#n69">Turning a SWIG module into a Tcl Package.</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n44">Turning Perl references into C pointers</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n69">Two classes</a> (9 SWIG and Python)<br>
<a href="Advanced.html#n9">Type casting</a> (11 Advanced Topics)<br>
<a href="Advanced.html#n8">Type equivalence</a> (11 Advanced Topics)<br>
<a href="SWIG.html#n26">Typedef</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n36">Typedef and structures</a> (3 SWIG Basics)<br>
<a href="Typemaps.html#n12">Typemap examples</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Typemaps.html#n35">Typemap matching rules</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Perl5.html#n39">Typemap variables</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n56">Typemap variables</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n55">Typemap variables</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n11">Typemaps</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n6">Typemaps (from C)</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n14">Typemaps and the future</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Typemaps.html#n10">Typemaps and the SWIG Library</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Typemaps.html#n9">Typemaps for handling arrays</a> (6 Pointers, Constraints, and Typemaps)<br></blockquote>
<h2> - U - </h2>
<blockquote>
<a href="Python.html#n38">Use Python for control, C for performance</a> (9 SWIG and Python)<br>
<a href="Python.html#n51">Use static linking</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n61">Useful functions</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n45">Useful functions</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n63">Useful Functions</a> (9 SWIG and Python)<br>
<a href="Python.html#n36">Using our new module</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n20">Using a dynamic module</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n49">Using a new documentation module</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n24">Using different names</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Python.html#n50">Using dynamic loading</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n24">Using NMAKE</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n26">Using NMAKE</a> (10 SWIG and Tcl)<br>
<a href="Python.html#n21">Using NMAKE</a> (9 SWIG and Python)<br>
<a href="Typemaps.html#n20">Using some typemaps</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Python.html#n43">Using the gd module</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n51">Using the OpenGL module</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n72">Using the plugin</a> (10 SWIG and Tcl)<br>
<a href="Exceptions.html#n6">Using The SWIG exception library</a> (7 Exception Handling)<br>
<a href="Python.html#n60">Using typemaps to return arguments</a> (9 SWIG and Python)<br>
<a href="Perl5.html#n42">Using typemaps to return values</a> (8 SWIG and Perl5)<br>
<a href="Python.html#n18">Using your module</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n24">Using [incr Tcl] namespaces</a> (10 SWIG and Tcl)<br></blockquote>
<h2> - V - </h2>
<blockquote>
<a href="Python.html#n24">Variable Linking</a> (9 SWIG and Python)<br>
<a href="Scripting.html#n8">Variable linking</a> (2 Scripting Languages)<br>
<a href="SWIG.html#n20">Variables</a> (3 SWIG Basics)<br></blockquote>
<h2> - W - </h2>
<blockquote>
<a href="Perl5.html#n50">What gets created?</a> (8 SWIG and Perl5)<br>
<a href="Extending.html#n27">What happens on typemap lookup?</a> (12 Extending SWIG)<br>
<a href="SWIG.html#n25">What happens when SWIG encounters an unknown datatype?</a> (3 SWIG Basics)<br>
<a href="Python.html#n54">What is a typemap?</a> (9 SWIG and Python)<br>
<a href="Tcl.html#n53">What is a typemap?</a> (10 SWIG and Tcl)<br>
<a href="Typemaps.html#n31">What is a typemap?</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Introduction.html#n1">What is SWIG?</a> (1 Introduction)<br>
<a href="SWIG.html#n85">What to do with main()</a> (3 SWIG Basics)<br>
<a href="Preface.html#n5">What's new?</a> (Preface)<br>
<a href="Python.html#n33">Where to go for more information</a> (9 SWIG and Python)<br>
<a href="Extending.html#n50">Where to go for more information</a> (12 Extending SWIG)<br>
<a href="Perl5.html#n16">Where to go from here?</a> (8 SWIG and Perl5)<br>
<a href="Advanced.html#n10">Why a name based approach?</a> (11 Advanced Topics)<br>
<a href="Advanced.html#n5">Why doesn't C++ inheritance work between modules?</a> (11 Advanced Topics)<br>
<a href="SWIG.html#n83">Why use separate interface files?</a> (3 SWIG Basics)<br>
<a href="Python.html#n30">Why write shadow classes in Python?</a> (9 SWIG and Python)<br>
<a href="Scripting.html#n6">Will adding a scripting language to my C program make it unmanagable?</a> (2 Scripting Languages)<br>
<a href="Scripting.html#n5">Will scripting languages make my C program inefficient?</a> (2 Scripting Languages)<br>
<a href="Library.html#n8">Working with library files</a> (4 Multiple files and the SWIG library)<br>
<a href="SWIG.html#n86">Working with the C preprocessor</a> (3 SWIG Basics)<br>
<a href="SWIG.html#n80">Wrapper code blocks</a> (3 SWIG Basics)<br>
<a href="Scripting.html#n7">Wrapper functions</a> (2 Scripting Languages)<br>
<a href="Python.html#n7">Wrapping a C library</a> (9 SWIG and Python)<br>
<a href="Library.html#n14">Wrapping a library file</a> (4 Multiple files and the SWIG library)<br>
<a href="Perl5.html#n10">Wrapping C libraries and other packages</a> (8 SWIG and Perl5)<br>
<a href="Tcl.html#n46">Wrapping gl.h</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n47">Wrapping glu.h</a> (10 SWIG and Tcl)<br>
<a href="Tcl.html#n48">Wrapping the aux library</a> (10 SWIG and Tcl)<br>
<a href="Perl5.html#n34">Wrapping the MATLAB matrix functions</a> (8 SWIG and Perl5)<br>
<a href="Extending.html#n19">Writing a main program</a> (12 Extending SWIG)<br>
<a href="Tcl.html#n64">Writing a main program and Tcl_AppInit()</a> (10 SWIG and Tcl)<br>
<a href="Extending.html#n48">Writing a new documentation module</a> (12 Extending SWIG)<br>
<a href="Extending.html#n10">Writing a Real Language Module</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n6">Writing new typemaps</a> (6 Pointers, Constraints, and Typemaps)<br>
<a href="Extending.html#n41">Writing the default typemaps</a> (12 Extending SWIG)<br>
<a href="Typemaps.html#n8">Writing typemap code</a> (6 Pointers, Constraints, and Typemaps)<br></blockquote>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:47:18 1997 </address>
</body>
</html>

View file

@ -0,0 +1,323 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Introduction</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1>1 Introduction</h1><p><ul>
<li> <a href="#n1">What is SWIG?</a>
<li> <a href="#n2">Life before SWIG</a>
<li> <a href="#n3">Life after SWIG</a>
<li> <a href="#n4">The SWIG package</a>
<li> <a href="#n5">A SWIG example</a>
<li> <a href="#n6">C syntax, but not a C compiler</a>
<li> <a href="#n7">Non-intrusive interface building</a>
<li> <a href="#n8">Hands off code generation</a>
<li> <a href="#n9">Event driven C programming</a>
<li> <a href="#n10">Automatic documentation generation</a>
<li> <a href="#n11">Summary</a>
<li> <a href="#n12">SWIG for Windows and Macintosh</a>
</ul>
<a name="n1"></a><h2> What is SWIG?</h2>
SWIG is a code development tool that makes it possible to quickly build powerful scripting language interfaces to C, C++, or Objective-C programs. In a nutshell, SWIG is a compiler that takes C declarations and turns them into the "glue" needed to access them from common scripting languages including Perl, Python, and Tcl. SWIG usually requires no modifications to existing C code and can often be used to build a working interface in a matter of minutes. This makes it possible to do a number of interesting things including :<p>
<p>
<ul>
<li>Building powerful interfaces to existing C programs.
<li>Rapid prototyping and application development.
<li>Interactive debugging.
<li>Making a graphical user interface (using Tk for example).
<li>Powerful testing of C libraries and programs (using scripts).
<li>Building high performance C modules for scripting languages.
<li>Making C programming more enjoyable (or tolerable depending on your point of view)
<li>Impressing your friends.
</ul>
<p>
There are some computer scientists who seem to believe that the only way to solve complicated problems is to create software of epic proportions and to have some sort of "grand" software design. Unfortunately this seems to lead to solutions that are even more complicated than the original problem. This, in turn enables the user to forget about the original problem and spend their time cursing at their machine (hence, the term "enabling technology"). SWIG, on the other hand, was developed because I was fed up with how much time I was wasting trying to develop flexible scientific applications. I wanted a tool that would let me use scripting languages to glue different things together, but didn't get in the way of the real problems I was working on. I wanted a simple tool that scientists and engineers could use to put together applications involving number crunching, data analysis, and visualization without having to worry about tedious systems programming, making substantial modifications to existing code, trying to figure out a big monolithic computing "environment," or having to get a second degree in computer science. In short, I wanted to have a tool that would improve application development, but stay out of the way as much as possible.<p>
<a name="n2"></a><h2> Life before SWIG</h2>
SWIG was developed to make my life easier as a C programmer. While C is great for high performance and systems programming, trying to make an interactive and highly flexible C program is a nightmare (in fact, it's much worse than that). The real problem is that for every C program I wrote, I needed to have some sort of interface, but being more interested in other problems, I would always end up writing a really bad interface that was hard to extend, hard to modify, and hard to use. I suppose I could have tried to do something fancy using X11, but who has time to waste weeks or months trying to come up with an interface that is probably going to end up being larger than the original C code? There are more interesting problems to work on.<p>
<p>
The real problem, perhaps, is that most C programs end up being structured as follows :<p>
<p>
<ul>
<li>A collection of functions and variables.
<li>A <tt>main()</tt> program that starts everything up.
<li>A bunch of hacks added to make it usable.
</ul>
<p>
The <tt>main()</tt> program may be written to handle command line arguments or to read data from <tt>stdin</tt>, but either way, modifying or extending the program to do something new requires changing the C code, recompiling, and testing. If you make a mistake, you need to repeat this cycle until things work. Of course, as more and more features are added, your C program turns into a hideous unintelligible mess that is even more difficult to modify than it was before (Of course, if you're lucky, your project starts out as an unintelligible mess).<p>
<a name="n3"></a><h2> Life after SWIG</h2>
With SWIG, I was hoping to avoid many of the headaches of working with C programs, by structuring things as follows :<p>
<p>
<ul>
<li>A collection of functions and variables.
<li>A nice interpreted interface language that can be used to access everything.
</ul>
<p>
With this model, you keep all of the functionality of your C program, but access it through a scripting language interface instead of writing more C code. This is nice because you are given full freedom to call functions in any order, access variables, and write scripts to do all sorts of interesting things. If you want to change something, just modify a script, and rerun it. If you're trying to debug a collection of functions, you can call them individually and see what they do. If you're trying to build a package out of various components, you can just glue everything together with a scripting language and have a common interface to all of the various pieces. <p>
<p>
SWIG tries to make the integration between scripting languages and C as painless as possible. This allows you to focus on the underlying C program and using the high-level scripting language interface, but not the tedious and complex chore of making the two languages talk to each other.<p>
<p>
I like this model of programming. While it's certainly no "silver-bullet", it has made programming alot more enjoyable and has enabled us to solve complicated problems that would have been unneccessarily difficult using only C.<p>
<a name="n4"></a><h2> The SWIG package</h2>
SWIG is a compiler that takes ANSI C declarations and turns them into a file containing the C code for binding C functions, variables, and constants to a scripting language (SWIG also supports C++ class and Objective-C interface definitions). Input is specified in the form of an "interface file" containing declarations (input can also be given from C source files provided they are sufficiently clean). The SWIG parser takes this input file and passes it on to a code generation and documentation module. These modules produce an interface for a particular scripting language along with a document describing the interface that was created. Different scripting languages are supported by writing new back-end modules to the system.<p>
<p><center><img src="ch1.1.png"></center><p>
<p>
<a name="n5"></a><h2> A SWIG example</h2>
The best way to illustrate SWIG is with a simple example. Consider the following C code: <p>
<p>
<blockquote><pre>/* File : example.c */
double My_variable = 3.0;
/* Compute factorial of n */
int fact(int n) {
if (n &lt;= 1) return 1;
else return n*fact(n-1);
}
/* Compute n mod m */
int my_mod(int n, int m) {
return(n % m);
}
</pre></blockquote>
<p>
Suppose that you wanted to access these functions and the global variable <tt>My_variable</tt> from Tcl. We start by making a SWIG interface file as shown below (by convention, these files carry a .i suffix) : <p>
<a name="n13"></a><h3> SWIG interface file</h3>
<blockquote><pre>
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
%}
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
</pre></blockquote>
<p>
The interface file contains ANSI C function prototypes and variable declarations. The <tt>%module</tt> directive defines the name of the module that will be created by SWIG. The <tt>%{,%}</tt> block provides a location for inserting additional code such as C header files or additional C functions. <p>
<a name="n14"></a><h3> The swig command</h3>
SWIG is invoked using the <tt>swig</tt> command. We can use this to build a Tcl module (under Linux) as follows :<p>
<p>
<blockquote><pre>unix &gt; swig -tcl example.i
Generating wrappers for Tcl.
unix &gt; gcc -c -fpic example.c example_wrap.c -I/usr/local/include
unix &gt; gcc -shared example.o example_wrap.o -o example.so
unix &gt; tclsh
% load ./example.so
% fact 4
24
% my_mod 23 7
2
% expr $My_variable + 4.5
7.5
%
</pre></blockquote>
<p>
The <tt>swig</tt> command produced a new file called <tt>example_wrap.c</tt> that should be compiled along with the <tt>example.c</tt> file. Most operating systems and scripting languages now support dynamic loading of modules. In our example, our Tcl module has been compiled into a shared library that can be loaded into Tcl and used. When loaded, Tcl will now have our new functions and variables added to it. Taking a careful look at the file <tt>example_wrap.c</tt> reveals a hideous mess, but fortunately you almost never need to worry about it.<p>
<a name="n15"></a><h3> Building a Perl5 module</h3>
Now, let's turn these functions into a Perl5 module. Without making any changes type the following (shown for Solaris):<p>
<p>
<p>
<blockquote><pre>unix &gt; swig -perl5 example.i
Generating wrappers for Perl5
unix &gt; gcc -c example.c example_wrap.c \
-I/usr/local/lib/perl5/sun4-solaris/5.003/CORE
unix&gt; ld -G example.o example_wrap.o -o example.so # This is for Solaris
unix &gt; perl5.003
use example;
print example::fact(4), "\n";
print example::my_mod(23,7), "\n";
print $example::My_variable + 4.5, "\n";
&lt;ctrl-d&gt;
24
2
7.5
unix &gt;
</pre></blockquote>
<a name="n16"></a><h3> Building a Python module</h3>
Finally, let's build a module for Python1.4 (shown for Irix).<p>
<p>
<blockquote><pre>unix &gt; swig -python example.i
Generating wrappers for Python
unix &gt; gcc -c example.c example_wrap.c -I/usr/local/include/python1.4
unix &gt; ld -shared example.o example_wrap.o -o examplemodule.so # Irix
unix &gt; Python
Python 1.4 (Sep 10 1996) [GCC 2.7.0]
Copyright 1991-1996 Stichting Mathematisch Centrum,
Amsterdam
&gt;&gt;&gt; import example
&gt;&gt;&gt; example.fact(4)
24
&gt;&gt;&gt; example.my_mod(23,7)
2
&gt;&gt;&gt; example.cvar.My_variable + 4.5
7.5
</pre></blockquote>
<a name="n17"></a><h3> Shortcuts</h3>
To the truly lazy programmer, one may wonder why we needed the extra interface file at all. As it turns out, we can often do without it. For example, we could also build a Perl5 module by just running SWIG on the C source and specifying a module name as follows<p>
<p>
<blockquote><pre>% swig -perl5 -module example example.c
unix &gt; gcc -c example.c example_wrap.c \
-I/usr/local/lib/perl5/sun4-solaris/5.003/CORE
unix&gt; ld -G example.o example_wrap.o -o example.so
unix &gt; perl5.003
use example;
print example::fact(4), "\n";
print example::my_mod(23,7), "\n";
print $example::My_variable + 4.5, "\n";
&lt;ctrl-d&gt;
24
2
7.5
</pre></blockquote>
<p>
Of course, there are some restrictions as SWIG is not a full C/C++ parser. If you make heavy use of the C preprocessor, complicated declarations, or C++, giving SWIG a raw source file probably isn't going to work very well (in this case, you would probably want to use a separate interface file). <p>
<p>
SWIG also supports a limited form of conditional compilation. If we wanted to make a combination SWIG/C header file, we might do the following :<p>
<p>
<blockquote><pre>/* File : example.h */
#ifdef SWIG
%module example
%include tclsh.i
#endif
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
</pre></blockquote>
<a name="n18"></a><h3> Documentation generation</h3>
In addition to producing an interface, SWIG also produces documentation. For our simple example, the documentation file may look like this :<p>
<blockquote><pre>
example_wrap.c
[ Module : example, Package : example ]
$My_variable
[ Global : double My_variable ]
fact(n);
[ returns int ]
my_mod(n,m);
[ returns int ]
get_time();
[ returns char * ]
</pre></blockquote>
<p>
C comments can be used to provide additional descriptions. SWIG can even grab these out of C source files in a variety of ways. For example, if we process <tt>example.c</tt> as follows :<p>
<p>
<blockquote><pre>swig -perl5 -Sbefore -module example example.c
</pre></blockquote>
We will get a documentation file that looks like this (with our C comments added) :<p>
<blockquote><pre>
example_wrap.c
[ Module : example, Package : example ]
$My_variable
[ Global : double My_variable ]
fact(n);
[ returns int ]
Compute factorial of n
my_mod(n,m);
[ returns int ]
Compute n mod m
</pre></blockquote>
<a name="n19"></a><h3> Building libraries and modules</h3>
In addition to generating wrapper code, SWIG provides extensive support for handling multiple files and building interface libraries. For example, our <tt>example.i</tt> file, could be used in another interface as follows :<p>
<p>
<blockquote><pre>%module foo
%include example.i // Get definitions from example.i
... Now more declarations ...
</pre></blockquote>
In a large system, an interface might be built from a variety of pieces like this :<p>
<p>
<blockquote><pre>%module package
%include network.i
%include file.i
%include graphics.i
%include objects.i
%include simulation.i
</pre></blockquote>
SWIG comes with a library of existing functions known as the SWIG library. The library contains a mix of language independent and language dependent functionality. For example, the file `<tt>array.i</tt>' provides access to C arrays while the file `<tt>wish.i</tt>' includes specialized code for rebuilding the Tcl wish interpreter. Using the library, you can use existing modules to build up your own personalized environment for building interfaces. If changes are made to any of the components, they will appear automatically the next time SWIG is run. <p>
<a name="n6"></a><h2> C syntax, but not a C compiler</h2>
SWIG uses ANSI C syntax, but is not a full ANSI C compiler. By using C syntax, I hope to make SWIG easy to use with most C programs, easy to learn, and easy to remember. Other tools tend to use a precise interface definition language, but I personally find this approach to be painful. When I want to build an interface to a collection of several hundred C functions, I don't necessarily want to write a special interface definition for each one. Nor do I want to have to go dig up the manual because I can't remember the syntax.<p>
<p>
On the other hand, using C syntax can be ambiguous. For example, if you have the following declaration <p>
<p>
<blockquote><pre>int foo(double *a);
</pre></blockquote>
We don't really know if <tt>a</tt> is an array of some fixed size, a dynamically allocated block of memory, a single value, or an output value of the function. For the most part, SWIG takes a literal approach (<tt>a</tt> is obviously a <tt>double *</tt>) and leaves it up to the user to use the function in a correct manner. Fortunately, there are several ways to help SWIG out using additional directives and "helper" functions. Thus, the input to SWIG is often a mix of C declarations, special directives and hints. Like Perl programming, there is almost always more than one way to do almost anything.<p>
<p>
SWIG does not currently parse every conceivable type of C declaration that it might encounter in a C/C++ file. Many things may be difficult or impossible to integrate with a scripting language (C++ templates for example). Thus, SWIG may not recognize advanced C/C++ constructs. I make no apologies for this--SWIG was designed to access C, but was never intended to magically turn scripting languages into some sort of bizarre C++ interpreter. Of course, SWIG's parser is always being improved so currently unsupported features may be supported in later releases.<p>
<a name="n7"></a><h2> Non-intrusive interface building</h2>
When used as I intended, SWIG requires minimal modification to existing C code. This makes SWIG extremely easy to use with existing packages, but also promotes software reuse and modularity. By making the C code independent of the high level interface, you can change the interface and reuse the code in other applications. In a similar spirit, I don't believe that there is any one "best" scripting language--use whichever one you like (they're all pretty good). There's no real reason why a particular application couldn't support multiple scripting language interfaces to serve different needs (or to have no scripting language interface at all).<p>
<a name="n8"></a><h2> Hands off code generation</h2>
SWIG is designed to produce working code that needs no hand-modification (in fact, if you look at the output, you probably won't want to modify it). Ideally, SWIG should be invoked automatically inside a Makefile just as one would call the C compiler. You should think of your scripting language interface being defined entirely by the input to SWIG, not the resulting output file. While this approach may limit flexibility for hard-core hackers, it allows others to forget about the low-level implementation details. This is my goal.<p>
<a name="n9"></a><h2> Event driven C programming</h2>
By adding a scripting language interface to a program, SWIG encourages an event-driven style of programming (although it may not be immediately obvious). An event driven program basically consists of a large-collection of functions (called callback functions), and an infinite loop that just sits and waits for the user to do something. When the user does something like type a command, hit a key, or move the mouse the program will call a function to perform an operation--this is an event. Of course, unless you've been living in cave for the last 20 years, you've used this kind of approach when running any sort of graphical user interface.<p>
<p>
While you may not be using SWIG to develop a GUI, the underlying concept is the same. The scripting language acts as an event-loop waiting for you to issue commands. Unlike a traditional C application (that may use command line options), there is no longer a fixed sequence of operations. Functions may be issued in any order and at any time. Of course, this flexibility is exactly what we want! <p>
<p>
However, there are a number of things to keep in mind when developing an application with SWIG :<p>
<p>
<ul>
<li>Functions may be called at any time and in any order. It is always a good idea for functions to check their arguments and internal state to see if it's legal to proceed (Not doing so usually results in mysterious crashes later on).
<li>Code should be structured as a collection of independent modules, not a huge mess of interrelated functions and variables (ie. spaghetti code).
<li>Global variables should be used with care.
<li>Careful attention to the naming of variables and functions may be required to avoid namespace conflicts when combining packages.
</ul>
<p>
While it may be hard (or impossible) to address all these problems in a legacy code, I believe that using SWIG encourages all of the above qualities when developing new applications. This results in code that is more reliable, more modular, and easier to integrate into larger packages. By providing a non-intrusive, easy to use tool, it is possible to develop highly reliable event-driven code from the start--not as a hack to be added later. As a final sales pitch, in the initial application for which SWIG was developed, code reliability and flexibility has increased substantially while code size has decreased by more than 25%. I believe this is a good thing.<p>
<a name="n10"></a><h2> Automatic documentation generation</h2>
SWIG makes it very easy to build large interactive C programs, but it can sometimes be hard to remember what C functions are available and how they are called in the scripting language interface. To address this problem, SWIG automatically generates a documentation file in a number of different formats. C comments can be used to provide additional descriptions of each function, and documentation can be grouped into a hierarchy of sections and subsections. The documentation file is intended to provide a reasonable description of the scripting language interface. While it's no competition for a full-blown C code documentation generator, the documentation system can do a reasonable job of documenting an interface.<p>
<a name="n11"></a><h2> Summary</h2>
At this point, you know about 95% of everything you need to know to start using SWIG. First, functions and variables are specified using ANSI C, C++, or Objective-C syntax. These may appear in a separate interface file or you can use a C source file (if it is sufficiently clean). SWIG requires no modifications to existing C code so it's easy to get started. To build a module, use the <tt>swig</tt> command with an appropriate target language option. This generates a C file that you need to compile with the rest of your code and you're ready to go. <p>
<p>
I don't consider there to be a "right" or "wrong" way to use SWIG, although I personally use separate interface files for a variety of reasons :<p>
<p>
<ul>
<li>It helps keep me organized.
<li>It's usually not necessary to wrap every single C function in a program.
<li>SWIG provides a number of directives that I tend to use alot.
<li>Having a special interface file makes it clear where the scripting language interface is defined. If you decide to change something in the interface, it's easy to track down if you have special file.
</ul>
<p>
Of course, your mileage may vary. <p>
<a name="n12"></a><h2> SWIG for Windows and Macintosh</h2>
SWIG has been primarily developed and designed to work with Unix-based applications. However, most of the scripting languages supported by SWIG are available on a variety of other platforms including Windows 95/NT and Macintosh. SWIG generated code is mostly compatible with these versions (and SWIG itself can now run on these platforms). <p>
<a name="n20"></a><h3> SWIG on Windows 95/NT</h3>
The Windows 95/NT port of SWIG (provided by Kevin Butler), is a straightforward translation of the Unix version. At this time it is only known to work with Microsoft Visual C++ 4.x, but there is nothing to prevent its use with other compilers (at least not that I'm aware of). SWIG should be invoked from the MS-DOS prompt in exactly the same manner as in Unix. SWIG can also be used in NMAKE build files and added to Visual C++ projects under the custom build setting. As of this writing, SWIG is known to work with Windows versions of Tcl/Tk, Python, and Perl (including the ActiveWare Perl for Win32 port). SWIG makes extensive use of long filenames, so it is unlikely that the SWIG compiler will operate correctly under Windows 3.1 or DOS (the wrapper code generated by SWIG may compile however).<p>
<a name="n21"></a><h3> SWIG on the Power Macintosh</h3>
A Macintosh port of SWIG is also available, but is highly experimental at this time. It only works on PowerPC based Macintosh systems running System 7 or later. Modules can be compiled with the Metrowerks Code Warrior compiler, but support for other compilers is unknown. Due to the lack of command line options on the Mac, SWIG has been packaged in a Tcl/Tk interface that allows various settings and command line options to be specified with a graphical user interface. Underneath the hood, SWIG is identical to the Unix/Windows version. It recognizes the same set of options and generates identical code. Any SWIG command line option listed in this manual can be given to the Mac version under "Advanced Options" shown in the figure. At this writing, SWIG is only known to support Mac versions of Tcl/Tk. Work on Macintosh versions of Python and Perl is underway.<p>
<p>Click <a href="ch1.2.png">here</a> to see a Macintosh screen shot.<p>
<p>
<p>
<a name="n22"></a><h3> Cross platform woes</h3>
While SWIG and various freely available scripting languages are supported on different platforms, developing cross platform applications with these tools is still immature and filled with pitfalls. In fact, it's probably only recommended for true masochists. Despite this, it's interesting to think about using freely available tools to provide common interfaces to C/C++ code. I believe that SWIG may help, but it is by no means a cross-platform solution by itself. <p>
<a name="n23"></a><h3> How to survive this manual</h3>
This manual was written to support the Unix version of SWIG. However, all of the main concepts and usage of SWIG also apply to the Windows and Macintosh versions. You should be forewarned that most of the examples are Unix-centric and may not compile correctly on other machines. However, most of the examples provided with the SWIG distribution have now been tested under both Unix and Windows-NT. When applicable, I will try to point out incompatibilities, but make no promises... <p>
<p>
<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:46:50 1997</address>
</body>
</html>

View file

@ -0,0 +1,225 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Multiple Files and the SWIG Library</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1>4 Multiple files and the SWIG library</h1><p><ul>
<li> <a href="#n1">The %include directive</a>
<li> <a href="#n2">The %extern directive</a>
<li> <a href="#n3">The %import directive</a>
<li> <a href="#n4">Including files on the command line</a>
<li> <a href="#n5">The SWIG library</a>
<li> <a href="#n6">Library example</a>
<li> <a href="#n7">Creating Library Files</a>
<li> <a href="#n8">Working with library files</a>
<li> <a href="#n9">Static initialization of multiple modules</a>
<li> <a href="#n10">More about the SWIG library</a>
</ul>
For increased modularity and convenience, it is usually useful to break an interface specification up into multiple files or modules. SWIG provides a number of features for doing just this.<p>
<a name="n1"></a><h2> The %include directive</h2>
The <tt>%include</tt> directive inserts code from another file into the current interface file. It is primarily used to build a package from a collection of smaller modules. For example :<p>
<p>
<blockquote><pre>// File : interface.i
%module package
%include equations.i
%include graphics.i
%include fileio.i
%include data.i
%include network.c
%include "../Include/user.h"
</pre></blockquote>
<p>
When used in this manner, SWIG will create a single wrapper file containing all of the included functions. <p>
<p>
The <tt>%include</tt> directive can process SWIG interface files, C header files, and C source files (provided they are sufficiently clean). When processing a C source file, SWIG will automatically declare all functions it finds as "extern". Thus, use of a header file may not be required in this case.<p>
<a name="n2"></a><h2> The %extern directive</h2>
The <tt>%extern</tt> directive is like <tt>%include</tt> except that it only scans a file for type and class information. It does not actually wrap anything found in the input file. This directive is primarily used for handling class hierarchies and multiple modules. For example :<p>
<p>
<blockquote><pre>%module derived
%extern baseclass.h // Grab definition of a base class
// Now wrap a derived class
class Derived : public BaseClass {
public:
...
};
</pre></blockquote>
This interface file would grab the member functions and data from a baseclass, but only use them in the specification of a derived class. <tt>%extern</tt> processing of files is also useful for picking up common typedefs and definitions in a large package.<p>
<a name="n3"></a><h2> The %import directive</h2>
The <tt>%extern</tt> directive is used to gather declarations from files that you don't want to wrap into an interface. Unfornunately, the exact role of these files is not always clear. They could just contain definitions, or they might correspond to an entrirely different SWIG module. The <tt>%import</tt> directive is a stronger version of <tt>%extern</tt> that tells SWIG that all of the declarations in the file are indeed, in an entirely different module. This information may affect the code generated by various language modules since they will have a better idea of where things are defined and how they are to be used.<p>
<a name="n4"></a><h2> Including files on the command line</h2>
Much like the C or C++ compiler, SWIG can also include library files on the command line using the <tt>-l</tt> option as shown<p>
<p>
<blockquote><pre>
# Include a library file at compile time
% swig -tcl -lwish.i interface.i
</pre></blockquote>
This approach turns out to be particularly useful for debugging and building extensions to different kinds of languages. When libraries are specified in this manner, they are included after all of the declarations in <tt>interface.i</tt> have been wrapped. Thus, this does not work if you are trying to include common declarations, typemaps, and other files.<p>
<a name="n5"></a><h2> The SWIG library </h2>
SWIG comes with a library of functions that can be used to build up more complex interfaces. As you build up a collection of modules, you may also find yourself with a large number of interface files. While the <tt>%include</tt> directive can be used to insert files, it also searches the files installed in the SWIG library (think of this as the SWIG equivalent of the C library). When you use <tt>%include</tt>, SWIG will search for the file in the following order :<p>
<p>
<ul>
<li>The current directory
<li>Directories specified with the -I option
<li>.<tt>/swig_lib</tt>
<li><tt>/usr/local/lib/swig_lib</tt> (or wherever you installed SWIG)
</ul>
<p>
Within each directory, you can also create subdirectories for each target language. If found, SWIG will search these directories first, allowing the creation of language-specific implementations of a particular library file.<p>
<p>
You can override the location of the SWIG library by setting the <tt>SWIG_LIB</tt> environment variable.<p>
<a name="n6"></a><h2> Library example</h2>
The SWIG library is really a repository of "useful modules" that can be used to build better interfaces. To use a library file, you can simply use the <tt>%include </tt>directive with the name of a library file. For example :<p>
<p>
<blockquote><pre>%module example
%include pointer.i // Grab the SWIG pointer library
// a+b --&gt; c
extern double add(double a, double b, double *c);
</pre></blockquote>
In this example, we are including the SWIG pointer library that adds functions for manipulating C pointers. These added functions become part of your module that can be used as needed. For example, we can write a Tcl script like this that involves both the <tt>add()</tt> function and two functions from the <tt>pointer.i</tt> library :<p>
<p>
<blockquote><pre>set c [ptrcreate double 0] ;# Create a double * for holding the result
add 4 3.5 $c ;# Call our C function
puts [ptrvalue $c] ;# Print out the result
</pre></blockquote>
<a name="n7"></a><h2> Creating Library Files</h2>
It is easy to create your own library files. To illustrate the process, we consider two different library files--one to build a new <tt>tclsh</tt> program, and one to add a few memory management functions.<p>
<a name="n11"></a><h3> tclsh.i</h3>
To build a new <tt>tclsh</tt> application, you need to supply a <tt>Tcl_AppInit()</tt> function. This can be done using the following SWIG interface file (simplified somewhat for clarity) :<p>
<blockquote><pre>
// File : tclsh.i
%{
#if TCL_MAJOR_VERSION == 7 &amp;&amp; TCL_MINOR_VERSION &gt;= 4
int main(int argc, char **argv) {
Tcl_Main(argc, argv, Tcl_AppInit);
return(0);
}
#else
extern int main();
#endif
int Tcl_AppInit(Tcl_Interp *interp){
int SWIG_init(Tcl_Interp *);
if (Tcl_Init(interp) == TCL_ERROR)
return TCL_ERROR;
/* Now initialize our functions */
if (SWIG_init(interp) == TCL_ERROR)
return TCL_ERROR;
return TCL_OK;
}
%}
</pre></blockquote>
In this case, the entire file consists of a single code block. This code will be inserted directly into the resulting wrapper file, providing us with the needed <tt>Tcl_AppInit()</tt> function.<p>
<a name="n12"></a><h3> malloc.i</h3>
Now suppose we wanted to write a file <tt>malloc.i</tt> that added a few memory management functions. We could do the following :<p>
<p>
<blockquote><pre>// File : malloc.i
%{
#include &lt;malloc.h&gt;
%}
%typedef unsigned int size_t
void *malloc(size_t nbytes);
void *realloc(void *ptr, size_t nbytes);
void free(void *);
</pre></blockquote>
<p>
In this case, we have a general purpose library that could be used whenever we needed access to the <tt>malloc()</tt> functions. Since this interface file is language independent, we can use it anywhere.<p>
<a name="n13"></a><h3> Placing the files in the library</h3>
While both of our examples are SWIG interface files, they are quite different in functionality since <tt>tclsh.i</tt> would only work with Tcl while <tt>malloc.i</tt> would work with any of the target languages. Thus, we should put these files into the SWIG library as follows :<p>
<blockquote><pre><tt>
./swig_lib/malloc.i
./swig_lib/tcl/tclsh.i
</tt></pre></blockquote>
<p>
When used in other interface files, this allows us to use <tt>malloc.i</tt> with any target language while <tt>tclsh.i</tt> will only be accessible if creating for wrappers for Tcl (ie. when creating a Perl5 module, SWIG will not look in the <tt>tcl</tt> subdirectory.<p>
<p>
It should be noted that language specific libraries can mask general libraries. For example, if you wanted to make a Perl specific modification to <tt>malloc.i</tt>, you could make a special version and call it <tt>./swig_lib/perl5/malloc.i</tt>. When using Perl, you'd get this version, while all other target languages would use the general purpose version.<p>
<a name="n8"></a><h2> Working with library files</h2>
There are a variety of additional methods for working with files in the SWIG library described next.<p>
<a name="n14"></a><h3> Wrapping a library file</h3>
If you would like to wrap a file in the SWIG library, simply give SWIG the name of the appropriate library file on the command line. For example :<p>
<p>
<blockquote><pre>unix &gt; swig -python pointer.i
</pre></blockquote>
If the file <tt>pointer.i</tt> is not in the current directory, SWIG will look it up in the library, generate wrapper code, and place the output in the current directory. This technique can be used to quickly make a module out of a library file regardless of where you are working.<p>
<a name="n15"></a><h3> Checking out library files</h3>
At times, it is useful to check a file out of the library and copy it into the working directory. This allows you to modify the file or to simply retrieve useful files. To check a file out of the library, run SWIG as follows :<p>
<p>
<blockquote><pre>unix &gt; swig -co -python array.i
array.i checked out from the SWIG library
unix &gt;
</pre></blockquote>
The library file will be placed in the current directory unless a file with the same name already exists (in which case nothing is done). <p>
<p>
The SWIG library is not restricted to interface files. Suppose you had a cool Perl script that you liked to use alot. You could place this in the SWIG library. Now whenever you wanted to use it, you could retrieve it by issuing :<p>
<p>
<blockquote><pre>unix &gt; swig -perl5 -co myscript.pl
myscript.pl checked out from the SWIG library
</pre></blockquote>
Support files can also be checked out within an interface file using the <tt>%checkout</tt> directive.<p>
<p>
<blockquote><pre>%checkout myscript.pl
</pre></blockquote>
This will attempt to copy the file <tt>myscript.pl</tt> to the current directory when SWIG is run. If the file already exists, nothing is done.<p>
<a name="n16"></a><h3> The world's fastest way to write a Makefile</h3>
Since the SWIG library is not restricted to SWIG interface files, it can be used to hold other kinds of useful files. For example, if you need a quick Makefile for building Tcl extensions, type the following:<p>
<blockquote><pre>
unix&gt; swig -tcl -co Makefile
Makefile checked out from the SWIG library
</pre></blockquote>
During installation, SWIG creates a collection of preconfigured Makefiles for various scripting languages. If you need to make a new module, just check out one of these Makefiles, make a few changes, and you should be ready to compile and extension for your system.<p>
<a name="n17"></a><h3> Checking in library files</h3>
It is also possible to check files into the SWIG library. If you've made an interesting interface that you would like to keep around, simply type :<p>
<p>
<blockquote><pre>unix &gt; swig -ci -python cool.i
</pre></blockquote>
and the file `<tt>cool.i</tt>' will be placed in the Python section of the library. If your interface file is general purpose, you can install it into the general library as follows :<p>
<p>
<blockquote><pre>unix &gt; swig -ci ../cool.i
</pre></blockquote>
When files are checked in, they are placed into the directory defined by the <tt>SWIG_LIB</tt> variable that was used during SWIG compilation or the <tt>SWIG_LIB</tt> environment variable (if set). If you don't know what the value of this variable is, type the following to display its location.<p>
<p>
<blockquote><pre>unix &gt; swig -swiglib
/usr/local/lib/swig_lib
unix &gt;
</pre></blockquote>
<p>
In order to check files into the library, you must have write permission on the library directory. For this reason, one of the primary uses for the <tt>-ci</tt> option is to provide a simple mechanism for installing SWIG extensions. If these extensions need to install library files, that can be done by simply running SWIG.<p>
<a name="n9"></a><h2> Static initialization of multiple modules</h2>
When using static linking, some language modules allow multiple modules to be initialized as follows :<p>
<p>
<blockquote><pre>%module package, equations, graphics, fileio, data, network, user
... More declarations ...
</pre></blockquote>
The module list can contain SWIG generated modules or third-party applications. Refer to the appropriate language chapter for a detailed description of this feature.<p>
<a name="n10"></a><h2> More about the SWIG library</h2>
Full documentation about the SWIG library is included in the SWIG source distribution. In fact, the documentation is automatically generated by SWIG, which leads us to the next section...<p>
<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:46:54 1997</address>
</body>
</html>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,83 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Preface</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1> Preface</h1><p><ul>
<li> <a href="#n1">Introduction</a>
<li> <a href="#n2">SWIG resources</a>
<li> <a href="#n3">About this manual</a>
<li> <a href="#n4">Credits</a>
<li> <a href="#n5">What's new?</a>
<li> <a href="#n6">Bug reports</a>
<li> <a href="#n7">SWIG is free</a>
</ul>
<a name="n1"></a><h2> Introduction</h2>
SWIG is a tool for solving problems.<p>
<p>
More specifically, SWIG is a simple tool for building interactive C, C++, or Objective-C programs with common scripting languages such as Tcl, Perl, and Python. Of course, more importantly, SWIG is a tool for making C programming more enjoyable and promoting laziness (an essential feature). SWIG is not part of an overgrown software engineering project, an attempt to build some sort of monolithic programming environment, or an attempt to force everyone to rewrite all of their code (ie. code reuse). In fact, none of these things have ever been a priority.<p>
<p>
SWIG was originally developed in the Theoretical Physics Division at Los Alamos National Laboratory for building interfaces to large materials science research simulations being run on the Connection Machine 5 supercomputer. In this environment, we were faced with the problems of working with huge amounts of data, complicated machines, and constantly changing code. As scientists, we needed a mechanism for building interactive programs that was extremely easy to use, could keep pace with code that was constantly changing, and didn't get in the way of the real problems that were being solved. Mainly, we just wanted to "cut the crap" and work on the real problems at hand.<p>
<p>
While SWIG was originally developed for scientific applications, it has become a general purpose tool that is being used in an increasing variety of other computing applications--in fact almost anything where C programming is involved. Some of the application areas that I am aware of include scientific applications, visualization, databases, semiconductor CAD, remote sensing and distributed objects. Development has been pragmatic in nature--features have been added to address interesting problems as they arise. Most of the really cool stuff has been contributed or suggested by SWIG's users. There has never really been a "grand" design to SWIG other than the goal of creating a practical programming tool that could be used in other applications.<p>
<a name="n2"></a><h2> SWIG resources</h2>
The official location of SWIG related material is<p>
<blockquote><pre>
http://www.cs.utah.edu/~beazley/SWIG
</pre></blockquote>
<p>
This site contains the latest version of the software, users guide, and information regarding bugs, installation problems, and implementation tricks. The latest version of the software and related files are also available via anonymous ftp at <p>
<p>
<blockquote><pre>ftp://ftp.cs.utah.edu/pub/beazley/SWIG
</pre></blockquote>
<p>
You can also subscribe to the SWIG mailing list by sending a message with the text "subscribe swig" to <p>
<p>
<blockquote><pre>majordomo@cs.utah.edu
</pre></blockquote>
<p>
The mailing list often discusses some of the more technical aspects of SWIG along with information about beta releases and future work.<p>
<a name="n3"></a><h2> About this manual</h2>
This manual has been written in parallel with the development of SWIG because I hate black boxes and I don't like using software that is poorly documented. This manual attempts to describe all aspects of SWIG and how it can be used to solve interesting problems. Don't let the size scare you, SWIG can be quite easy to use. However, covering automatic code generation for four different scripting languages takes abit of explanation. SWIG can do quite a few interesting things that might not be so obvious so I hope that the manual can shed some light on many of these issues. The manual also serves as a general reference describing many of SWIG's implementation issues (I use the manual quite regularly myself).<p>
<a name="n8"></a><h3> Prerequisites</h3>
This manual assumes that you are interested in writing interactive C programs and that you have at least heard of scripting languages such as Tcl, Python, and Perl. A detailed knowledge of these scripting languages is not required although some familiarity certainly won't hurt. No prior experience with building C extensions to these languages is required---after all, this is what SWIG allows you to do automatically. However, I do assume that you are reasonably familiar with the use of compilers, linkers, and makefiles since making scripting language extensions is somewhat more complicated than writing a normal C program (although not significantly more complex).<p>
<a name="n9"></a><h3> Organization of this manual</h3>
The first few chapters of this manual describe SWIG in general and provide an overview of its capabilities. The remaining chapters are devoted to specific SWIG language modules and are self contained. Thus, if you are using SWIG to build Python interfaces, you can skip right to that chapter and find just about everything you need to know. So, in a sense, you are really getting 3 or 4 manuals in one.<p>
<a name="n10"></a><h3> How to avoid reading the manual</h3>
If you hate reading manuals, glance at the "Introduction" which contains a few simple examples and the overall philosophy. These examples contain about 95% of everything you need to know to use SWIG. After that, simply use the language-specific chapters for reference. The SWIG distribution also comes with a large directory of examples that illustrate how to do most kinds of things. <p>
<a name="n4"></a><h2> Credits</h2>
This work would certainly not be possible without the support of many people. I would like to acknowledge Peter Lomdahl, Brad Holian, Shujia Zhou, Niels Jensen, and Tim Germann at Los Alamos National Laboratory for allowing me to pursue this project and for being the first users. Patrick Tullmann at the University of Utah suggested the idea of automatic documentation generation. John Schmidt and Kurtis Bleeker at the University of Utah tested out the early versions. I'd also like to acknowledge Chris Johnson and the Scientific Computing and Imaging Group at the University of Utah for their continued support. John Buckman, Larry Virden, and Tom Schwaller provided valuable input on the first releases and improving the portability of SWIG. David Fletcher and Gary Holt have provided a great deal of input on improving SWIG's Perl5 implementation. I'd also like to thank Kevin Butler for his valuable input and contribution of a Windows NT port. Finally, I'd like to acknowledge all of the users who have braved the first few releases and have been instrumental in suggesting way to make SWIG more fun to use than I ever imagined possible.<p>
<a name="n5"></a><h2> What's new?</h2>
The following significant features are new in version 1.1<p>
<p>
<ul>
<li>Support for typemaps (a mechanism for customizing SWIG).
<li>Multiple inheritance.
<li>Default/optional arguments.
<li>Perl5 shadow classes.
<li>Tcl8.0 module (uses the native Tcl8 object interface).
<li>An entirely new documentation system.
<li>Limited support for nested structures.
<li>New object oriented Tcl interface.
<li>User defined exception handling.
<li>New directives for better library support (%inline, %extern %import)
<li>Objective-C support.
<li>Support for Windows-NT and Macintosh.
<li>Lots of minor bug fixes to almost everything.
</ul>
<p>
This release should be backwards compatible with interface files generated for SWIG 1.0. However, many things have changed in the SWIG C++ API so special purpose SWIG C++ extensions written for 1.0 will need to be modified.<p>
<a name="n6"></a><h2> Bug reports</h2>
While every attempt has been made to make SWIG bug-free, occasionally bugs will arrise. To report a bug, send mail to the SWIG mailing list at <tt>swig@cs.utah.edu</tt>. In your message, be as specific as possible, including (if applicable), error messages, tracebacks (if a core dump occurred), corresponding portions of the SWIG interface file used, and any important pieces of the SWIG generated wrapper code. I attempt to respond to all bug reports, but I can only fix bugs if I know about them.<p>
<a name="n7"></a><h2> SWIG is free</h2>
SWIG is a completely free package that you can use in any manner that you wish, including modification, redistribution, and use in commercial products. The only restriction on its use is that redistributions of the SWIG compiler should reproduce the SWIG copyright notice in the supporting documentation. The code generated by SWIG can be redistributed in any manner. On a more personal note, if you've used SWIG to make something cool, I'd like to find out more about it so that I can make SWIG better (and to satisfy my curiosity).<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:46:49 1997</address>
</body>
</html>

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,255 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Scripting Languages</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1>2 Scripting Languages</h1><p><ul>
<li> <a href="#n1">The two language view of the world</a>
<li> <a href="#n2">How does a scripting language talk to C?</a>
<li> <a href="#n3">Building scripting language extensions</a>
<li> <a href="#n4">Shared libraries and dynamic loading</a>
</ul>
SWIG is all about using scripting languages with C/C++ to make flexible applications. This chapter provides a brief overview of several concepts and important aspects of this interface. Many of SWIG's potential users may not have considered using a scripting language before, so I hope that this chapter can provide a little motivation.<p>
<a name="n1"></a><h2> The two language view of the world</h2>
By developing SWIG, I am trying to build systems that are loosely structured as follows :<p>
<p><center><img src="ch2.1.png"></center><p>
<p>
<p>
A real application might look more like this :<p>
<p><center><img src="ch2.2.png"></center><p>
<p>
In either case, we are interested in controlling a C/C++ program with a scripting language interface. Our interface may be for a small group of functions or a large collection of C libraries for performing a variety of tasks. In this model, C functions are turned into commands. To control the program, the user now types these commands or writes scripts to perform a particular operation. If you have used commercial packages such as MATLAB or IDL, it is a very similar model--you execute commands and write scripts, yet most of the underlying functionality is still written in C or Fortran for performance.<p>
<p>
The two-language model of computing is extremely powerful because it exploits the strengths of each language. C/C++ can be used for maximal performance and complicated systems programming tasks. Scripting languages can be used for rapid prototyping, interactive debugging, scripting, and access to high-level data structures such as lists, arrays, and hash tables.<p>
<a name="n5"></a><h3> Will scripting languages make my C program inefficient?</h3>
One of the criticisms of scripting languages is that they are interpreted and slow. No doubt about it, a scripting language will always run much slower than C. However, if you are using a scripting language to control a big C program, most of your functionality is still written in C and still fast. Thus, there is really no difference between writing the following in C <p>
<p>
<blockquote><pre>for (i = 0; i &lt; 1000; i++) {
call a bunch of C functions to do something
}
</pre></blockquote>
<p>
or writing the same thing in Python :<p>
<p>
<blockquote><pre>for i in range(0,1000):
call a bunch of C functions to do something
</pre></blockquote>
Most of the time is still spent in the underlying C functions. Of course, you wouldn't want to write the inner loop of a matrix multiply in a scripting language, but you already knew this. It is also worth noting that reimplementing certain operations in C might not lead to better performance. For example, Perl is highly optimized for text-processing operations. Most of these operations are already implemented in C (underneath the hood) so in certain cases, using a scripting language may actually be faster than an equivalent implementation in C. <p>
<a name="n6"></a><h3> Will adding a scripting language to my C program make it unmanagable?</h3>
A fear among some users is that by adding a second language, you will end up with a package that is hard to maintain and use. I believe that there are two answers to this question. If you find yourself modifying the C code to fit it into a specific scripting language, then it will be difficult to maintain. By doing this, you will lock yourself into a particular language. If that language changes or disappears off the face of the earth, then you will be left with serious maintenance problems. On the flip side of the coin, a non-invasive tool like SWIG can build interfaces without requiring language-specific modifications to the underlying C code. If the scripting language changes, it is easy to update the resulting interface. If you decide that you want to scrap the whole interface scheme and try something else, you still have a clean set of C libraries<p>
<p>
My personal experience has been that adding a scripting language to a C program makes the C program more managable! You are encouraged to think about how your C program is structured and how you want things to work. In every program in which I have added a scripting interface, the C code has actually decreased in size, improved in reliability, become easier to maintain, while becoming more functional and flexible than before.<p>
<a name="n2"></a><h2> How does a scripting language talk to C?</h2>
Scripting languages are built around a small parser that reads and executes statements on the fly as your program runs. Within the parser, there is a mechanism for executing commands or accessing variables. However, in order to access C functions and variables, it is necessary to tell the parser additional information such as the name of the function, what kind of arguments does it take, and what to do when it is called. Unfortunately, this process can be extremely tedious and technical. SWIG automates the process and allows you to forget about it. In any case, it's probably a good idea to know what's going on under the hood.<p>
<a name="n7"></a><h3> Wrapper functions</h3>
Suppose you have an ordinary C function like this :<p>
<p>
<blockquote><pre>int fact(int n) {
if (n &lt;= 1) return 1;
else return n*fact(n-1);
}
</pre></blockquote>
<p>
In order to access this function from a scripting language, it is necessary to write a special "wrapper" function that serves as the glue between the scripting language and the underlying C function. A wrapper function must do three things :<p>
<p>
<ul>
<li>Gather function arguments and make sure they are valid.
<li>Call the C function.
<li>Convert the return value into a form recognized by the scripting language.
</ul>
<p>
As an example, the Tcl wrapper function for the <tt>fact()</tt> function above example might look like the following : <p>
<blockquote><pre>
int wrap_fact(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[]) {
int _result;
int _arg0;
if (argc != 2) {
interp-&gt;result = "wrong # args";
return TCL_ERROR;
}
_arg0 = atoi(argv[1]);
_result = fact(_arg0);
sprintf(interp-&gt;result,"%d", _result);
return TCL_OK;
}
</pre></blockquote>
Once we have created a wrapper function, the final step is to tell the scripting language about our new function. This is usually done in an initialization function called by the language when our module is loaded. For example, adding the above function to the Tcl interpreter would require code like the following :<p>
<p>
<blockquote><pre>int Wrap_Init(Tcl_Interp *interp) {
Tcl_CreateCommand(interp, "fact", wrap_fact, (ClientData) NULL,
(Tcl_CmdDeleteProc *) NULL);
return TCL_OK;
}
</pre></blockquote>
<p>
When executed, Tcl will now have a new command called "<tt>fact</tt>" that you can use like any other Tcl command.<p>
<p>
While the process of adding a new function to Tcl has been illustrated, the procedure is almost identical for Perl and Python. Both require special wrappers to be written and both need additional initialization code. Only the specific details are different.<p>
<a name="n8"></a><h3> Variable linking</h3>
Variable linking is a slightly more difficult problem. The idea here is to map a C/C++ global variable into a variable in the scripting language (we are "linking" a variable in the scripting language to a C variable). For example, if you have the following variable:<p>
<p>
<blockquote><pre>double My_variable = 3.5;
</pre></blockquote>
<p>
It would be nice to be able to access it from a script as follows (shown for Perl):<p>
<p>
<blockquote><pre>$a = $My_variable * 2.3;
</pre></blockquote>
<p>
Unfortunately, the process of linking variables is somewhat problematic and not supported equally in all scripting languages. There are two primary methods for approaching this problem:<p>
<p>
<ul>
<li>Direct access. Tcl provides a mechanism for directly accessing C <tt>int</tt>, <tt>double</tt>, and <tt>char</tt> * datatypes as Tcl variables. Whenever these variables are used in a Tcl script, the interpreter will directly access the corresponding C variable. In order for this to work, one must first "register" the C variables with the Tcl interpreter. While this approach is easy to support it is also somewhat problematic. Not all C datatypes are supported, and having Tcl directly manipulate your variables in its native representation could be potentially dangerous.
<li>Access through function calls. Languages such as Perl and Python can access global variables using a function call mechanism. Rather than allowing direct access, the idea is to provide a pair of set/get functions that set or get the value of a particular variable. In many cases, this mechanism may be completely hidden. For example, it is possible to create a magical Perl variable that looks and feels just like a normal Perl variable, but is really mapped into a C variable via a pair of set/get functions. The advantage of this approach is that it is possible to support almost all C datatypes. The disadvantage is that it introduces alot of complexity to the wrapper code as it is now necessary to write a pair of C functions for every single global variable.
</ul>
<p>
SWIG supports both styles of variable linking although the latter is more common. In some cases, a hybrid approach is taken (for example, the Tcl module will create a pair of set/get functions if it encounters a datatype that Tcl can't support). Fortunately, global variables are relatively rare when working with modular code.<p>
<a name="n9"></a><h3> Constants</h3>
Constants can easily be created by simply creating a new variable in the target language with the appropriate value. Unfortunately, this can have the undesirable side-effect of making the constant non-constant. As a result, a somewhat better (although perhaps inefficient) method of creating constants is to install them as read-only variables. SWIG tends to prefer this approach.<p>
<a name="n10"></a><h3> Structures and classes</h3>
Most scripting languages have trouble directly dealing with C structures and C++ classes. This is because the use of structures is inherently C dependent and it doesn't always map well into a scripting language environment. Many of these problems are simply due to data representation issues and differences in the way C and a scripting language might represent integers, floats, strings, and so on. Other times, the problem is deeper than that--for example, what does it mean (if anything) to try and use C++ inheritance from Perl?<p>
<p>
Dealing with objects is a tough problem that many people are looking at. Packages such as CORBA and ILU are primarily concerned with the representation of objects in a portable manner. This allows objects to be used in distributed systems, used with different languages and so on. SWIG is not concerned with the representation problem, but rather the problem of accessing and using C/C++ objects from a scripting language (in fact SWIG has even been used in conjunction with CORBA-based systems).<p>
<p>
To provide access, the simplist approach is to transform a structure into a collection of accessor functions. For example :<p>
<p>
<blockquote><pre>struct Vector {
Vector();
~Vector();
double x,y,z;
};
</pre></blockquote>
can be transformed into the following set of functions :<p>
<p>
<blockquote><pre>Vector *new_Vector();
void delete_Vector(Vector *v);
double Vector_x_get(Vector *v);
double Vector_y_get(Vector *v);
double Vector_y_get(Vector *v);
void Vector_x_set(Vector *v, double x);
void Vector_y_set(Vector *v, double y);
void Vector_z_set(Vector *v, double z);
</pre></blockquote>
When accessed in Tcl, the functions could be used as follows :<p>
<p>
<blockquote><pre>% set v [new_Vector]
% Vector_x_set $v 3.5
% Vector_y_get $v
% delete_Vector $v
% ...
</pre></blockquote>
<p>
The accessor functions provide a mechanism for accessing a real C/C++ object. Since all access occurs though these function calls, Tcl does not need to know anything about the actual representation of a <tt>Vector</tt>. This simplifies matters considerably and steps around many of the problems associated with objects--in fact, it lets the C/C++ compiler do most of the work.<p>
<a name="n11"></a><h3> Shadow classes</h3>
As it turns out, it is possible to use the low-level accessor functions above to create something known as a "shadow" class. In a nutshell, a "shadow class" is a funny kind of object that gets created in a scripting language to access a C/C++ class (or struct) in a way that looks like the original structure (that is, it "shadows" the real C++ class). Of course, in reality, it's just a slick way of accessing objects that is more natural to most programmers. For example, if you have the following C definition :<p>
<blockquote><pre>
class Vector {
public:
Vector();
~Vector();
double x,y,z;
};
</pre></blockquote>
<p>
A shadow classing mechanism would allow you to access the structure in a natural manner. For example, in Python, you might do the following,<p>
<p>
<blockquote><pre>&gt;&gt;&gt; v = Vector()
&gt;&gt;&gt; v.x = 3
&gt;&gt;&gt; v.y = 4
&gt;&gt;&gt; v.z = -13
&gt;&gt;&gt; ...
&gt;&gt;&gt; del v
</pre></blockquote>
<p>
while in Perl5, it might look like this :<p>
<blockquote><pre>
$v = new Vector;
$v-&gt;{x} = 3;
$v-&gt;{y} = 4;
$v-&gt;{z} = -13;
</pre></blockquote>
and in Tcl :<p>
<blockquote><pre>
Vector v
v configure -x 3 -y 4 -z 13
</pre></blockquote>
When shadow classes are used, two objects are at really work--one in the scripting language, and an underlying C/C++ object. Operations affect both objects equally and for all practical purposes, it appears as if you are simply manipulating a C/C++ object. However, the introduction of additional "objects" can also produce excessive overhead if working with huge numbers of objects in this manner. Despite this, shadow classes turn out to be extremely useful. The actual implementation is covered later.<p>
<a name="n3"></a><h2> Building scripting language extensions</h2>
The final step in using a scripting language with your C/C++ application is adding your extensions to the scripting language itself. Unfortunately, this almost always seems to be the most difficult part. There are two fundamental approaches for doing this. First, you can build an entirely new version of the scripting language interpreter with your extensions built into it. Alternatively, you can build a shared library and dynamically load it into the scripting language as needed. Both approachs are described below :<p>
<a name="n12"></a><h3> Static linking</h3>
With static linking you rebuild the scripting language interpreter with extensions. The process usually involves compiling a short main program that adds your customized commands to the language and starts the interpreter. You then link your program with a library to produce a new executable. When using static linking, SWIG will provide a <tt>main()</tt> program for you so you usually just have to compile as follows (shown for Tcl) :<p>
<p>
<blockquote><pre>unix &gt; swig -tcl -ltclsh.i example.i
Generating wrappers for Tcl.
unix &gt; gcc example.c example_wrap.c -I/usr/local/include \
-L/usr/local/lib -ltcl -lm -o my_tclsh
</pre></blockquote>
<tt>my_tclsh</tt> is a new executable containing the Tcl intepreter. <tt>my_tclsh</tt> will be exactly the same as tclsh except with your new commands added to it. When invoking SWIG, the <tt>-ltclsh.i</tt> option includes support code needed to rebuild the <tt>tclsh</tt> application. <p>
<p>
Virtually all machines support static linking and in some cases, it may be the only way to build an extension. The downside to static linking is that you can end up with a large executable. In a very large system, the size of the executable may be prohibitively large.<p>
<a name="n4"></a><h2> Shared libraries and dynamic loading</h2>
An alternative to static linking is to build a shared library. With this approach, you build a shared object file containing only the code related to your module (on Windows, this is the same as building a DLL). Unfortunately the process of building these modules varies on every single machine, but the procedure for a few common machines is show below :<p>
<p>
<blockquote><pre># Build a shared library for Solaris
gcc -c example.c example_wrap.c -I/usr/local/include
ld -G example.o example_wrap.o -o example.so
# Build a shared library for Irix
gcc -c example.c example_wrap.c -I/usr/local/include
ld -shared example.o example_wrap.o -o example.so
# Build a shared library for Linux
gcc -fpic -c example.c example_wrap.c -I/usr/local/include
gcc -shared example.o example_wrap.o -o example.so
</pre></blockquote>
To use your shared library, you simply use the corresponding command in the scripting language (load, import, use, etc...). This will import your module and allow you to start using it. <p>
<p>
When working with C++ codes, the process of building shared libraries is more difficult--primarily due to the fact that C++ modules may need additional code in order to operate correctly. On most machines, you can build a shared C++ module by following the above procedures, but changing the link line to the following :<p>
<p>
<blockquote><pre>c++ -shared example.o example_wrap.o -o example.so
</pre></blockquote>
<p>
The advantages to dynamic loading is that you can use modules as they are needed and they can be loaded on the fly. The disadvantage is that dynamic loading is not supported on all machines (although support is improving). The compilation process also tends to be more complicated than what might be used for a typical C/C++ program.<p>
<a name="n13"></a><h3> Linking with shared libraries</h3>
When building extensions as shared libraries, it is not uncommon for your extension to rely upon other shared libraries on your machine. In order for the extension to work, it needs to be able to find all of these libraries at run-time. Otherwise, you may get an error such as the following :<p>
<p>
<blockquote><pre>&gt;&gt;&gt; import graph
Traceback (innermost last):
File "&lt;stdin&gt;", line 1, in ?
File "/home/sci/data1/beazley/graph/graph.py", line 2, in ?
import graphc
ImportError: 1101:/home/sci/data1/beazley/bin/python: rld: Fatal Error: cannot
successfully map soname 'libgraph.so' under any of the filenames /usr/lib/libgraph.so:/
lib/libgraph.so:/lib/cmplrs/cc/libgraph.so:/usr/lib/cmplrs/cc/libgraph.so:
&gt;&gt;&gt;
</pre></blockquote>
<p>
What this error means is that the extension module created by SWIG depends upon a shared library called "<tt>libgraph.so</tt>" that the system was unable to locate. To fix this problem, there are a few approaches you can take.<p>
<p>
<ul>
<li>Set the UNIX environment variable <tt>LD_LIBRARY_PATH</tt> to the directory where shared libraries are located before running Python.
<li>Link your extension and explicitly tell the linker where the required libraries are located. Often times, this can be done with a special linker flag such as <tt>-R</tt>, <tt>-rpath</tt>, etc... This is not implemented in a standard manner so read the man pages for your linker to find out more about how to set the search path for shared libraries.
<li>Put shared libraries in the same directory as the executable. This technique is sometimes required for correct operation on non-Unix platforms.
</ul>
<p>
With a little patience and after some playing around, you can usually get things to work. Afterwards, building extensions becomes alot easier.<p>
<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:46:50 1997</address>
</body>
</html>

2048
swigweb/Doc1.1/HTML/Tcl.html Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,733 @@
<!-- Published by Quadralay WebWorks HTML Lite 1.5.1 -->
<!-- And munged by Dave's special Python script -->
<html>
<head>
<title>Pointers, Constraints, and Typemaps</title>
</head>
<body bgcolor="#ffffff">
<a name="n0"></a><h1>6 Pointers, Constraints, and Typemaps</h1><p><ul>
<li> <a href="#n1">Introduction</a>
<li> <a href="#n2">The SWIG Pointer Library</a>
<li> <a href="#n3">Introduction to typemaps</a>
<li> <a href="#n4">Managing input and output parameters</a>
<li> <a href="#n5">Applying constraints to input values</a>
<li> <a href="#n6">Writing new typemaps</a>
<li> <a href="#n7">Common typemap methods</a>
<li> <a href="#n8">Writing typemap code</a>
<li> <a href="#n9">Typemaps for handling arrays</a>
<li> <a href="#n10">Typemaps and the SWIG Library</a>
<li> <a href="#n11">Implementing constraints with typemaps</a>
<li> <a href="#n12">Typemap examples</a>
<li> <a href="#n13">How to break everything with a typemap</a>
<li> <a href="#n14">Typemaps and the future</a>
</ul>
<a name="n1"></a><h2> Introduction</h2>
For most applications, SWIG's treatment of basic datatypes and pointers is enough to build an interface. However, in certain cases, it is desirable to change SWIG's treatment of particular datatypes. For example, we may want a <tt>char ** </tt>to act like a list of strings instead of a pointer. In another instance, we may want to tell SWIG that <tt>double *result</tt> is the output value of a function. Similarly, we might want to map a datatype of <tt>float[4]</tt> into a 4 element tuple. This chapter describes advanced methods for managing pointers, arrays, and complex datatypes. It also describes how you can customize SWIG to handle new kinds of objects and datatypes.<p>
<a name="n2"></a><h2> The SWIG Pointer Library</h2>
If your interface involves C pointers, chances are you will need to work with these pointers in some way or another. The SWIG pointer library provides a collection of useful methods for manipulating pointers. To use the library, simply put the following declaration in your interface file :<p>
<p>
<blockquote><pre>%include pointer.i // Grab the SWIG pointer library
</pre></blockquote>
or run SWIG as follows :<p>
<p>
<blockquote><pre>swig -perl5 -lpointer.i interface.i
</pre></blockquote>
Doing so adds a collection of pointer manipulation functions that are described below. The functions are mainly designed to work with basic C datatypes, but can often be used with more complicated structures.<p>
<a name="n15"></a><h3> Pointer Library Functions</h3>
<b>ptrcreate(type,?value?,?nitems?)</b><p>
<dl>
<dt><dd>
<dt><dd>Creates a new object and returns a pointer to it. <tt>type</tt> is a string containing the C datatype and may be one of "<tt>int</tt>","<tt>short</tt>","<tt>long</tt>","<tt>float</tt>","<tt>double</tt>","<tt>char</tt>","<tt>char *</tt>", or "<tt>void</tt>". <tt>value</tt> is the optional initial value to be assigned to the object. <tt>nitems</tt> is an optional parameter containing the number of objects to create. By default it is 1, but specifying another value allows you to create an array of values. This function is really just a wrapper around the C <tt>malloc()</tt> function.
</dl>
<b></b><p>
<b>ptrfree(ptr)</b><p>
<dl>
<dt><dd>Destroys an object created by <tt>ptrcreate</tt>. It is generally unsafe to use this function on objects not created by <tt>ptrcreate</tt>. Calls the C <tt>free()</tt> function.
</dl>
<p>
<b>ptrvalue(ptr,?index?,?type?)</b><p>
<dl>
<dt><dd>This dereferences a pointer and returns the value that it is pointing to. <tt>index</tt> is an optional parameter that allows array access by returning the value of <tt>ptr[index]</tt>. <tt>type</tt> is an optional parameter that explicitly specifies the datatype. Since SWIG pointers are encoded with type information, the type is usually unnecessary. The <tt>type</tt> parameter provides somewhat better performance and allows you to dereference a pointer of different type however.
<dt><dd>
</dl>
<b>ptrset(ptr, value, ?index?, ?type?)</b><p>
<dl>
<dt><dd>Sets the value of the object a pointer is pointing to. <tt>value</tt> is the new value of the object. <tt>index </tt>is an optional parameter allowing array access by setting <tt>ptr[index] = value</tt>. <tt>type </tt>is an optional parameter that explicitly specifies the datatype as described above.
<dt><dd>
</dl>
<b>ptrcast(ptr, newtype)</b><p>
<dl>
<dt><dd>Casts a pointer to a new datatype and returns the new value. <tt>newtype</tt> is a string containing the new datatype and may either be the "mangled" version used by SWIG (such as "<tt>_Vector_p</tt>") or the C version (such as "<tt>Vector *</tt>"). This function works with any kind of pointer value. In additional to pointers, <tt>ptr</tt> may also hold an integer value in which case the integer is turned into a pointer of given type.
<dt><dd>
</dl>
<b>ptradd(ptr, offset)</b><p>
<dl>
<dt><dd>Adds an offset to a pointer and returns a new pointer. <tt>offset</tt> is specified as the number of objects except for unknown complex datatypes in which case it is the number of bytes. For example, is <tt>ptr</tt> is a "<tt>double *</tt>", <tt>ptradd(ptr,1)</tt> will return the next double. On the other hand, if if <tt>ptr</tt> is "<tt>Vector *</tt>", then <tt>ptradd(ptr,1)</tt> will update the pointer by 1 byte.
<dt><dd>
</dl>
<b>ptrmap(type1,type2)</b><p>
<dl>
<dt><dd>This performs a "runtime typedef" and makes SWIG recognize pointers of <tt>type1</tt> and <tt>type2</tt> as equivalent. <tt>type1</tt> and <tt>type2</tt> are specified as strings. Not generally needed, but sometimes useful.
</dl>
<a name="n16"></a><h3> A simple example</h3>
Suppose you have the following C function :<p>
<blockquote><pre>
void add(double a, double b, double *result) {
*result = a + b;
}
</pre></blockquote>
To manage the result output, we can write an interface file like this :<p>
<p>
<blockquote><pre>%module example
%include pointer.i
extern void add(double a, double b, double *result);
</pre></blockquote>
Now, let's use the pointer library (shown for a few languages) :<p>
<blockquote><pre>
# Tcl
set result [ptrcreate double] ;# Create a double
add 4.5 3 $result ;# Call our C function
puts [ptrvalue $result] ;# Print out the result
ptrfree $result ;# Destroy the double
# Perl5
use example;
package example; # Functions are in example package
$result = ptrcreate("double"); # Create a double
add(4.5,3,$result); # Call C function
print ptrvalue($result),"\n"; # Print the result
ptrfree($result); # Destroy the double
# Python
import example
result = example.ptrcreate("double") # Create a double
example.add(4.5,3,result) # Call C function
print example.ptrvalue(result) # Print the result
example.ptrfree(result) # Destroy the double
</pre></blockquote>
In this case, the idea is simple--we create a pointer, pass it to our C function, and dereference it to get the result. It's essentially identical to how we would have done it in C (well, minus the function call to dereference the value).<p>
<a name="n17"></a><h3> Creating arrays</h3>
Now suppose you have a C function involving arrays :<p>
<p>
<blockquote><pre>void addv(double a[], double b[], double c[], int nitems) {
int i;
for (i = 0; i &lt; nitems; i++) {
c[i] = a[i]+b[i];
}
}
</pre></blockquote>
This is also easily handled by our pointer library. For example (in Python) :<p>
<p>
<blockquote><pre># Python function to turn a list into an "array"
def build_array(l):
nitems = len(l)
a = ptrcreate("double",0,nitems)
i = 0
for item in l:
ptrset(a,item,i)
i = i + 1
return a
# Python function to turn an array into list
def build_list(a,nitems):
l = []
for i in range(0,nitems):
l.append(ptrvalue(a,i))
return l
# Now use our functions
a = listtoarray([0.0,-2.0,3.0,9.0])
b = build_array([-2.0,3.5,10.0,22.0])
c = ptrcreate("double",0,4) # For return result
add(a,b,c,4) # Call our C function
result = build_list(c) # Build a python list from the result
print result
ptrfree(a)
ptrfree(b)
ptrfree(c)
</pre></blockquote>
This example may look quite inefficient on the surface (due to the translation of Python lists to and from C arrays). However, if you're working with lots of C functions, it's possible to simply pass C pointers around between them without any translation. As a result, applications can run fast--even when controlled from a scripting language. It's also worth emphasizing that the <tt>ptrcreate()</tt> function created a real C array that can be interchanged with other arrays. The <tt>ptrvalue()</tt> function can also dereference a C pointer even if it wasn't created from Python. <p>
<a name="n18"></a><h3> Packing a data structure</h3>
The pointer library can even be used to pack simple kinds of data-structures, perhaps for sending across a network, or simply for changing the value. For example, suppose you had this data structure:<p>
<p>
<blockquote><pre>struct Point {
double x,y;
int color;
};
</pre></blockquote>
<p>
You could write a Tcl function to set the fields of the structure as follows :<p>
<p>
<blockquote><pre>proc set_point { ptr x y c } {
set p [ptrcast $ptr "double *"] ;# Make a double *
ptrset $p $x ;# Set x component
set p [ptradd $p 1] ;# Update pointer
ptrset $p $y ;# Set y component
set p [ptrcast [ptradd $p 1] "int *"] ;# Update pointer and cast
ptrset $p $c ;# Set color component
}
</pre></blockquote>
<p>
This function could be used even if you didn't tell SWIG anything about the "Point" structure above. <p>
<a name="n3"></a><h2> Introduction to typemaps</h2>
Sometimes it's desirable to change SWIG behavior in some manner. For example, maybe you want to automatically translate C arrays to and from Perl lists. Or perhaps you would like a particular function argument to behave as an output parameter. Typemaps provide a mechanism for doing just this by modifying SWIG's code generator. Typemaps are new to SWIG 1.1, but it should be emphasized that they are not required to build an interface. <p>
<a name="n19"></a><h3> The idea (in a nutshell)</h3>
The idea behind typemaps is relatively simple--given the occurrence of a particular C datatype, we want to apply rules for special processing. For example, suppose we have a C function like this :<p>
<p>
<blockquote><pre>void add(double a, double b, double *result) {
*result = a + b;
}
</pre></blockquote>
<p>
It is clear to us that the result of the function is being returned in the <tt>result</tt> parameter. Unfortunately, SWIG isn't this smart--after all "result" is just like any other pointer. However, with a typemap, we can make SWIG recognize "<tt>double *result</tt>" as a special datatype and change the handling to do exactly what we want.<p>
<p>
So, despite being a common topic of discussion on the SWIG mailing list, a typemap is really just a special processing rule that is applied to a particular datatype. Each typemap relies on two essential attributes--a datatype and a name (which is optional). When trying to match parameters, SWIG looks at both attributes. Thus, special processing applied to a parameter of "<tt>double *result</tt>" will not be applied to "<tt>double *input</tt>". On the other hand, special processing defined for a datatype of "<tt>double *</tt>" could be applied to both (since it is more general). <p>
<a name="n20"></a><h3> Using some typemaps</h3>
It is easy to start using some typemaps right away. To wrap the above function, simply use the <tt>typemaps.i</tt> library file (which is part of the SWIG library) as follows :<p>
<p>
<blockquote><pre>// Simple example using typemaps
%module example
%include typemaps.i // Grab the standard typemap library
%apply double *OUTPUT { double *result };
extern void add(double a, double b, double *result);
</pre></blockquote>
The <tt>%apply</tt> directive tells SWIG that we are going to apply special processing to a datatype. The "<tt>double *OUTPUT</tt>" is the name of a rule describing how to return an output value from a "<tt>double *</tt>" (this rule is defined in the file <tt>typemaps.i</tt>). The rule gets applied to all of the datatypes listed in curly braces-- in this case "<tt>double *result</tt>".<p>
<p>
While it may sound complicated, when you compile the module and use it, you get a function that works as follows :<p>
<p>
<blockquote><pre># Perl code to call our add function
$a = add(3,4);
print $a,"\n";
7
</pre></blockquote>
Our function is much easier to use and it is no longer necessary to create a special double * object and pass it to the function. Typemaps took care of this automatically.<p>
<a name="n4"></a><h2> Managing input and output parameters</h2>
By default, when SWIG encounters a pointer, it makes no assumptions about what it is (well, other than the fact that it's a pointer). The <tt>typemaps.i</tt> library file contains a variety of methods for changing this behavior. The following methods are available in this file :<p>
<a name="n21"></a><h3> Input Methods</h3>
These methods tell SWIG that a pointer is a single input value. When used, functions will expect values instead of pointers.<p>
<blockquote><pre>
int *INPUT
short *INPUT
long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
double *INPUT
float *INPUT
</pre></blockquote>
<p>
Suppose you had a C function like this :<p>
<blockquote><pre>
double add(double *a, double *b) {
return *a+*b;
}
</pre></blockquote>
You could wrap it with SWIG as follows :<p>
<p>
<blockquote><pre>%module example
%include typemaps.i
...
extern double add(double *INPUT, double *INPUT);
</pre></blockquote>
Now, when you use your function ,it will work like this :<p>
<p>
<blockquote><pre>% set result [add 3 4]
% puts $result
7
</pre></blockquote>
<a name="n22"></a><h3> Output Methods</h3>
These methods tell SWIG that pointer is the output value of a function. When used, you do not need to supply the argument when calling the function, but multiple return values can be returned.<p>
<p>
<blockquote><pre>int *OUTPUT
short *OUTPUT
long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
double *OUTPUT
float *OUTPUT
</pre></blockquote>
These methods can be used as shown in an earlier example. For example, if you have this C function :<p>
<p>
<blockquote><pre>void add(double a, double b, double *c) {
*c = a+b;
}
</pre></blockquote>
<p>
A SWIG interface file might look like this :<p>
<p>
<blockquote><pre>%module example
%include typemaps.i
...
extern void add(double a, double b, double *OUTPUT);
</pre></blockquote>
In this case, only a single output value is returned, but this is not a restriction. For example, suppose you had a function like this :<p>
<p>
<blockquote><pre>// Returns a status code and double
int get_double(char *str, double *result);
</pre></blockquote>
When declared in SWIG as :<p>
<p>
<blockquote><pre>int get_double(char *str, double *OUTPUT);
</pre></blockquote>
The function would return a list of output values as shown for Python below :as follows :<p>
<p>
<blockquote><pre>&gt;&gt;&gt; get_double("3.1415926") # Returns both a status and value
[0, 3.1415926]
&gt;&gt;&gt;
</pre></blockquote>
<a name="n23"></a><h3> Input/Output Methods</h3>
When a pointer serves as both an input and output value you can use the following methods :<p>
<blockquote><pre>
int *BOTH
short *BOTH
long *BOTH
unsigned int *BOTH
unsigned short *BOTH
unsigned long *BOTH
double *BOTH
float *BOTH
</pre></blockquote>
A typical C function would be as follows :<p>
<p>
<blockquote><pre>void negate(double *x) {
*x = -(*x);
}
</pre></blockquote>
To make x function as both and input and output value, declare the function like this in an interface file :<p>
<p>
<blockquote><pre>%module example
%include typemaps.i
...
extern void negate(double *BOTH);
</pre></blockquote>
Now within a script, you can simply call the function normally :<p>
<p>
<blockquote><pre>$a = negate(3); # a = -3 after calling this
</pre></blockquote>
<a name="n24"></a><h3> Using different names</h3>
By explicitly using the parameter names of INPUT, OUTPUT, and BOTH in your declarations, SWIG performs different operations. If you would like to use different names, you can simply use the <tt>%apply</tt> directive. For example :<p>
<p>
<blockquote><pre>// Make double *result an output value
%apply double *OUTPUT { double *result };
// Make Int32 *in an input value
%apply int *INPUT { Int32 *in };
// Make long *x both
%apply long *BOTH {long *x};
</pre></blockquote>
<tt>%apply</tt> only renames the different type handling rules. You can use it to match up with the naming scheme used in a header file and so forth. To later clear a naming rule, the <tt>%clear</tt> directive can be used :<p>
<p>
<blockquote><pre>%clear double *result;
%clear Int32 *in, long *x;
</pre></blockquote>
<a name="n5"></a><h2> Applying constraints to input values</h2>
In addition to changing the handling of various input values, it is also possible to apply constraints. For example, maybe you want to insure that a value is positive, or that a pointer is non-NULL. This can be accomplished including the <tt>constraints.i</tt> library file (which is also based on typemaps).<p>
<a name="n25"></a><h3> Simple constraint example</h3>
The constraints library is best illustrated by the following interface file :<p>
<p>
<blockquote><pre>// Interface file with constraints
%module example
%include constraints.i
double exp(double x);
double log(double POSITIVE); // Allow only positive values
double sqrt(double NONNEGATIVE); // Non-negative values only
double inv(double NONZERO); // Non-zero values
void free(void *NONNULL); // Non-NULL pointers only
</pre></blockquote>
The behavior of this file is exactly as you would expect. If any of the arguments violate the constraint condition, a scripting language exception will be raised. As a result, it is possible to catch bad values, prevent mysterious program crashes and so on.<p>
<a name="n26"></a><h3> Constraint methods</h3>
The following constraints are currently available<p>
<blockquote><pre>
POSITIVE Any number &gt; 0 (not zero)
NEGATIVE Any number &lt; 0 (not zero)
NONNEGATIVE Any number &gt;= 0
NONPOSITIVE Any number &lt;= 0
NONZERO Nonzero number
NONNULL Non-NULL pointer (pointers only).
</pre></blockquote>
<a name="n27"></a><h3> Applying constraints to new datatypes</h3>
The constraints library only supports the built-in C datatypes, but it is easy to apply it to new datatypes using <tt>%apply</tt>. For example :<p>
<p>
<blockquote><pre>// Apply a constraint to a Real variable
%apply Number POSITIVE { Real in };
// Apply a constraint to a pointer type
%apply Pointer NONNULL { Vector * };
</pre></blockquote>
The special types of "Number" and "Pointer" can be applied to any numeric and pointer variable type respectively. To later remove a constraint, the %clear directive can be used :<p>
<p>
<blockquote><pre>%clear Real in;
%clear Vector *;
</pre></blockquote>
<a name="n6"></a><h2> Writing new typemaps</h2>
So far, we have only seen a high-level picture of typemaps and have utilized pre-existing typemaps in the SWIG library. However, it is possible to do more if you're willing to get your hands dirty and dig into the internals of SWIG and your favorite scripting language.<p>
<p>
Before diving in, first ask yourself do I really need to change SWIG's default behavior? The basic pointer model works pretty well most of the time and I encourage you to use it--after all, I wanted SWIG to be easy enough to use so that you didn't need to worry about low level details. If, after contemplating this for awhile, you've decided that you really want to change something, a word of caution is in order. Writing a typemap from scratch usually requires a detailed knowledge of the internal workings of a particular scripting language. It is also quite easy to break all of the output code generated by SWIG if you don't know what you're doing. On the plus side, once a typemap has been written it can be reused over and over again by putting it in the SWIG library (as has already been demonstrated). This section describes the basics of typemaps. Language specific information (which can be quite technical) is contained in the later chapters.<p>
<a name="n28"></a><h3> Motivations for using typemaps</h3>
Suppose you have a few C functions such as the following :<p>
<p>
<blockquote><pre>void glLightfv(GLenum light, Glenum pname, GLfloat parms[4]);
</pre></blockquote>
In this case, the third argument takes a 4 element array. If you do nothing, SWIG will convert the last argument into a pointer. When used in the scripting language, you will need to pass a "<tt>GLfloat *</tt>" object to the function to make it work. <p>
<a name="n29"></a><h3> Managing special data-types with helper functions</h3>
Helper functions provide one mechanism for dealing with odd datatypes. With a helper function, you provide additional functionality for creating and destroying objects or converting values into a useful form. These functions are usually just placed into your interface file with the rest of the functions. For example, a few helper functions to work with 4 element arrays for the above function, might look like this :<p>
<p>
<blockquote><pre>%inline %{
/* Create a new GLfloat [4] object */
GLfloat *newfv4(double x, double y, double z, double w) {
GLfloat *f = (GLfloat *) malloc(4*sizeof(GLfloat));
f[0] = x;
f[1] = y;
f[2] = z;
f[3] = w;
return f;
}
/* Destroy a GLfloat [4] object */
void delete_fv4(GLfloat *d) {
free(d);
}
%}
</pre></blockquote>
<p>
When wrapped, our helper functions will show up the interface and can be used as follows :<p>
<p>
<blockquote><pre>% set light [newfv4 0.0 0.0 0.0 1.0] # Creates a GLfloat *
% glLightfv GL_LIGHT0 GL_AMBIENT $light # Pass it to the function
...
% delete_fv4 $light # Destroy it (When done)
</pre></blockquote>
<p>
<p>
While not the most elegant approach, helper functions provide a simple mechanism for working with more complex datatypes. In most cases, they can be written without diving into SWIG's internals. Before typemap support was added to SWIG, helper functions were the only method for handling these kinds of problems. The pointer.i library file described earlier is an example of just this sort of approach. As a rule of thumb, I recommend that you try to use this approach before jumping into typemaps. <p>
<a name="n30"></a><h3> A Typemap Implementation</h3>
As we have seen, a typemap can often eliminate the need for helper functions. Without diving into the details, a more sophisticated typemap implementation of the previous example can permit you to pass an array or list of values directly into the C function like this :<p>
<p>
<blockquote><pre>% glLightfv GL_LIGHT0 GL_AMBIENT {0.0 0.0 0.0 1.0}
</pre></blockquote>
This is a more natural implementation that replaces the low-level pointer method. Now we will look into how one actually specifies a typemap.<p>
<a name="n31"></a><h3> What is a typemap?</h3>
A typemap is specified using the <tt>%typemap</tt> directive in your interface file. A simple typemap might look liks this :<p>
<p>
<blockquote><pre>%module example
%typemap(tcl,in) int {
$target = atoi($source);
printf("Received an integer : %d\n", $target);
}
int add(int a, int b);
</pre></blockquote>
<p>
In this case, we changed the processing of integers as input arguments to functions. When used in a Tcl script, we would get the following debugging information:<p>
<p>
<blockquote><pre>% set a [add 7 13]
Received an integer : 7
Received an integer : 13
</pre></blockquote>
In the typemap specification, the symbols <tt>$source</tt> and <tt>$target</tt> are holding places for C variable names that SWIG is going to use when generating wrapper code. In this example, <tt>$source</tt> would contain a Tcl string containing the input value and <tt>$target</tt> would be the C integer value that is going to be passed into the "add" function.<p>
<a name="n32"></a><h3> Creating a new typemap</h3>
A new typemap can be created as follows :<p>
<blockquote><pre>
%typemap(lang,method) Datatype {
... Conversion code ...
}
</pre></blockquote>
<tt>lang</tt> specifies the target language, <tt>method</tt> defines a particular conversion method, and <tt>Datatype</tt> gives the corresponding C datatype. The code corresponding to the typemap is enclosed in braces after the declaration. There are about a dozen different kinds of typemaps that are used within SWIG, but we will get to that shortly.<p>
<p>
A single conversion can be applied to multiple datatypes by giving a comma separated list of datatypes. For example :<p>
<p>
<blockquote><pre>%typemap(tcl,in) int, short, long, signed char {
$target = ($type) atol($source);
}
</pre></blockquote>
Here, <tt>$type</tt> will be expanded into the real datatype during code generation. Datatypes may also carry names as in<p>
<p>
<blockquote><pre>%typemap(perl5,in) char **argv {
... Turn a perl array into a char ** ...
}
</pre></blockquote>
A "named"typemap will only apply to an object that matches both the C datatype and the name. Thus the <tt>char **argv</tt> typemap will only be applied to function arguments that exactly match "<tt>char **argv</tt>". In some cases, the name may correspond to a function name (as is the case for return values).<p>
<p>
Finally, there is a shortened form of the typemap directive :<p>
<p>
<blockquote><pre>%typemap(method) Datatype {
...
}
</pre></blockquote>
When the language name is ommitted, the typemap will be applied to the current target language. This form is only recommended for typemap methods that are language independent (there are a few). It is not recommended if you are building interfaces for multiple languages.<p>
<a name="n33"></a><h3> Deleting a typemap</h3>
A typemap can be deleted by providing no conversion code. For example :<p>
<p>
<blockquote><pre>%typemap(lang,method) Datatype; // Deletes this typemap
</pre></blockquote>
<a name="n34"></a><h3> Copying a typemap</h3>
A typemap can be copied using the following declaration :<p>
<p>
<blockquote><pre>%typemap(python,out) unsigned int = int; // Copies a typemap
</pre></blockquote>
This specifies that the typemap for "<tt>unsigned int</tt>" should be the same as the "<tt>int</tt>" typemap.<p>
This is most commonly used when working with library files.<p>
<a name="n35"></a><h3> Typemap matching rules</h3>
When you specify a typemap, SWIG is going to try and match it with all future occurrences of the datatype you specify. The matching process is based upon the target language, typemap method, datatype, and optional name. Because of this, it is perfectly legal for multiple typemaps to exist for a single datatype at any given time. For example :<p>
<p>
<blockquote><pre>%typemap(tcl,in) int * {
... Convert an int * ...
}
%typemap(tcl,in) int [4] {
... Convert an int[4] ...
}
%typemap(tcl,in) int out[4] {
... Convert an out[4] ...
}
%typemap(tcl,in) int *status {
... Convert an int *status ...
}
</pre></blockquote>
These typemaps all involve the "<tt>int *</tt>" datatype in one way or another, but are all considered to be distinct. There is an extra twist to typemaps regarding the similarity between C pointers and arrays. A typemap applied to a pointer will also work for any array of the same type. On the other hand, a typemap applied to an array will only work for arrays, not pointers. Assuming that you're not completely confused at this point, the following rules are applied in order to match pointers and arrays :<p>
<p>
<ul>
<li>Named arrays
<li>Unnamed arrays
<li>Named datatypes
<li>Unnamed datatypes
</ul>
<p>
The following interface file shows how these rules are applied.<p>
<p>
<blockquote><pre>void foo1(int *); // Apply int * typemap
void foo2(int a[4]); // Apply int[4] typemap
void foo3(int out[4]); // Apply int out[4] typemap
void foo4(int *status); // Apply int *status typemap
void foo5(int a[20]); // Apply int * typemap (because int [20] is an int *)
</pre></blockquote>
Because SWIG uses a name-based approach, it is possible to attach special properties to named parameters. For example, we can make an argument of "<tt>int *OUTPUT</tt>" always be treated as an output value of a function or make a "<tt>char **argv</tt>" always accept a list of string values.<p>
<a name="n7"></a><h2> Common typemap methods</h2>
The following methods are supported by most SWIG language modules. Individual language may provide any number of other methods not listed here.<p>
<center>
<img src="ch6.table.1.png"><br>
<img src="ch6.table.2.png"><br>
</center>
<p>
Understanding how some of these methods are applied takes a little practice and better understanding of what SWIG does when it creates a wrapper function. The next few diagrams show the anatomy of a wrapper function and how the typemaps get applied. More detailed examples of typemaps can be found on the chapters for each target language.<p>
<p>
<center><img src="ch6.1.png">
<p></center>
<p>
<center><img src="ch6.2.png">
<p></center>
<a name="n8"></a><h2> Writing typemap code</h2>
The conversion code supplied to a typemap needs to follow a few conventions described here.<p>
<a name="n36"></a><h3> Scope</h3>
Typemap code is enclosed in braces when it is inserted into the resulting wrapper code (using C's block-scope). It is perfectly legal to declare local and static variables in a typemap. However, local variables will only exist in the tiny portion of code you supply. In other words, any local variables that you create in a typemap will disappear when the typemap has completed its execution.<p>
<a name="n37"></a><h3> Creating local variables</h3>
Sometimes it is necessary to declare a new local variable that exists in the scope of the entire wrapper function. This can be done by specifying a typemap with parameters as follows :<p>
<p>
<blockquote><pre>%typemap(tcl,in) int *INPUT(int temp) {
temp = atoi($source);
$target = &amp;temp;
}
</pre></blockquote>
What happens here is that <tt>temp</tt> becomes a local variable in the scope of the entire wrapper function. When we set it to a value, that values persists for the duration of the wrapper function and gets cleaned up automatically on exit. This is particularly useful when working with pointers and temporary values.<p>
<p>
It is perfectly safe to use multiple typemaps involving local variables in the same function. For example, we could declare a function as :<p>
<p>
<blockquote><pre>void foo(int *INPUT, int *INPUT, int *INPUT);
</pre></blockquote>
When this occurs, SWIG will create three different local variables named `temp'. Of course, they don't all end up having the same name---SWIG automatically performs a variable renaming operation if it detects a name-clash like this.<p>
<p>
Some typemaps do not recognize local variables (or they may simply not apply). At this time, only the "in", "argout", "default", and "ignore" typemaps use local variables.<p>
<a name="n38"></a><h3> Special variables</h3>
The following special variables may be used within a typemap conversion code :<p>
<center><img src="ch6.table.3.png"></center>
<p>
When found in the conversion code, these variables will be replaced with the correct values. Not all values are used in all typemaps. Please refer to the SWIG reference manual for the precise usage. <p>
<a name="n9"></a><h2> Typemaps for handling arrays</h2>
One of the most common uses of typemaps is providing some support for arrays. Due to the subtle differences between pointers and arrays in C, array support is somewhat limited unless you provide additional support. For example, consider the following structure appears in an interface file :<p>
<p>
<blockquote><pre>struct Person {
char name[32];
char address[64];
int id;
};
</pre></blockquote>
When SWIG is run, you will get the following warnings :<p>
<p>
<blockquote><pre>swig -python example.i
Generating wrappers for Python
example.i : Line 2. Warning. Array member will be read-only.
example.i : Line 3. Warning. Array member will be read-only.
</pre></blockquote>
<p>
These warning messages indicate that SWIG does not know how you want to set the name and address fields. As a result, you will only be able to query their value.<p>
<p>
To fix this, we could supply two typemaps in the file such as the following :<p>
<blockquote><pre>
%typemap(memberin) char [32] {
strncpy($target,$source,32);
}
%typemap(memberin) char [64] {
strncpy($target,$source,64);
}
</pre></blockquote>
The "memberin" typemap is used to set members of structures and classes. When you run the new version through SWIG, the warnings will go away and you can now set each member. It is important to note that <tt>char[32]</tt> and <tt>char[64]</tt> are different datatypes as far as SWIG typemaps are concerned. However, both typemaps can be combined as follows :<p>
<p>
<blockquote><pre>// A better typemap for char arrays
%typemap(memberin) char [ANY] {
strncpy($target,$source,$dim0);
}
</pre></blockquote>
The <tt>ANY</tt> keyword can be used in a typemap to match any array dimension. When used, the special variable <tt>$dim0</tt> will contain the real dimension of the array and can be used as shown above.<p>
<p>
Multidimensional arrays can also be handled by typemaps. For example :<p>
<p>
<blockquote><pre>// A typemap for handling any int [][] array
%typemap(memberin) int [ANY][ANY] {
int i,j;
for (i = 0; i &lt; $dim0; i++)
for (j = 0; j &lt; $dim1; j++) {
$target[i][j] = *($source+$dim1*i+j);
}
}
</pre></blockquote>
When multi-dimensional arrays are used, the symbols <tt>$dim0,</tt> <tt>$dim1</tt>,<tt> $dim2</tt>, etc... get replaced by the actual array dimensions being used. <p>
<p>
The ANY keyword can be combined with any specific dimension. For example :<p>
<p>
<blockquote><pre>%typemap(python,in) int [ANY][4] {
...
}
</pre></blockquote>
A typemap using a specific dimension always has precedence over a more general version. For example, <tt>[ANY][4]</tt> will be used before<tt>[ANY][ANY]</tt>.<p>
<a name="n10"></a><h2> Typemaps and the SWIG Library</h2>
Writing typemaps is a tricky business. For this reason, many common typemaps can be placed into a SWIG library file and reused in other modules without having to worry about nasty underlying details. To do this, you first write a file containing typemaps such as this :<p>
<p>
<blockquote><pre>// file : stdmap.i
// A file containing a variety of useful typemaps
%typemap(tcl,in) int INTEGER {
...
}
%typemap(tcl,in) double DOUBLE {
...
}
%typemap(tcl,out) int INT {
...
}
%typemap(tcl,out) double DOUBLE {
...
}
%typemap(tcl,argout) double DOUBLE {
...
}
// and so on...
</pre></blockquote>
This file may contain dozens or even hundreds of possible mappings. Now, to use this file with other modules, simply include it in other files and use the <tt>%apply</tt> directive :<p>
<p>
<blockquote><pre>// interface.i
// My interface file
%include stdmap.i // Load the typemap library
// Now grab the typemaps we want to use
%apply double DOUBLE {double};
// Rest of your declarations
</pre></blockquote>
<p>
In this case, <tt>stdmap.i</tt> contains a variety of standard mappings. The <tt>%apply</tt> directive lets us apply specific versions of these to new datatypes without knowing the underlying implementation details.<p>
<p>
To clear a typemap that has been applied, you can use the <tt>%clear</tt> directive. For example :<p>
<p>
<blockquote><pre>%clear double x; // Clears any typemaps being applied to double x
</pre></blockquote>
<a name="n11"></a><h2> Implementing constraints with typemaps</h2>
One particularly interesting application of typemaps is the implementation of argument constraints. This can be done with the "check" typemap. When used, this allows you to provide code for checking the values of function arguments. For example :<p>
<p>
<blockquote><pre>%module math
%typemap(perl5,check) double *posdouble {
if ($target &lt; 0) {
croak("Expecting a positive number");
}
}
...
double sqrt(double posdouble);
</pre></blockquote>
This provides a sanity check to your wrapper function. If a negative number is passed to this function, a Perl exception will be raised and your program terminated with an error message.<p>
<p>
This kind of checking can be particularly useful when working with pointers. For example :<p>
<p>
<blockquote><pre>%typemap(python,check) Vector * {
if ($target == 0) {
PyErr_SetString(PyExc_TypeError,"NULL Pointer not allowed");
return NULL;
}
}
</pre></blockquote>
will prevent any function involving a <tt>Vector *</tt> from accepting a NULL pointer. As a result, SWIG can often prevent a potential segmentation faults or other run-time problems by raising an exception rather than blindly passing values to the underlying C/C++ program.<p>
<a name="n12"></a><h2> Typemap examples</h2>
Typemaps are inherently language dependent so more examples appear in later chapters. The SWIG<tt> Examples </tt>directory also includes a variety of examples. Sophisticated users may gain more by examining the <tt>typemaps.i</tt> and <tt>constraints.i </tt>SWIG library files.<p>
<a name="n13"></a><h2> How to break everything with a typemap</h2>
It should be emphasized that typemaps provide a direct mechanism for modifying SWIG's output. As a result, it can be very easy to break almost everything if you don't know what you're doing. For this reason, it should be stressed that typemaps are not required in order to use SWIG with most kinds of applications. Power users, however, will probably find typemaps to be a useful tool for creating extremely powerful scripting language extensions. <p>
<a name="n14"></a><h2> Typemaps and the future</h2>
The current typemap mechanism, while new, will probably form the basis of SWIG 2.0. Rather than having code buried away inside a C++ module, it may soon be possible to redefine almost all of SWIG's code generation on the fly. Future language modules will rely upon typemaps almost exclusively. <p>
<p>
<p><hr>
<address>SWIG 1.1 - Last Modified : Mon Aug 4 10:46:56 1997</address>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB