merge from trunk
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11489 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
commit
21671f7534
86 changed files with 3689 additions and 502 deletions
|
|
@ -337,13 +337,13 @@
|
|||
</ul>
|
||||
<li><a href="Typemaps.html#Typemaps_nn10">Typemap specifications</a>
|
||||
<ul>
|
||||
<li><a href="Typemaps.html#Typemaps_nn11">Defining a typemap</a>
|
||||
<li><a href="Typemaps.html#Typemaps_defining">Defining a typemap</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn12">Typemap scope</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn13">Copying a typemap</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn14">Deleting a typemap</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn15">Placement of typemaps</a>
|
||||
</ul>
|
||||
<li><a href="Typemaps.html#Typemaps_nn16">Pattern matching rules</a>
|
||||
<li><a href="Typemaps.html#Typemaps_pattern_matching">Pattern matching rules</a>
|
||||
<ul>
|
||||
<li><a href="Typemaps.html#Typemaps_nn17">Basic matching rules</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn18">Typedef reductions</a>
|
||||
|
|
@ -356,6 +356,11 @@
|
|||
<li><a href="Typemaps.html#Typemaps_nn22">Scope</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn23">Declaring new local variables</a>
|
||||
<li><a href="Typemaps.html#Typemaps_special_variables">Special variables</a>
|
||||
<li><a href="Typemaps.html#Typemaps_special_variable_macros">Special variable macros</a>
|
||||
<ul>
|
||||
<li><a href="Typemaps.html#Typemaps_special_macro_descriptor">$descriptor(type)</a>
|
||||
<li><a href="Typemaps.html#Typemaps_special_macro_typemap">$typemap(method, typepattern)</a>
|
||||
</ul>
|
||||
</ul>
|
||||
<li><a href="Typemaps.html#Typemaps_nn25">Common typemap methods</a>
|
||||
<ul>
|
||||
|
|
@ -384,7 +389,7 @@
|
|||
<li><a href="Typemaps.html#runtime_type_checker">The run-time type checker</a>
|
||||
<ul>
|
||||
<li><a href="Typemaps.html#Typemaps_nn45">Implementation</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn46">Usage</a>
|
||||
<li><a href="Typemaps.html#Typemaps_runtime_type_checker_usage">Usage</a>
|
||||
</ul>
|
||||
<li><a href="Typemaps.html#Typemaps_overloading">Typemaps and overloading</a>
|
||||
<li><a href="Typemaps.html#Typemaps_nn48">More about <tt>%apply</tt> and <tt>%clear</tt></a>
|
||||
|
|
@ -1115,7 +1120,7 @@
|
|||
<li><a href="Perl5.html#Perl5_nn24">Modules and packages</a>
|
||||
</ul>
|
||||
<li><a href="Perl5.html#Perl5_nn25">Input and output parameters</a>
|
||||
<li><a href="Perl5.html#Perl5_nn26">Exception handling </a>
|
||||
<li><a href="Perl5.html#Perl5_nn26">Exception handling</a>
|
||||
<li><a href="Perl5.html#Perl5_nn27">Remapping datatypes with typemaps</a>
|
||||
<ul>
|
||||
<li><a href="Perl5.html#Perl5_nn28">A simple typemap example</a>
|
||||
|
|
@ -1125,8 +1130,8 @@
|
|||
</ul>
|
||||
<li><a href="Perl5.html#Perl5_nn32">Typemap Examples</a>
|
||||
<ul>
|
||||
<li><a href="Perl5.html#Perl5_nn33">Converting a Perl5 array to a char ** </a>
|
||||
<li><a href="Perl5.html#Perl5_nn34">Return values </a>
|
||||
<li><a href="Perl5.html#Perl5_nn33">Converting a Perl5 array to a char **</a>
|
||||
<li><a href="Perl5.html#Perl5_nn34">Return values</a>
|
||||
<li><a href="Perl5.html#Perl5_nn35">Returning values from arguments</a>
|
||||
<li><a href="Perl5.html#Perl5_nn36">Accessing array structure members</a>
|
||||
<li><a href="Perl5.html#Perl5_nn37">Turning Perl references into C pointers</a>
|
||||
|
|
@ -1173,6 +1178,16 @@
|
|||
</ul>
|
||||
<li><a href="Php.html#Php_nn2_7">PHP Pragmas, Startup and Shutdown code</a>
|
||||
</ul>
|
||||
<li><a href="Php.html#Php_nn3">Cross language polymorphism</a>
|
||||
<ul>
|
||||
<li><a href="Php.html#Php_nn3_1">Enabling directors</a>
|
||||
<li><a href="Php.html#Php_nn3_2">Director classes</a>
|
||||
<li><a href="Php.html#Php_nn3_3">Ownership and object destruction</a>
|
||||
<li><a href="Php.html#Php_nn3_4">Exception unrolling</a>
|
||||
<li><a href="Php.html#Php_nn3_5">Overhead and code bloat</a>
|
||||
<li><a href="Php.html#Php_nn3_6">Typemaps</a>
|
||||
<li><a href="Php.html#Php_nn3_7">Miscellaneous</a>
|
||||
</ul>
|
||||
</ul>
|
||||
</div>
|
||||
<!-- INDEX -->
|
||||
|
|
@ -1502,6 +1517,7 @@
|
|||
<ul>
|
||||
<li><a href="Tcl.html#Tcl_nn45">Proxy classes</a>
|
||||
</ul>
|
||||
<li><a href="Tcl.html#Tcl_nn46">Tcl/Tk Stubs</a>
|
||||
</ul>
|
||||
</div>
|
||||
<!-- INDEX -->
|
||||
|
|
|
|||
|
|
@ -32,6 +32,16 @@
|
|||
</ul>
|
||||
<li><a href="#Php_nn2_7">PHP Pragmas, Startup and Shutdown code</a>
|
||||
</ul>
|
||||
<li><a href="#Php_nn3">Cross language polymorphism</a>
|
||||
<ul>
|
||||
<li><a href="#Php_nn3_1">Enabling directors</a>
|
||||
<li><a href="#Php_nn3_2">Director classes</a>
|
||||
<li><a href="#Php_nn3_3">Ownership and object destruction</a>
|
||||
<li><a href="#Php_nn3_4">Exception unrolling</a>
|
||||
<li><a href="#Php_nn3_5">Overhead and code bloat</a>
|
||||
<li><a href="#Php_nn3_6">Typemaps</a>
|
||||
<li><a href="#Php_nn3_7">Miscellaneous</a>
|
||||
</ul>
|
||||
</ul>
|
||||
</div>
|
||||
<!-- INDEX -->
|
||||
|
|
@ -866,5 +876,381 @@ The <tt>%rinit</tt> and <tt>%rshutdown</tt> statements insert code
|
|||
into the request init and shutdown code respectively.
|
||||
</p>
|
||||
|
||||
<H2><a name="Php_nn3"></a>29.3 Cross language polymorphism</H2>
|
||||
|
||||
|
||||
<p>
|
||||
Proxy classes provide a more natural, object-oriented way to access
|
||||
extension classes. As described above, each proxy instance has an
|
||||
associated C++ instance, and method calls to the proxy are passed to the
|
||||
C++ instance transparently via C wrapper functions.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
This arrangement is asymmetric in the sense that no corresponding
|
||||
mechanism exists to pass method calls down the inheritance chain from
|
||||
C++ to PHP. In particular, if a C++ class has been extended in PHP
|
||||
(by extending the proxy class), these extensions will not be visible
|
||||
from C++ code. Virtual method calls from C++ are thus not able access
|
||||
the lowest implementation in the inheritance chain.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Changes have been made to SWIG 1.3.18 to address this problem and make
|
||||
the relationship between C++ classes and proxy classes more symmetric.
|
||||
To achieve this goal, new classes called directors are introduced at the
|
||||
bottom of the C++ inheritance chain. Support for generating PHP classes
|
||||
has been added in SWIG 1.3.40. The job of the directors is to route
|
||||
method calls correctly, either to C++ implementations higher in the
|
||||
inheritance chain or to PHP implementations lower in the inheritance
|
||||
chain. The upshot is that C++ classes can be extended in PHP and from
|
||||
C++ these extensions look exactly like native C++ classes. Neither C++
|
||||
code nor PHP code needs to know where a particular method is
|
||||
implemented: the combination of proxy classes, director classes, and C
|
||||
wrapper functions takes care of all the cross-language method routing
|
||||
transparently.
|
||||
</p>
|
||||
|
||||
<H3><a name="Php_nn3_1"></a>29.3.1 Enabling directors</H3>
|
||||
|
||||
|
||||
<p>
|
||||
The director feature is disabled by default. To use directors you
|
||||
must make two changes to the interface file. First, add the "directors"
|
||||
option to the %module directive, like this:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%module(directors="1") modulename
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
Without this option no director code will be generated. Second, you
|
||||
must use the %feature("director") directive to tell SWIG which classes
|
||||
and methods should get directors. The %feature directive can be applied
|
||||
globally, to specific classes, and to specific methods, like this:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
// generate directors for all classes that have virtual methods
|
||||
%feature("director");
|
||||
|
||||
// generate directors for all virtual methods in class Foo
|
||||
%feature("director") Foo;
|
||||
|
||||
// generate a director for just Foo::bar()
|
||||
%feature("director") Foo::bar;
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
You can use the %feature("nodirector") directive to turn off
|
||||
directors for specific classes or methods. So for example,
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%feature("director") Foo;
|
||||
%feature("nodirector") Foo::bar;
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
will generate directors for all virtual methods of class Foo except
|
||||
bar().
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Directors can also be generated implicitly through inheritance.
|
||||
In the following, class Bar will get a director class that handles
|
||||
the methods one() and two() (but not three()):
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%feature("director") Foo;
|
||||
class Foo {
|
||||
public:
|
||||
Foo(int foo);
|
||||
virtual void one();
|
||||
virtual void two();
|
||||
};
|
||||
|
||||
class Bar: public Foo {
|
||||
public:
|
||||
virtual void three();
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
then at the PHP side you can define
|
||||
</p>
|
||||
|
||||
<div class="targetlang">
|
||||
<pre>
|
||||
require("mymodule.php");
|
||||
|
||||
class MyFoo extends Foo {
|
||||
function one() {
|
||||
print "one from php\n";
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
|
||||
<H3><a name="Php_nn3_2"></a>29.3.2 Director classes</H3>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<p>
|
||||
For each class that has directors enabled, SWIG generates a new class
|
||||
that derives from both the class in question and a special
|
||||
<tt>Swig::Director</tt> class. These new classes, referred to as director
|
||||
classes, can be loosely thought of as the C++ equivalent of the PHP
|
||||
proxy classes. The director classes store a pointer to their underlying
|
||||
PHP object. Indeed, this is quite similar to the "_cPtr" and "thisown"
|
||||
members of the PHP proxy classes.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
For simplicity let's ignore the <tt>Swig::Director</tt> class and refer to the
|
||||
original C++ class as the director's base class. By default, a director
|
||||
class extends all virtual methods in the inheritance chain of its base
|
||||
class (see the preceding section for how to modify this behavior).
|
||||
Thus all virtual method calls, whether they originate in C++ or in
|
||||
PHP via proxy classes, eventually end up in at the implementation in the
|
||||
director class. The job of the director methods is to route these method
|
||||
calls to the appropriate place in the inheritance chain. By "appropriate
|
||||
place" we mean the method that would have been called if the C++ base
|
||||
class and its extensions in PHP were seamlessly integrated. That
|
||||
seamless integration is exactly what the director classes provide,
|
||||
transparently skipping over all the messy extension API glue that binds
|
||||
the two languages together.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
In reality, the "appropriate place" is one of only two possibilities:
|
||||
C++ or PHP. Once this decision is made, the rest is fairly easy. If the
|
||||
correct implementation is in C++, then the lowest implementation of the
|
||||
method in the C++ inheritance chain is called explicitly. If the correct
|
||||
implementation is in PHP, the Zend API is used to call the method of the
|
||||
underlying PHP object (after which the usual virtual method resolution
|
||||
in PHP automatically finds the right implementation).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Now how does the director decide which language should handle the method call?
|
||||
The basic rule is to handle the method in PHP, unless there's a good
|
||||
reason not to. The reason for this is simple: PHP has the most
|
||||
"extended" implementation of the method. This assertion is guaranteed,
|
||||
since at a minimum the PHP proxy class implements the method. If the
|
||||
method in question has been extended by a class derived from the proxy
|
||||
class, that extended implementation will execute exactly as it should.
|
||||
If not, the proxy class will route the method call into a C wrapper
|
||||
function, expecting that the method will be resolved in C++. The wrapper
|
||||
will call the virtual method of the C++ instance, and since the director
|
||||
extends this the call will end up right back in the director method. Now
|
||||
comes the "good reason not to" part. If the director method were to blindly
|
||||
call the PHP method again, it would get stuck in an infinite loop. We avoid this
|
||||
situation by adding special code to the C wrapper function that tells
|
||||
the director method to not do this. The C wrapper function compares the
|
||||
called and the declaring class name of the given method. If these are
|
||||
not the same, then the C wrapper function tells the director to resolve
|
||||
the method by calling up the C++ inheritance chain, preventing an
|
||||
infinite loop.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
One more point needs to be made about the relationship between director
|
||||
classes and proxy classes. When a proxy class instance is created in
|
||||
PHP, SWIG creates an instance of the original C++ class and assigns it
|
||||
to <tt>->_cPtr</tt>. This is exactly what happens without directors
|
||||
and is true even if directors are enabled for the particular class in
|
||||
question. When a class <i>derived</i> from a proxy class is created,
|
||||
however, SWIG then creates an instance of the corresponding C++ director
|
||||
class. The reason for this difference is that user-defined subclasses
|
||||
may override or extend methods of the original class, so the director
|
||||
class is needed to route calls to these methods correctly. For
|
||||
unmodified proxy classes, all methods are ultimately implemented in C++
|
||||
so there is no need for the extra overhead involved with routing the
|
||||
calls through PHP.
|
||||
</p>
|
||||
|
||||
<H3><a name="Php_nn3_3"></a>29.3.3 Ownership and object destruction</H3>
|
||||
|
||||
|
||||
<p>
|
||||
Memory management issues are slightly more complicated with directors
|
||||
than for proxy classes alone. PHP instances hold a pointer to the
|
||||
associated C++ director object, and the director in turn holds a pointer
|
||||
back to the PHP object. By default, proxy classes own their C++ director
|
||||
object and take care of deleting it when they are garbage collected.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
This relationship can be reversed by calling the special
|
||||
<tt>->thisown</tt> property of the proxy class. After setting this
|
||||
property to <tt>0</tt>, the director class no longer destroys the PHP
|
||||
object. Assuming no outstanding references to the PHP object remain,
|
||||
the PHP object will be destroyed at the same time. This is a good thing,
|
||||
since directors and proxies refer to each other and so must be created
|
||||
and destroyed together. Destroying one without destroying the other will
|
||||
likely cause your program to segfault.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Here is an example:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
class Foo {
|
||||
public:
|
||||
...
|
||||
};
|
||||
class FooContainer {
|
||||
public:
|
||||
void addFoo(Foo *);
|
||||
...
|
||||
};
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<br>
|
||||
|
||||
<div class="targetlang">
|
||||
<pre>
|
||||
$c = new FooContainer();
|
||||
$a = new Foo();
|
||||
$a->thisown = 0;
|
||||
$c->addFoo($a);
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
In this example, we are assuming that FooContainer will take care of
|
||||
deleting all the Foo pointers it contains at some point.
|
||||
</p>
|
||||
|
||||
<H3><a name="Php_nn3_4"></a>29.3.4 Exception unrolling</H3>
|
||||
|
||||
|
||||
<p>
|
||||
With directors routing method calls to PHP, and proxies routing them
|
||||
to C++, the handling of exceptions is an important concern. By default, the
|
||||
directors ignore exceptions that occur during method calls that are
|
||||
resolved in PHP. To handle such exceptions correctly, it is necessary
|
||||
to temporarily translate them into C++ exceptions. This can be done with
|
||||
the %feature("director:except") directive. The following code should
|
||||
suffice in most cases:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%feature("director:except") {
|
||||
if ($error == FAILURE) {
|
||||
throw Swig::DirectorMethodException();
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
This code will check the PHP error state after each method call from a
|
||||
director into PHP, and throw a C++ exception if an error occurred. This
|
||||
exception can be caught in C++ to implement an error handler.
|
||||
Currently no information about the PHP error is stored in the
|
||||
Swig::DirectorMethodException object, but this will likely change in the
|
||||
future.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
It may be the case that a method call originates in PHP, travels up to
|
||||
C++ through a proxy class, and then back into PHP via a director method.
|
||||
If an exception occurs in PHP at this point, it would be nice for that
|
||||
exception to find its way back to the original caller. This can be done
|
||||
by combining a normal %exception directive with the
|
||||
<tt>director:except</tt> handler shown above. Here is an example of a
|
||||
suitable exception handler:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%exception {
|
||||
try { $action }
|
||||
catch (Swig::DirectorException &e) { SWIG_fail; }
|
||||
}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
The class Swig::DirectorException used in this example is actually a
|
||||
base class of Swig::DirectorMethodException, so it will trap this
|
||||
exception. Because the PHP error state is still set when
|
||||
Swig::DirectorMethodException is thrown, PHP will register the exception
|
||||
as soon as the C wrapper function returns.
|
||||
</p>
|
||||
|
||||
<H3><a name="Php_nn3_5"></a>29.3.5 Overhead and code bloat</H3>
|
||||
|
||||
|
||||
<p>
|
||||
Enabling directors for a class will generate a new director method for
|
||||
every virtual method in the class' inheritance chain. This alone can
|
||||
generate a lot of code bloat for large hierarchies. Method arguments
|
||||
that require complex conversions to and from target language types can
|
||||
result in large director methods. For this reason it is recommended that
|
||||
you selectively enable directors only for specific classes that are
|
||||
likely to be extended in PHP and used in C++.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Compared to classes that do not use directors, the call routing in the
|
||||
director methods does add some overhead. In particular, at least one
|
||||
dynamic cast and one extra function call occurs per method call from
|
||||
PHP. Relative to the speed of PHP execution this is probably completely
|
||||
negligible. For worst case routing, a method call that ultimately
|
||||
resolves in C++ may take one extra detour through PHP in order to ensure
|
||||
that the method does not have an extended PHP implementation. This could
|
||||
result in a noticeable overhead in some cases.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Although directors make it natural to mix native C++ objects with PHP
|
||||
objects (as director objects) via a common base class pointer, one
|
||||
should be aware of the obvious fact that method calls to PHP objects
|
||||
will be much slower than calls to C++ objects. This situation can be
|
||||
optimized by selectively enabling director methods (using the %feature
|
||||
directive) for only those methods that are likely to be extended in PHP.
|
||||
</p>
|
||||
|
||||
<H3><a name="Php_nn3_6"></a>29.3.6 Typemaps</H3>
|
||||
|
||||
|
||||
<p>
|
||||
Typemaps for input and output of most of the basic types from director
|
||||
classes have been written. These are roughly the reverse of the usual
|
||||
input and output typemaps used by the wrapper code. The typemap
|
||||
operation names are 'directorin', 'directorout', and 'directorargout'.
|
||||
The director code does not currently use any of the other kinds of
|
||||
typemaps. It is not clear at this point which kinds are appropriate and
|
||||
need to be supported.
|
||||
</p>
|
||||
|
||||
|
||||
<H3><a name="Php_nn3_7"></a>29.3.7 Miscellaneous</H3>
|
||||
|
||||
|
||||
<p> Director typemaps for STL classes are mostly in place, and hence you
|
||||
should be able to use std::string, etc., as you would any other type.
|
||||
</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@
|
|||
<li><a href="#scilab_nn10">Global variables</a>
|
||||
<li><a href="#Scilab_nn11">Constants</a>
|
||||
<li><a href="#Scilab_nn12">Enums</a>
|
||||
<li><a href="#Octave_nn13">Pointers</a>
|
||||
<li><a href="#Octave_nn14">Structs</a>
|
||||
<li><a href="#Scilab_nn13">Pointers</a>
|
||||
<li><a href="#Scilab_nn14">Structs</a>
|
||||
</ul>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
@ -315,7 +315,7 @@ scilab:4> printf(" GREEN = %i\n", color.GREEN);
|
|||
</pre></div>
|
||||
|
||||
|
||||
<H3><a name="Octave_nn13"></a>27.3.5 Pointers</H3>
|
||||
<H3><a name="Scilab_nn13"></a>27.3.5 Pointers</H3>
|
||||
<p>
|
||||
Pointers are fully supported by SWIG. One way to deal with the pointers is using the INPUT and OUTPUT typemaps. For example, in order to call C functions as the following:
|
||||
</p>
|
||||
|
|
@ -356,7 +356,7 @@ scilab:4> printf(" 42/37 = %d remainder %d\n",q,r);
|
|||
we only need a real value instead.
|
||||
</p>
|
||||
|
||||
<H3><a name="Octave_nn14"></a>27.3.6 Structs</H3>
|
||||
<H3><a name="Scilab_nn14"></a>27.3.6 Structs</H3>
|
||||
<p>
|
||||
SWIG creates a set of accessor functions when encountering a structure or union. For example:
|
||||
</p>
|
||||
|
|
|
|||
|
|
@ -3412,6 +3412,7 @@ interesting things.
|
|||
|
||||
<H2><a name="Tcl_nn46"></a>33.10 Tcl/Tk Stubs</H2>
|
||||
|
||||
|
||||
<p>
|
||||
For background information about the Tcl Stubs feature, see
|
||||
<a href="http://www.tcl.tk/doc/howto/stubs.html">http://www.tcl.tk/doc/howto/stubs.html</a>.
|
||||
|
|
|
|||
|
|
@ -22,13 +22,13 @@
|
|||
</ul>
|
||||
<li><a href="#Typemaps_nn10">Typemap specifications</a>
|
||||
<ul>
|
||||
<li><a href="#Typemaps_nn11">Defining a typemap</a>
|
||||
<li><a href="#Typemaps_defining">Defining a typemap</a>
|
||||
<li><a href="#Typemaps_nn12">Typemap scope</a>
|
||||
<li><a href="#Typemaps_nn13">Copying a typemap</a>
|
||||
<li><a href="#Typemaps_nn14">Deleting a typemap</a>
|
||||
<li><a href="#Typemaps_nn15">Placement of typemaps</a>
|
||||
</ul>
|
||||
<li><a href="#Typemaps_nn16">Pattern matching rules</a>
|
||||
<li><a href="#Typemaps_pattern_matching">Pattern matching rules</a>
|
||||
<ul>
|
||||
<li><a href="#Typemaps_nn17">Basic matching rules</a>
|
||||
<li><a href="#Typemaps_nn18">Typedef reductions</a>
|
||||
|
|
@ -41,6 +41,11 @@
|
|||
<li><a href="#Typemaps_nn22">Scope</a>
|
||||
<li><a href="#Typemaps_nn23">Declaring new local variables</a>
|
||||
<li><a href="#Typemaps_special_variables">Special variables</a>
|
||||
<li><a href="#Typemaps_special_variable_macros">Special variable macros</a>
|
||||
<ul>
|
||||
<li><a href="#Typemaps_special_macro_descriptor">$descriptor(type)</a>
|
||||
<li><a href="#Typemaps_special_macro_typemap">$typemap(method, typepattern)</a>
|
||||
</ul>
|
||||
</ul>
|
||||
<li><a href="#Typemaps_nn25">Common typemap methods</a>
|
||||
<ul>
|
||||
|
|
@ -69,7 +74,7 @@
|
|||
<li><a href="#runtime_type_checker">The run-time type checker</a>
|
||||
<ul>
|
||||
<li><a href="#Typemaps_nn45">Implementation</a>
|
||||
<li><a href="#Typemaps_nn46">Usage</a>
|
||||
<li><a href="#Typemaps_runtime_type_checker_usage">Usage</a>
|
||||
</ul>
|
||||
<li><a href="#Typemaps_overloading">Typemaps and overloading</a>
|
||||
<li><a href="#Typemaps_nn48">More about <tt>%apply</tt> and <tt>%clear</tt></a>
|
||||
|
|
@ -655,7 +660,7 @@ of "The C Programming Language" by Kernighan and Ritchie or
|
|||
This section describes the behavior of the <tt>%typemap</tt> directive itself.
|
||||
</p>
|
||||
|
||||
<H3><a name="Typemaps_nn11"></a>10.2.1 Defining a typemap</H3>
|
||||
<H3><a name="Typemaps_defining"></a>10.2.1 Defining a typemap</H3>
|
||||
|
||||
|
||||
<p>
|
||||
|
|
@ -988,7 +993,7 @@ It should be noted that for scoping to work, SWIG has to know that <tt>string</t
|
|||
within a particular namespace. In this example, this is done using the class declaration <tt>class string</tt>.
|
||||
</p>
|
||||
|
||||
<H2><a name="Typemaps_nn16"></a>10.3 Pattern matching rules</H2>
|
||||
<H2><a name="Typemaps_pattern_matching"></a>10.3 Pattern matching rules</H2>
|
||||
|
||||
|
||||
<p>
|
||||
|
|
@ -1646,6 +1651,7 @@ each type must have its own local variable declaration.
|
|||
|
||||
<p>
|
||||
Within all typemaps, the following special variables are expanded.
|
||||
This is by no means a complete list as some target languages have additional special variables which are documented in the language specific chapters.
|
||||
</p>
|
||||
|
||||
<center>
|
||||
|
|
@ -1892,6 +1898,86 @@ Another approach, which only works for arrays is to use the <tt>$1_basetype</tt>
|
|||
</pre>
|
||||
</div>
|
||||
|
||||
<H3><a name="Typemaps_special_variable_macros"></a>10.4.4 Special variable macros</H3>
|
||||
|
||||
|
||||
<p>
|
||||
Special variable macros are like macro functions in that they take one or more input arguments
|
||||
which are used for the macro expansion.
|
||||
They look like macro/function calls but use the special variable <tt>$</tt> prefix to the macro name.
|
||||
Note that unlike normal macros, the expansion is not done by the preprocessor,
|
||||
it is done during the SWIG parsing/compilation stages.
|
||||
The following special variable macros are available across all language modules.
|
||||
</p>
|
||||
|
||||
<H4><a name="Typemaps_special_macro_descriptor"></a>10.4.4.1 $descriptor(type)</H4>
|
||||
|
||||
|
||||
<p>
|
||||
This macro expands into the type descriptor structure for any C/C++ type specified in <tt>type</tt>.
|
||||
It behaves like the <tt>$1_descriptor</tt> special variable described above except that the type to expand is
|
||||
taken from the macro argument rather than inferred from the typemap type.
|
||||
For example, <tt>$descriptor(std::vector<int> *)</tt> will expand into <tt>SWIGTYPE_p_std__vectorT_int_t</tt>.
|
||||
This macro is mostly used in the scripting target languages and is demonstrated later in the <a href="#Typemaps_runtime_type_checker_usage">Run-time type checker usage</a> section.
|
||||
</p>
|
||||
|
||||
<H4><a name="Typemaps_special_macro_typemap"></a>10.4.4.2 $typemap(method, typepattern)</H4>
|
||||
|
||||
|
||||
<p>
|
||||
This macro uses the <a href="#Typemaps_pattern_matching">pattern matching rules</a> described earlier to lookup and
|
||||
then substitute the special variable macro with the code in the matched typemap.
|
||||
The typemap to search for is specified by the arguments, where <tt>method</tt> is the typemap method name and
|
||||
<tt>typepattern</tt> is a type pattern as per the <tt>%typemap</tt> specification in the <a href="#Typemaps_defining">Defining a typemap</a> section.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The special variables within the matched typemap are expanded into those for the matched typemap type,
|
||||
not the typemap within which the macro is called.
|
||||
In practice, there is little use for this macro in the scripting target languages.
|
||||
It is mostly used in the target languages that are statically typed as a way to obtain the target language type given the C/C++ type and more commonly only when the C++ type is a template parameter.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The example below is for C# only and uses some typemap method names documented in the C# chapter, but it shows some of the possible syntax variations.
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%typemap(cstype) unsigned long "uint"
|
||||
%typemap(cstype) unsigned long bb "bool"
|
||||
%typemap(cscode) BarClass %{
|
||||
void foo($typemap(cstype, unsigned long aa) var1,
|
||||
$typemap(cstype, unsigned long bb) var2,
|
||||
$typemap(cstype, (unsigned long bb)) var3,
|
||||
$typemap(cstype, unsigned long) var4)
|
||||
{
|
||||
// do something
|
||||
}
|
||||
%}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<p>
|
||||
The result is the following expansion
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
<pre>
|
||||
%typemap(cstype) unsigned long "uint"
|
||||
%typemap(cstype) unsigned long bb "bool"
|
||||
%typemap(cscode) BarClass %{
|
||||
void foo(uint var1,
|
||||
bool var2,
|
||||
bool var3,
|
||||
uint var4)
|
||||
{
|
||||
// do something
|
||||
}
|
||||
%}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
<H2><a name="Typemaps_nn25"></a>10.5 Common typemap methods</H2>
|
||||
|
||||
|
||||
|
|
@ -3295,7 +3381,7 @@ structures rather than creating new ones. These <tt>swig_module_info</tt>
|
|||
structures are chained together in a circularly linked list.
|
||||
</p>
|
||||
|
||||
<H3><a name="Typemaps_nn46"></a>10.10.2 Usage</H3>
|
||||
<H3><a name="Typemaps_runtime_type_checker_usage"></a>10.10.2 Usage</H3>
|
||||
|
||||
|
||||
<p>This section covers how to use these functions from typemaps. To learn how to
|
||||
|
|
@ -3335,8 +3421,8 @@ type tables and improves efficiency.
|
|||
|
||||
<p>
|
||||
Occasionally, you might need to write a typemap that needs to convert
|
||||
pointers of other types. To handle this, a special macro substitution
|
||||
<tt>$descriptor(type)</tt> can be used to generate the SWIG type
|
||||
pointers of other types. To handle this, the special variable macro
|
||||
<tt>$descriptor(type)</tt> covered earlier can be used to generate the SWIG type
|
||||
descriptor name for any C datatype. For example:
|
||||
</p>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue