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

« back to all changes in this revision

Viewing changes to Doc/Manual/Pike.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:16:04 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205011604-ygx904it6413k3go
Tags: 1.3.27-1ubuntu1
Resynchronise with Debian again, for the new subversion packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
<html>
3
3
<head>
4
4
<title>SWIG and Pike</title>
 
5
<link rel="stylesheet" type="text/css" href="style.css">
5
6
</head>
6
7
 
7
8
<body bgcolor="#ffffff">
8
9
<H1><a name="Pike"></a>25 SWIG and Pike</H1>
9
10
<!-- INDEX -->
 
11
<div class="sectiontoc">
10
12
<ul>
11
13
<li><a href="#Pike_nn2">Preliminaries</a>
12
14
<ul>
24
26
<li><a href="#Pike_nn12">Static Members</a>
25
27
</ul>
26
28
</ul>
 
29
</div>
27
30
<!-- INDEX -->
28
31
 
29
32
 
49
52
<H3><a name="Pike_nn3"></a>25.1.1 Running SWIG</H3>
50
53
 
51
54
 
 
55
<p>
52
56
Suppose that you defined a SWIG module such as the following:
53
 
<blockquote>
 
57
</p>
 
58
 
 
59
<div class="code">
54
60
  <pre>%module example<br><br>%{<br>#include "example.h"<br>%}<br><br>int fact(int n);<br></pre>
55
 
</blockquote>
 
61
</div>
 
62
 
 
63
<p>
56
64
To build a C extension module for Pike, run SWIG using the <tt>-pike</tt> option :
57
 
<blockquote>
 
65
</p>
 
66
 
 
67
<div class="code">
58
68
  <pre>$ <b>swig -pike example.i</b><br></pre>
59
 
</blockquote>
 
69
</div>
 
70
 
 
71
<p>
60
72
If you're building a C++ extension, be sure to add the <tt>-c++</tt> option:
61
 
<blockquote>
 
73
</p>
 
74
 
 
75
<div class="code">
62
76
  <pre>$ <b>swig -c++ -pike example.i</b><br></pre>
63
 
</blockquote>
 
77
</div>
64
78
 
65
79
<p>
66
80
This creates a single source file named <tt>example_wrap.c</tt> (or <tt>example_wrap.cxx</tt>, if you
77
91
can use the <tt>-o</tt> option:
78
92
</p>
79
93
 
80
 
<blockquote>
 
94
<div class="code">
81
95
  <pre>$ <b>swig -pike -o pseudonym.c example.i</b><br></pre>
82
 
</blockquote>
 
96
</div>
83
97
<H3><a name="Pike_nn4"></a>25.1.2 Getting the right header files</H3>
84
98
 
85
99
 
89
103
directory such as
90
104
</p>
91
105
 
92
 
<blockquote>
 
106
<div class="code">
93
107
  <pre>/usr/local/pike/7.4.10/include/pike<br></pre>
94
 
</blockquote>
 
108
</div>
95
109
 
96
110
<p>
97
111
There doesn't seem to be any way to get Pike itself to reveal the
103
117
<H3><a name="Pike_nn5"></a>25.1.3 Using your module</H3>
104
118
 
105
119
 
 
120
<p>
106
121
To use your module, simply use Pike's <tt>import</tt> statement:
 
122
</p>
107
123
 
108
 
<blockquote><pre>
 
124
<div class="code"><pre>
109
125
$ <b>pike</b>
110
126
Pike v7.4 release 10 running Hilfe v3.5 (Incremental Pike Frontend)
111
127
&gt; <b>import example;</b>
112
128
&gt; <b>fact(4);</b>
113
129
(1) Result: 24
114
 
</pre></blockquote>
 
130
</pre></div>
115
131
 
116
132
<H2><a name="Pike_nn6"></a>25.2 Basic C/C++ Mapping</H2>
117
133
 
119
135
<H3><a name="Pike_nn7"></a>25.2.1 Modules</H3>
120
136
 
121
137
 
 
138
<p>
122
139
All of the code for a given SWIG module is wrapped into a single Pike
123
140
module. Since the name of the shared library that implements your
124
141
module ultimately determines the module's name (as far as Pike is
125
142
concerned), SWIG's <tt>%module</tt> directive doesn't really have any
126
143
significance.
 
144
</p>
127
145
 
128
146
<H3><a name="Pike_nn8"></a>25.2.2 Functions</H3>
129
147
 
130
148
 
 
149
<p>
131
150
Global functions are wrapped as new Pike built-in functions. For
132
151
example,
 
152
</p>
133
153
 
134
 
<blockquote><pre>
 
154
<div class="code"><pre>
135
155
%module example
136
156
 
137
157
int fact(int n);
138
 
</pre></blockquote>
 
158
</pre></div>
139
159
 
 
160
<p>
140
161
creates a new built-in function <tt>example.fact(n)</tt> that works
141
162
exactly as you'd expect it to:
 
163
</p>
142
164
 
143
 
<blockquote><pre>
 
165
<div class="code"><pre>
144
166
&gt; <b>import example;</b>
145
167
&gt; <b>fact(4);</b>
146
168
(1) Result: 24
147
 
</pre></blockquote>
 
169
</pre></div>
148
170
 
149
171
<H3><a name="Pike_nn9"></a>25.2.3 Global variables</H3>
150
172
 
151
173
 
 
174
<p>
152
175
Global variables are currently wrapped as a pair of of functions, one to get
153
176
the current value of the variable and another to set it. For example, the
154
177
declaration
 
178
</p>
155
179
 
156
 
<blockquote><pre>
 
180
<div class="code"><pre>
157
181
%module example
158
182
 
159
183
double Foo;
160
 
</pre></blockquote>
 
184
</pre></div>
161
185
 
 
186
<p>
162
187
will result in two functions, <tt>Foo_get()</tt> and <tt>Foo_set()</tt>:
 
188
</p>
163
189
 
164
 
<blockquote><pre>
 
190
<div class="code"><pre>
165
191
&gt; <b>import example;</b>
166
192
&gt; <b>Foo_get();</b>
167
193
(1) Result: 3.000000
169
195
(2) Result: 0
170
196
&gt; <b>Foo_get();</b>
171
197
(3) Result: 3.141590
172
 
</pre></blockquote>
 
198
</pre></div>
173
199
 
174
200
<H3><a name="Pike_nn10"></a>25.2.4 Constants and enumerated types</H3>
175
201
 
176
202
 
 
203
<p>
177
204
Enumerated types in C/C++ declarations are wrapped as Pike constants,
178
205
not as Pike enums.
 
206
</p>
179
207
 
180
208
<H3><a name="Pike_nn11"></a>25.2.5 Constructors and Destructors</H3>
181
209
 
182
210
 
 
211
<p>
183
212
Constructors are wrapped as <tt>create()</tt> methods, and destructors are
184
213
wrapped as <tt>destroy()</tt> methods, for Pike classes.
 
214
</p>
185
215
 
186
216
<H3><a name="Pike_nn12"></a>25.2.6 Static Members</H3>
187
217
 
188
218
 
 
219
<p>
189
220
Since Pike doesn't support static methods or data for Pike classes, static
190
221
member functions in your C++ classes are wrapped as regular functions and
191
222
static member variables are wrapped as pairs of functions (one to get the
192
223
value of the static member variable, and another to set it). The names of
193
224
these functions are prepended with the name of the class.
194
225
For example, given this C++ class declaration:
 
226
</p>
195
227
 
196
 
<blockquote><pre>
 
228
<div class="code"><pre>
197
229
class Shape
198
230
{
199
231
public:
200
232
    static void print();
201
233
    static int nshapes;
202
234
};
203
 
</pre></blockquote>
 
235
</pre></div>
204
236
 
 
237
<p>
205
238
SWIG will generate a <tt>Shape_print()</tt> method that invokes the static
206
239
<tt>Shape::print()</tt> member function, as well as a pair of methods,
207
240
<tt>Shape_nshapes_get()</tt> and <tt>Shape_nshapes_set()</tt>, to get and set
208
241
the value of <tt>Shape::nshapes</tt>.
 
242
</p>
209
243
 
210
244
</body>
211
245
</html>