~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Doc/Manual/Modules.html

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-06 10:27:08 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206102708-t37t62i45n595w0e
Tags: 1.3.33-2ubuntu1
* Merge with Debian; remaining changes:
  - Drop support for pike.
  - Use python2.5 instead of python2.4.
  - Clean Runtime/ as well.
  - Force a few environment variables.
* debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
<!-- INDEX -->
11
11
<div class="sectiontoc">
12
12
<ul>
 
13
<li><a href="#Modules_nn1">Basics</a>
13
14
<li><a href="#Modules_nn2">The SWIG runtime code</a>
14
15
<li><a href="#external_run_time">External access to the runtime</a>
15
16
<li><a href="#Modules_nn4">A word of caution about static libraries</a>
36
37
where you want to create a collection of modules.
37
38
</p>
38
39
 
39
 
<H2><a name="Modules_nn2"></a>15.1 The SWIG runtime code</H2>
40
 
 
41
 
 
42
 
<p>
43
 
Many of SWIG's target languages generate a set of functions
44
 
commonly known as the "SWIG runtime."   These functions are 
45
 
primarily related to the runtime type system which checks pointer
46
 
types and performs other tasks such as proper casting of pointer
47
 
values in C++. As a general rule, the statically typed target languages,
48
 
such as Java, use the language's built in static type checking and
 
40
<H2><a name="Modules_nn1"></a>15.1 Basics</H2>
 
41
 
 
42
 
 
43
<p>
 
44
The basic usage case with multiple modules is when modules do not have
 
45
cross-references (ie. when wrapping multiple independent C APIs). In that case,
 
46
swig input files should just work out of the box - you simply create multiple
 
47
wrapper .cxx files, link them into your application, and insert/load each in the
 
48
scripting language runtime as you would do for the single module case.
 
49
</p>
 
50
 
 
51
<p>
 
52
A bit more complex is the case in which modules need to share information.
 
53
For example, when one module extends the class of the another by deriving from
 
54
it:
 
55
</p>
 
56
 
 
57
<div class="code"><pre>
 
58
%module base
 
59
 
 
60
%inline %{
 
61
class base {
 
62
public:
 
63
       int foo(void);
 
64
};
 
65
%}
 
66
</pre></div>
 
67
&nbsp;
 
68
<div class="code"><pre>
 
69
%module derived
 
70
 
 
71
%import "base.i"
 
72
 
 
73
%inline %{
 
74
class derived : public base {
 
75
public:
 
76
       int bar(void);
 
77
};
 
78
%}
 
79
</pre></div>
 
80
 
 
81
<p>To create the wrapper properly, module <tt>derived</tt> needs to know the
 
82
<tt>base</tt> class and that it's interface is covered in another module. The
 
83
line <tt>%import "base.i"</tt> lets SWIG know exactly that. The common mistake here is
 
84
to <tt>%import</tt> the <tt>.h</tt> file instead of the <tt>.i</tt>, which sadly won't do the trick. Another issue
 
85
to take care of is that multiple dependent wrappers should not be linked/loaded
 
86
in parallel from multiple threads as SWIG provides no locking - for more on that
 
87
issue, read on.</p>
 
88
 
 
89
<H2><a name="Modules_nn2"></a>15.2 The SWIG runtime code</H2>
 
90
 
 
91
 
 
92
<p>
 
93
Many of SWIG's target languages generate a set of functions commonly known as
 
94
the "SWIG runtime." These functions are primarily related to the runtime type
 
95
system which checks pointer types and performs other tasks such as proper
 
96
casting of pointer values in C++. As a general rule, the statically typed target
 
97
languages, such as Java, use the language's built in static type checking and
49
98
have no need for a SWIG runtime. All the dynamically typed / interpreted
50
99
languages rely on the SWIG runtime.
51
100
</p>
52
101
 
53
102
<p>
54
 
The runtime functions are private to each SWIG-generated
55
 
module.  That is, the runtime functions are declared with "static"
56
 
linkage and are visible only to the wrapper functions defined in that
57
 
module.  The only problem with this approach is that when more than one SWIG
58
 
module is used in the same application, those modules often need to
59
 
share type information.  This is especially true for C++ programs
60
 
where SWIG must collect and share information about inheritance
 
103
The runtime functions are private to each SWIG-generated module. That is, the
 
104
runtime functions are declared with "static" linkage and are visible only to the
 
105
wrapper functions defined in that module. The only problem with this approach is
 
106
that when more than one SWIG module is used in the same application, those
 
107
modules often need to share type information. This is especially true for C++
 
108
programs where SWIG must collect and share information about inheritance
61
109
relationships that cross module boundaries.
62
110
</p>
63
111
 
64
112
<p>
65
113
To solve the problem of sharing information across modules, a pointer to the
66
 
type information is stored in a global variable in the target language namespace.
67
 
During module initialization, type information is loaded into the global data
68
 
structure of type information from all modules.
69
 
</p>
70
 
 
71
 
<p>
72
 
This can present a problem with threads.  If two modules try and load at the same
73
 
time, the type information can become corrupt.  SWIG currently does not provide any
74
 
locking, and if you use threads, you must make sure that modules are loaded serially.
75
 
Be careful if you use threads and the automatic module loading that some scripting
76
 
languages provide.  One solution is to load all modules before spawning any threads.
77
 
</p>
78
 
 
79
 
<H2><a name="external_run_time"></a>15.2 External access to the runtime</H2>
 
114
type information is stored in a global variable in the target language
 
115
namespace. During module initialization, type information is loaded into the
 
116
global data structure of type information from all modules.
 
117
</p>
 
118
 
 
119
<p>
 
120
There are a few trade offs with this approach. This type information is global
 
121
across all SWIG modules loaded, and can cause type conflicts between modules
 
122
that were not designed to work together. To solve this approach, the SWIG
 
123
runtime code uses a define SWIG_TYPE_TABLE to provide a unique type table. This
 
124
behavior can be enabled when compiling the generated _wrap.cxx or _wrap.c file
 
125
by adding -DSWIG_TYPE_TABLE=myprojectname to the command line argument.
 
126
</p>
 
127
 
 
128
<p>
 
129
Then, only modules compiled with SWIG_TYPE_TABLE set to myprojectname will share
 
130
type information. So if your project has three modules, all three should be
 
131
compiled with -DSWIG_TYPE_TABLE=myprojectname, and then these three modules will
 
132
share type information. But any other project's types will not interfere or
 
133
clash with the types in your module.
 
134
</p>
 
135
 
 
136
<p>
 
137
Another issue relating to the global type table is thread safety. If two modules
 
138
try and load at the same time, the type information can become corrupt. SWIG
 
139
currently does not provide any locking, and if you use threads, you must make
 
140
sure that modules are loaded serially. Be careful if you use threads and the
 
141
automatic module loading that some scripting languages provide. One solution is
 
142
to load all modules before spawning any threads, or use SWIG_TYPE_TABLE to
 
143
separate type tables so they do not clash with each other.
 
144
</p>
 
145
 
 
146
<p>
 
147
Lastly, SWIG uses a #define SWIG_RUNTIME_VERSION, located in Lib/swigrun.swg and
 
148
near the top of every generated module. This number gets incremented when the
 
149
data structures change, so that SWIG modules generated with different versions
 
150
can peacefully coexist. So the type structures are separated by the
 
151
(SWIG_TYPE_TABLE, SWIG_RUNTIME_VERSION) pair, where by default SWIG_TYPE_TABLE
 
152
is empty. Only modules compiled with the same pair will share type information.
 
153
</p>
 
154
 
 
155
<H2><a name="external_run_time"></a>15.3 External access to the runtime</H2>
80
156
 
81
157
 
82
158
<p>As described in <a href="Typemaps.html#runtime_type_checker">The run-time type checker</a>,
86
162
to call the SWIG run-time functions from another C file, there is one header you need
87
163
to include.  To generate the header that needs to be included, run the following command:
88
164
 
89
 
<div class="code"><pre>
 
165
<div class="shell"><pre>
90
166
$ swig -python -external-runtime &lt;filename&gt;
91
167
</pre></div>
92
168
 
106
182
so that you can distribute a package that can be compiled without SWIG installed (this works
107
183
because the header file is self-contained, and does not need to link with anything).</p>
108
184
 
109
 
<H2><a name="Modules_nn4"></a>15.3 A word of caution about static libraries</H2>
 
185
<p>
 
186
This header will also use the -DSWIG_TYPE_TABLE described above, so when
 
187
compiling any code which includes the generated header file should define the
 
188
SWIG_TYPE_TABLE to be the same as the module whose types you are trying to
 
189
access.
 
190
</p>
 
191
 
 
192
<H2><a name="Modules_nn4"></a>15.4 A word of caution about static libraries</H2>
110
193
 
111
194
 
112
195
<p>
117
200
behavior. When working with dynamically loadable modules, you should try to work exclusively with shared libraries.
118
201
</p>
119
202
 
120
 
<H2><a name="Modules_nn5"></a>15.4 References</H2>
 
203
<H2><a name="Modules_nn5"></a>15.5 References</H2>
121
204
 
122
205
 
123
206
<p>
125
208
an outside reference.  John Levine's "Linkers and Loaders" is highly recommended.
126
209
</p>
127
210
 
128
 
<H2><a name="Modules_nn6"></a>15.5 Reducing the wrapper file size</H2>
 
211
<H2><a name="Modules_nn6"></a>15.6 Reducing the wrapper file size</H2>
129
212
 
130
213
 
131
214
<p>