PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/Root-branch-php-utl/SWIG/Examples/perl5/funcptr/index.html

#
HTML | 92 lines | 67 code | 25 blank | 0 comment | 0 complexity | 927c3efd7b488f2603f4b10213955815 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
 1<html>
 2<head>
 3<title>SWIG:Examples:perl5:funcptr</title>
 4</head>
 5
 6<body bgcolor="#ffffff">
 7
 8
 9<tt>SWIG/Examples/perl5/funcptr/</tt>
10<hr>
11
12<H2>Pointers to Functions</H2>
13
14<tt>$Header$</tt><br>
15
16<p>
17Okay, just what in the heck does SWIG do with a declaration like this?
18
19<blockquote>
20<pre>
21int do_op(int a, int b, int (*op)(int, int));
22</pre>
23</blockquote>
24
25Well, it creates a wrapper as usual.  Of course, that does raise some
26questions about the third argument (the pointer to a function). 
27
28<p>
29In this case, SWIG will wrap the function pointer as it does for all other
30pointers.  However, in order to actually call this function from a script,
31you will need to pass some kind of C function pointer object.  In C,
32this is easy, you just supply a function name as an argument like this:
33
34<blockquote>
35<pre>
36/* Some callback function */
37int add(int a, int b) {
38   return a+b;
39} 
40...
41int r = do_op(x,y,add);
42</pre>
43</blockquote>
44
45To make this work with SWIG, you will need to do a little extra work.  Specifically,
46you need to create some function pointer objects using the %constant directive like this:
47
48<blockquote>
49<pre>
50%constant(int (*)(int,int)) ADD = add;
51</pre>
52</blockquote>
53
54Now, in a script, you would do this:
55
56<blockquote>
57<pre>
58$r = do_op($x,$y, $ADD);
59</pre>
60</blockquote>
61
62<h2>An Example</h2>
63
64Here are some files that illustrate this with a simple example:
65
66<ul>
67<li><a href="example.c">example.c</a>
68<li><a href="example.h">example.h</a>
69<li><a href="example.i">example.i</a> (SWIG interface)
70<li><a href="runme.pl">runme.pl</a> (Sample script)
71</ul>
72
73<h2>Notes</h2>
74
75<ul>
76<li>The value of a function pointer must correspond to a function written in C or C++.
77It is not possible to pass an arbitrary Perl function object in as a substitute for a C 
78function pointer.
79
80<p>
81<li>A perl function can be used as a C/C++ callback if you write some
82clever typemaps and are very careful about how you create your extension.
83This is an advanced topic not covered here.
84</ul>
85
86<hr>
87</body>
88</html>
89
90
91
92