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

« back to all changes in this revision

Viewing changes to Doc/Manual/Typemaps.html

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2009-11-15 14:00:28 UTC
  • mfrom: (1.2.9 upstream) (2.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091115140028-me7amr2rie8zz1xn
Tags: 1.3.40-2ubuntu1
* Merge from Debian testing (LP: #356529), remaining changes:
  - Drop libchicken-dev from the build-depends (it's in universe)
  - Remove Pike from package description and from configure flags
  - drop "--without-mzscheme", we don't have it in our build-depends
  - use php-config5
  - Clean Runtime/ as well.
  - debian/rules (clean): Remove Lib/ocaml/swigp4.ml.
* debian/rules: Remove hardcoded python version.
* Remove upper limit for python from Build-Depends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
</ul>
23
23
<li><a href="#Typemaps_nn10">Typemap specifications</a>
24
24
<ul>
25
 
<li><a href="#Typemaps_nn11">Defining a typemap</a>
 
25
<li><a href="#Typemaps_defining">Defining a typemap</a>
26
26
<li><a href="#Typemaps_nn12">Typemap scope</a>
27
27
<li><a href="#Typemaps_nn13">Copying a typemap</a>
28
28
<li><a href="#Typemaps_nn14">Deleting a typemap</a>
29
29
<li><a href="#Typemaps_nn15">Placement of typemaps</a>
30
30
</ul>
31
 
<li><a href="#Typemaps_nn16">Pattern matching rules</a>
 
31
<li><a href="#Typemaps_pattern_matching">Pattern matching rules</a>
32
32
<ul>
33
33
<li><a href="#Typemaps_nn17">Basic matching rules</a>
34
34
<li><a href="#Typemaps_nn18">Typedef reductions</a>
41
41
<li><a href="#Typemaps_nn22">Scope</a>
42
42
<li><a href="#Typemaps_nn23">Declaring new local variables</a>
43
43
<li><a href="#Typemaps_special_variables">Special variables</a>
 
44
<li><a href="#Typemaps_special_variable_macros">Special variable macros</a>
 
45
<ul>
 
46
<li><a href="#Typemaps_special_macro_descriptor">$descriptor(type)</a>
 
47
<li><a href="#Typemaps_special_macro_typemap">$typemap(method, typepattern)</a>
 
48
</ul>
44
49
</ul>
45
50
<li><a href="#Typemaps_nn25">Common typemap methods</a>
46
51
<ul>
69
74
<li><a href="#runtime_type_checker">The run-time type checker</a>
70
75
<ul>
71
76
<li><a href="#Typemaps_nn45">Implementation</a>
72
 
<li><a href="#Typemaps_nn46">Usage</a>
 
77
<li><a href="#Typemaps_runtime_type_checker_usage">Usage</a>
73
78
</ul>
74
79
<li><a href="#Typemaps_overloading">Typemaps and overloading</a>
75
80
<li><a href="#Typemaps_nn48">More about <tt>%apply</tt> and <tt>%clear</tt></a>
76
81
<li><a href="#Typemaps_nn49">Reducing wrapper code size</a>
77
82
<li><a href="#Typemaps_nn47">Passing data between typemaps</a>
 
83
<li><a href="#Typemaps_nn52">C++ "this" pointer</a>
78
84
<li><a href="#Typemaps_nn51">Where to go for more information?</a>
79
85
</ul>
80
86
</div>
82
88
 
83
89
 
84
90
 
85
 
<p>
86
 
<b>Disclaimer: This chapter is under construction!</b>
87
 
</p>
88
 
 
89
91
<H2><a name="Typemaps_nn2"></a>10.1 Introduction</H2>
90
92
 
91
93
 
224
226
However, there is really not much to it.  The first typemap (the "in"
225
227
typemap) is used to convert a value from the target language to C.  The second
226
228
typemap (the "out" typemap) is used to convert in the other
227
 
direction. The content of each typemap is a small fragment of C code
228
 
that is inserted directly into the SWIG generated wrapper functions.  Within
229
 
this code, a number of special variables prefixed with a $ are expanded.  These are
230
 
really just placeholders for C variables that are generated in the course
 
229
direction. The content of each typemap is a small fragment of code
 
230
that is inserted directly into the SWIG generated wrapper functions.
 
231
The code is usually C or C++ code which will be generated into the C/C++ wrapper functions.
 
232
Note that this isn't always the case as some target language modules allow target language 
 
233
code within the typemaps which gets generated into target language specific files.
 
234
Within this code, a number of special variables prefixed with a $ are expanded.  These are
 
235
really just placeholders for C/C++ variables that are generated in the course
231
236
of creating the wrapper function. In this case, <tt>$input</tt> refers to an
232
 
input object that needs to be converted to C and <tt>$result</tt>
 
237
input object that needs to be converted to C/C++ and <tt>$result</tt>
233
238
refers to an object that is going to be returned by a wrapper
234
 
function.  <tt>$1</tt> refers to a C variable that has the same type as
 
239
function.  <tt>$1</tt> refers to a C/C++ variable that has the same type as
235
240
specified in the typemap declaration (an <tt>int</tt> in this
236
241
example).
237
242
</p>
654
659
This section describes the behavior of the <tt>%typemap</tt> directive itself.
655
660
</p>
656
661
 
657
 
<H3><a name="Typemaps_nn11"></a>10.2.1 Defining a typemap</H3>
 
662
<H3><a name="Typemaps_defining"></a>10.2.1 Defining a typemap</H3>
658
663
 
659
664
 
660
665
<p>
987
992
within a particular namespace.  In this example, this is done using the class declaration <tt>class string</tt>.
988
993
</p>
989
994
 
990
 
<H2><a name="Typemaps_nn16"></a>10.3 Pattern matching rules</H2>
 
995
<H2><a name="Typemaps_pattern_matching"></a>10.3 Pattern matching rules</H2>
991
996
 
992
997
 
993
998
<p>
1645
1650
 
1646
1651
<p>
1647
1652
Within all typemaps, the following special variables are expanded.
 
1653
This is by no means a complete list as some target languages have additional special variables which are documented in the language specific chapters.
1648
1654
</p>
1649
1655
 
1650
1656
<center>
1891
1897
</pre>
1892
1898
</div>
1893
1899
 
 
1900
<H3><a name="Typemaps_special_variable_macros"></a>10.4.4 Special variable macros</H3>
 
1901
 
 
1902
 
 
1903
<p>
 
1904
Special variable macros are like macro functions in that they take one or more input arguments 
 
1905
which are used for the macro expansion. 
 
1906
They look like macro/function calls but use the special variable <tt>$</tt> prefix to the macro name. 
 
1907
Note that unlike normal macros, the expansion is not done by the preprocessor, 
 
1908
it is done during the SWIG parsing/compilation stages. 
 
1909
The following special variable macros are available across all language modules.
 
1910
</p>
 
1911
 
 
1912
<H4><a name="Typemaps_special_macro_descriptor"></a>10.4.4.1 $descriptor(type)</H4>
 
1913
 
 
1914
 
 
1915
<p>
 
1916
This macro expands into the type descriptor structure for any C/C++ type specified in <tt>type</tt>.
 
1917
It behaves like the <tt>$1_descriptor</tt> special variable described above except that the type to expand is 
 
1918
taken from the macro argument rather than inferred from the typemap type. 
 
1919
For example, <tt>$descriptor(std::vector&lt;int&gt; *)</tt> will expand into <tt>SWIGTYPE_p_std__vectorT_int_t</tt>.
 
1920
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.
 
1921
</p>
 
1922
 
 
1923
<H4><a name="Typemaps_special_macro_typemap"></a>10.4.4.2 $typemap(method, typepattern)</H4>
 
1924
 
 
1925
 
 
1926
<p>
 
1927
This macro uses the <a href="#Typemaps_pattern_matching">pattern matching rules</a> described earlier to lookup and 
 
1928
then substitute the special variable macro with the code in the matched typemap.
 
1929
The typemap to search for is specified by the arguments, where <tt>method</tt> is the typemap method name and 
 
1930
<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.
 
1931
</p>
 
1932
 
 
1933
<p>
 
1934
The special variables within the matched typemap are expanded into those for the matched typemap type, 
 
1935
not the typemap within which the macro is called.
 
1936
In practice, there is little use for this macro in the scripting target languages.
 
1937
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.
 
1938
</p>
 
1939
 
 
1940
<p>
 
1941
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.
 
1942
</p>
 
1943
 
 
1944
<div class="code">
 
1945
<pre>
 
1946
%typemap(cstype) unsigned long    "uint"
 
1947
%typemap(cstype) unsigned long bb "bool"
 
1948
%typemap(cscode) BarClass %{
 
1949
  void foo($typemap(cstype, unsigned long aa) var1,
 
1950
           $typemap(cstype, unsigned long bb) var2,
 
1951
           $typemap(cstype, (unsigned long bb)) var3,
 
1952
           $typemap(cstype, unsigned long) var4)
 
1953
  {
 
1954
    // do something
 
1955
  }
 
1956
%}
 
1957
</pre>
 
1958
</div>
 
1959
 
 
1960
<p>
 
1961
The result is the following expansion
 
1962
</p>
 
1963
 
 
1964
<div class="code">
 
1965
<pre>
 
1966
%typemap(cstype) unsigned long    "uint"
 
1967
%typemap(cstype) unsigned long bb "bool"
 
1968
%typemap(cscode) BarClass %{
 
1969
  void foo(uint var1,
 
1970
           bool var2,
 
1971
           bool var3,
 
1972
           uint var4)
 
1973
  {
 
1974
    // do something
 
1975
  }
 
1976
%}
 
1977
</pre>
 
1978
</div>
 
1979
 
1894
1980
<H2><a name="Typemaps_nn25"></a>10.5 Common typemap methods</H2>
1895
1981
 
1896
1982
 
2635
2721
<p>
2636
2722
The "out" typemap is the main typemap for return types.
2637
2723
This typemap supports an optional attribute flag called "optimal", which is for reducing 
2638
 
temporary variables and the amount of generated code.
2639
 
It only really makes a difference when returning objects by value and it cannot always be used, 
 
2724
temporary variables and the amount of generated code, thereby giving the compiler the opportunity to 
 
2725
use <i>return value optimization</i> for generating faster executing code.
 
2726
It only really makes a difference when returning objects by value and has some limitations on usage,
2640
2727
as explained later on.
2641
2728
</p>
2642
2729
 
2694
2781
Note that three objects are being created as well as an assignment.
2695
2782
Wouldn't it be great if the <tt>XX::create()</tt> method was the only time a constructor was called?
2696
2783
As the method returns by value, this is asking a lot and the code that SWIG generates by default
2697
 
makes it impossible for the compiler to make this type of optimisation.
 
2784
makes it impossible for the compiler to use <i>return value optimisation (RVO)</i>.
2698
2785
However, this is where the "optimal" attribute in the "out" typemap can help out.
2699
2786
If the typemap code is kept the same and just the "optimal" attribute specified like this:
2700
2787
</p>
2753
2840
<p>
2754
2841
The major difference is the <tt>result</tt> temporary variable holding the value returned from <tt>XX::create()</tt> is no longer generated and instead the copy constructor call is made directly from
2755
2842
the value returned by <tt>XX::create()</tt>.
2756
 
With modern compiler optimisations turned on, the copy is not actually done, in fact the object is never created
 
2843
With modern compilers implementing RVO, the copy is not actually done, in fact the object is never created
2757
2844
on the stack in <tt>XX::create()</tt> at all, it is simply created directly on the heap.
2758
2845
In the first instance, the <tt>$1</tt> special variable in the typemap is expanded into <tt>result</tt>.
2759
2846
In the second instance, <tt>$1</tt> is expanded into <tt>XX::create()</tt> and this is essentially
2761
2848
</p>
2762
2849
 
2763
2850
<p>
2764
 
This kind of optimisation is not turned on by default as it has a number of restrictions.
 
2851
The "optimal" attribute optimisation is not turned on by default as it has a number of restrictions.
2765
2852
Firstly, some code cannot be condensed into a simple call for passing into the copy constructor.
2766
2853
One common occurrence is when <a href="Customization.html#exception">%exception</a> is used.
2767
2854
Consider adding the following <tt>%exception</tt> to the example:
3293
3380
structures are chained together in a circularly linked list.
3294
3381
</p>
3295
3382
 
3296
 
<H3><a name="Typemaps_nn46"></a>10.10.2 Usage</H3>
 
3383
<H3><a name="Typemaps_runtime_type_checker_usage"></a>10.10.2 Usage</H3>
3297
3384
 
3298
3385
 
3299
3386
<p>This section covers how to use these functions from typemaps.  To learn how to
3333
3420
 
3334
3421
<p>
3335
3422
Occasionally, you might need to write a typemap that needs to convert
3336
 
pointers of other types.  To handle this, a special macro substitution
3337
 
<tt>$descriptor(type)</tt> can be used to generate the SWIG type
 
3423
pointers of other types.  To handle this, the special variable macro
 
3424
<tt>$descriptor(type)</tt> covered earlier can be used to generate the SWIG type
3338
3425
descriptor name for any C datatype.  For example:
3339
3426
</p>
3340
3427
 
3899
3986
</p>
3900
3987
 
3901
3988
 
3902
 
<H2><a name="Typemaps_nn51"></a>10.15 Where to go for more information?</H2>
 
3989
<H2><a name="Typemaps_nn52"></a>10.15 C++ "this" pointer</H2>
 
3990
 
 
3991
 
 
3992
<p>
 
3993
All the rules discussed for Typemaps apply to C++ as well as C.
 
3994
However in addition C++ passes an extra parameter into every
 
3995
non-static class method -- the <tt>this</tt> pointer.  Occasionally it can be
 
3996
useful to apply a typemap to this pointer (for example to check
 
3997
and make sure <tt>this</tt> is non-null before deferencing).
 
3998
Actually, C also has an the equivalent of the <tt>this</tt> pointer which is used
 
3999
when accessing variables in a C struct.
 
4000
</p>
 
4001
<p>
 
4002
In order to customise the <tt>this</tt> pointer handling, target a variable named <tt>self</tt> in your typemaps.
 
4003
<tt>self</tt> is the name SWIG uses to refer to the extra parameter in wrapped functions. 
 
4004
</p>
 
4005
<p>
 
4006
For example, if wrapping for Java generation:
 
4007
</p>
 
4008
 
 
4009
<div class="code">
 
4010
<pre>
 
4011
%typemap(check) SWIGTYPE *self %{
 
4012
if (!$1) {
 
4013
  SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "swigCPtr null");
 
4014
  return $null;
 
4015
}
 
4016
%}
 
4017
</pre>
 
4018
</div>
 
4019
 
 
4020
<p>
 
4021
In the above case, the <tt>$1</tt> variable is expanded into the argument
 
4022
name that SWIG is using as the <tt>this</tt> pointer.
 
4023
 
 
4024
SWIG will then insert the check code before the actual C++ class method
 
4025
is called, and will raise an exception rather than crash
 
4026
the Java virtual machine.
 
4027
 
 
4028
The generated code will look something like:
 
4029
</p>
 
4030
 
 
4031
<div class="code">
 
4032
<pre>
 
4033
  if (!arg1) {
 
4034
    SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException,
 
4035
      "invalid native object; delete() likely already called");
 
4036
    return ;
 
4037
  }
 
4038
  (arg1)->wrappedFunction(...);
 
4039
</pre>
 
4040
</div>
 
4041
 
 
4042
<p>
 
4043
Note that if you have a parameter named <tt>self</tt> then it
 
4044
will also match the typemap.  One work around is to create an interface file that wraps
 
4045
the method, but give the argument a name other than <tt>self</tt>.
 
4046
</p>
 
4047
 
 
4048
<H2><a name="Typemaps_nn51"></a>10.16 Where to go for more information?</H2>
3903
4049
 
3904
4050
 
3905
4051
<p>
3913
4059
for how to define typemaps of your own.
3914
4060
Some of the language modules support additional typemaps and further
3915
4061
information is available in the individual chapters for each target language.
 
4062
There you may also find more hands-on practical examples.
3916
4063
</p>
3917
4064
 
3918
4065
</body>