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

« back to all changes in this revision

Viewing changes to Doc/Manual/SWIGPlus.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:
46
46
<li><a href="#SWIGPlus_nn27">Comments on overloading</a>
47
47
</ul>
48
48
<li><a href="#SWIGPlus_nn28">Wrapping overloaded operators</a>
49
 
<li><a href="#SWIGPlus_nn29">Class extension</a>
 
49
<li><a href="#SWIGPlus_class_extension">Class extension</a>
50
50
<li><a href="#SWIGPlus_nn30">Templates</a>
51
51
<li><a href="#SWIGPlus_nn31">Namespaces</a>
 
52
<li><a href="#SWIGPlus_renaming_templated_types_namespaces">Renaming templated types in namespaces</a>
52
53
<li><a href="#SWIGPlus_exception_specifications">Exception specifications</a>
53
54
<li><a href="#SWIGPlus_catches">Exception handling with %catches</a>
54
55
<li><a href="#SWIGPlus_nn33">Pointers to Members</a>
575
576
inherit from an abstract class, but don't provide definitions for all of the pure methods.
576
577
</li>
577
578
 
578
 
<li>A default constructor is not created unless all bases classes support a 
 
579
<li>A default constructor is not created unless all base classes support a 
579
580
default constructor. 
580
581
</li>
581
582
 
1493
1494
</p>
1494
1495
 
1495
1496
<p> SWIG treats private or protected inheritance as close to the C++
1496
 
spirit, and target language capabilities, as possible. In most of the
 
1497
spirit, and target language capabilities, as possible. In most
1497
1498
cases, this means that SWIG will parse the non-public inheritance
1498
1499
declarations, but that will have no effect in the generated code,
1499
1500
besides the implicit policies derived for constructor and
1656
1657
This behavior resulted in huge amounts of replicated code for large
1657
1658
class hierarchies and made it awkward to build applications spread
1658
1659
across multiple modules (since accessor functions are duplicated in
1659
 
every single module).   It is also unnecessary to have such wrappers
 
1660
every single module).  It is also unnecessary to have such wrappers
1660
1661
when advanced features like proxy classes are used.  
1661
1662
 
1662
1663
<b>Note:</b> Further optimizations are enabled when using the
2441
2442
</li>
2442
2443
 
2443
2444
<li><p>
 
2445
Currently no resolution is performed in order to match function parameters. This means function parameter types must match exactly.
 
2446
For example, namespace qualifiers and typedefs will not work. The following usage of typedefs demonstrates this:
 
2447
 
 
2448
<div class="code">
 
2449
<pre>
 
2450
typedef int Integer;
 
2451
 
 
2452
%rename(foo_i) foo(int);
 
2453
 
 
2454
class Spam {
 
2455
public:
 
2456
   void foo(Integer);  // Stays 'foo' (not renamed)
 
2457
};
 
2458
class Ham {
 
2459
public:
 
2460
   void foo(int);      // Renamed to foo_i
 
2461
};
 
2462
</pre>
 
2463
</div>
 
2464
 
 
2465
<li><p>
2444
2466
The name matching rules also use default arguments for finer control when wrapping methods that have default arguments.
2445
2467
Recall that methods with default arguments are wrapped as if the equivalent overloaded methods had been parsed
2446
2468
(<a href="#SWIGPlus_default_args">Default arguments</a> section).
2709
2731
</li>
2710
2732
</ul>
2711
2733
 
2712
 
<H2><a name="SWIGPlus_nn29"></a>6.17 Class extension</H2>
 
2734
<H2><a name="SWIGPlus_class_extension"></a>6.17 Class extension</H2>
2713
2735
 
2714
2736
 
2715
2737
<p>
2764
2786
The C++ 'this' pointer is often needed to access member variables, methods etc.
2765
2787
The <tt>$self</tt> special variable should be used wherever you could use 'this'.
2766
2788
The example above demonstrates this for accessing member variables.
 
2789
Note that the members dereferenced by <tt>$self</tt> must be public members as the code is ultimately generated
 
2790
into a global function and so will not have any access to non-public members.
2767
2791
The implicit 'this' pointer that is present in C++ methods is not present in <tt>%extend</tt> methods.
2768
2792
In order to access anything in the extended class or its base class, an explicit 'this' is required.
2769
2793
The following example shows how one could access base class members:
2780
2804
};
2781
2805
%extend Derived {
2782
2806
  virtual void method(int v) {
2783
 
    $self-&gt;Base::method(v);
2784
 
    $self-&gt;value = v;
 
2807
    $self-&gt;Base::method(v); // akin to this-&gt;Base::method(v);
 
2808
    $self-&gt;value = v;       // akin to this-&gt;value = v;
2785
2809
    ...
2786
2810
  }
2787
2811
}
2816
2840
</div>
2817
2841
 
2818
2842
<p>
2819
 
There are some restrictions on the use of non-type arguments.  Specifically,
2820
 
they have to be simple literals and not expressions.  For example:
 
2843
There are some restrictions on the use of non-type arguments.  Simple literals
 
2844
are supported, and so are some constant expressions.  However, use of '&lt;'
 
2845
and '&gt;' within a constant expressions currently is not supported by SWIG
 
2846
('&lt;=' and '&gt;=' are though).  For example:
2821
2847
</p>
2822
2848
 
2823
2849
<div class="code">
2824
2850
<pre>
2825
 
void bar(list&lt;int,100&gt; *x);    // OK
2826
 
void bar(list&lt;int,2*50&gt; *x);   // Illegal
 
2851
void bar(list&lt;int,100&gt; *x);                // OK
 
2852
void bar(list&lt;int,2*50&gt; *x);               // OK
 
2853
void bar(list&lt;int,(2&gt;1 ? 100 : 50)&gt; *x)    // Not supported
2827
2854
</pre>
2828
2855
</div>
2829
2856
 
3141
3168
</div>
3142
3169
 
3143
3170
<p>
3144
 
Note the use of a vararg macro for the type T. If this wasn't used, the comma in the templated type in last example would not be possible.
 
3171
Note the use of a vararg macro for the type T. If this wasn't used, the comma in the templated type in the last example would not be possible.
3145
3172
</p>
3146
3173
 
3147
3174
<p>
3479
3506
 
3480
3507
...
3481
3508
template&lt;class T&gt; class List {
3482
 
   ...
3483
 
   public:
3484
 
   List() { };
3485
 
   ...
 
3509
    ...
 
3510
    public:
 
3511
    List() { };
 
3512
    T get(int index);
 
3513
    ...
3486
3514
};
3487
3515
</pre>
3488
3516
</div>
3954
3982
</p>
3955
3983
 
3956
3984
<p>
3957
 
<b>Note:</b> Namespaces have a subtle effect on the wrapping of conversion operators.  For
3958
 
instance, suppose you had an interface like this:
 
3985
<b>Note:</b> In the same way that no resolution is performed on parameters, a conversion operator name must match exactly to how it is defined. Do not change the qualification of the operator. For example, suppose you had an interface like this:
3959
3986
</p>
3960
3987
 
3961
3988
<div class="code">
3973
4000
</div>
3974
4001
 
3975
4002
<p>
3976
 
To wrap the conversion function, you might be inclined to write this:
 
4003
The following is how the feature is expected to be written for a successful match:
3977
4004
</p>
3978
4005
 
3979
4006
<div class="code">
3983
4010
</div>
3984
4011
 
3985
4012
<p>
3986
 
The only problem is that it doesn't work.   The reason it doesn't work is that
3987
 
<tt>bar</tt> is not defined in the global scope.  Therefore, to make it work, do this
3988
 
instead:
 
4013
The following does not work as no namespace resolution is performed in the matching of conversion operator names:
3989
4014
</p>
3990
4015
 
3991
4016
<div class="code">
3995
4020
</div>
3996
4021
 
3997
4022
<p>
 
4023
Note, however, that if the operator is defined using a qualifier in its name, then the feature must use it too...
 
4024
</p>
 
4025
 
 
4026
<div class="code">
 
4027
<pre>
 
4028
%rename(tofoo) foo::spam::operator bar();      // will not match
 
4029
%rename(tofoo) foo::spam::operator foo::bar(); // will match
 
4030
namespace foo {
 
4031
   class bar;
 
4032
   class spam {
 
4033
   public:
 
4034
        ...
 
4035
        operator foo::bar();
 
4036
        ...
 
4037
   };
 
4038
}
 
4039
</pre>
 
4040
</div>
 
4041
 
 
4042
<p>
 
4043
<b>Compatibility Note:</b> Versions of SWIG prior to 1.3.32 were inconsistent in this approach. A fully qualified name was usually required, but would not work in some situations.
 
4044
</p>
 
4045
 
 
4046
 
 
4047
<p>
3998
4048
<b>Note:</b> The flattening of namespaces is only intended to serve as
3999
 
a basic namespace implementation.  Since namespaces are a new addition
4000
 
to SWIG, none of the target language modules are currently programmed
 
4049
a basic namespace implementation.  
 
4050
None of the target language modules are currently programmed
4001
4051
with any namespace awareness.   In the future, language modules may or may not provide
4002
4052
more advanced namespace support.
4003
4053
</p>
4004
4054
 
4005
 
 
4006
 
<H2><a name="SWIGPlus_exception_specifications"></a>6.20 Exception specifications</H2>
 
4055
<H2><a name="SWIGPlus_renaming_templated_types_namespaces"></a>6.20 Renaming templated types in namespaces</H2>
 
4056
 
 
4057
 
 
4058
<p>
 
4059
As has been mentioned, when %rename includes parameters, the parameter types must match exactly (no typedef or namespace resolution is performed).
 
4060
SWIG treats templated types slightly differently and has an additional matching rule so unlike non-templated types, an exact match is not always required.
 
4061
If the fully qualified templated type is specified, it will have a higher precedence over the generic template type.
 
4062
In the example below, the generic template type is used to rename to <tt>bbb</tt> and the fully qualified type is used to rename to <tt>ccc</tt>.
 
4063
</p>
 
4064
 
 
4065
<div class="code">
 
4066
<pre>
 
4067
%rename(bbb) Space::ABC::aaa(T t);                       // will match but with lower precedence than ccc
 
4068
%rename(ccc) Space::ABC&lt;Space::XYZ&gt;::aaa(Space::XYZ t);  // will match but with higher precedence than bbb
 
4069
 
 
4070
namespace Space {
 
4071
  class XYZ {};
 
4072
  template&lt;typename T&gt; struct ABC {
 
4073
    void aaa(T t) {}
 
4074
  };
 
4075
}
 
4076
%template(ABCXYZ) Space::ABC&lt;Space::XYZ&gt;;
 
4077
</pre>
 
4078
</div>
 
4079
 
 
4080
<p>
 
4081
It should now be apparent that there are many ways to achieve a renaming with %rename. This is demonstrated
 
4082
by the following two examples, which are effectively the same as the above example.
 
4083
Below shows how %rename can be placed inside a namespace.
 
4084
</p>
 
4085
 
 
4086
<div class="code">
 
4087
<pre>
 
4088
namespace Space {
 
4089
  %rename(bbb) ABC::aaa(T t);                       // will match but with lower precedence than ccc
 
4090
  %rename(ccc) ABC&lt;Space::XYZ&gt;::aaa(Space::XYZ t);  // will match but with higher precedence than bbb
 
4091
  %rename(ddd) ABC&lt;Space::XYZ&gt;::aaa(XYZ t);         // will not match
 
4092
}
 
4093
 
 
4094
namespace Space {
 
4095
  class XYZ {};
 
4096
  template&lt;typename T&gt; struct ABC {
 
4097
    void aaa(T t) {}
 
4098
  };
 
4099
}
 
4100
%template(ABCXYZ) Space::ABC&lt;Space::XYZ&gt;;
 
4101
</pre>
 
4102
</div>
 
4103
 
 
4104
<p>
 
4105
Note that <tt>ddd</tt> does not match as there is no namespace resolution for parameter types and the fully qualified type must be specified for template type expansion.
 
4106
The following example shows how %rename can be placed within %extend.
 
4107
</p>
 
4108
 
 
4109
<div class="code">
 
4110
<pre>
 
4111
namespace Space {
 
4112
  %extend ABC {
 
4113
    %rename(bbb) aaa(T t);           // will match but with lower precedence than ccc
 
4114
  }
 
4115
  %extend ABC&lt;Space::XYZ&gt; {
 
4116
    %rename(ccc) aaa(Space::XYZ t);  // will match but with higher precedence than bbb
 
4117
    %rename(ddd) aaa(XYZ t);         // will not match
 
4118
  }
 
4119
}
 
4120
 
 
4121
namespace Space {
 
4122
  class XYZ {};
 
4123
  template&lt;typename T&gt; struct ABC {
 
4124
    void aaa(T t) {}
 
4125
  };
 
4126
}
 
4127
%template(ABCXYZ) Space::ABC&lt;Space::XYZ&gt;;
 
4128
</pre>
 
4129
</div>
 
4130
 
 
4131
 
 
4132
<H2><a name="SWIGPlus_exception_specifications"></a>6.21 Exception specifications</H2>
4007
4133
 
4008
4134
 
4009
4135
<p>
4054
4180
The next section details a way of simulating an exception specification or replacing an existing one.
4055
4181
</p>
4056
4182
 
4057
 
<H2><a name="SWIGPlus_catches"></a>6.21 Exception handling with %catches</H2>
 
4183
<H2><a name="SWIGPlus_catches"></a>6.22 Exception handling with %catches</H2>
4058
4184
 
4059
4185
 
4060
4186
<p>
4104
4230
</p>
4105
4231
 
4106
4232
 
4107
 
<H2><a name="SWIGPlus_nn33"></a>6.22 Pointers to Members</H2>
 
4233
<H2><a name="SWIGPlus_nn33"></a>6.23 Pointers to Members</H2>
4108
4234
 
4109
4235
 
4110
4236
<p>
4154
4280
for member pointers.
4155
4281
</p>
4156
4282
 
4157
 
<H2><a name="SWIGPlus_nn34"></a>6.23 Smart pointers and operator-&gt;()</H2>
 
4283
<H2><a name="SWIGPlus_nn34"></a>6.24 Smart pointers and operator-&gt;()</H2>
4158
4284
 
4159
4285
 
4160
4286
<p>
4364
4490
</p>
4365
4491
 
4366
4492
 
4367
 
<H2><a name="SWIGPlus_nn35"></a>6.24 Using declarations and inheritance</H2>
 
4493
<H2><a name="SWIGPlus_nn35"></a>6.25 Using declarations and inheritance</H2>
4368
4494
 
4369
4495
 
4370
4496
<p>
4527
4653
</div>
4528
4654
</ul>
4529
4655
 
4530
 
<H2><a name="SWIGPlus_nested_classes"></a>6.25 Nested classes</H2>
 
4656
<H2><a name="SWIGPlus_nested_classes"></a>6.26 Nested classes</H2>
4531
4657
 
4532
4658
 
4533
4659
<p>
4618
4744
The downside to this approach is having to maintain two definitions of <tt>Inner</tt>, the real one and the one in the interface file that SWIG parses.
4619
4745
</p>
4620
4746
 
4621
 
<H2><a name="SWIGPlus_nn37"></a>6.26 A brief rant about const-correctness</H2>
 
4747
<H2><a name="SWIGPlus_nn37"></a>6.27 A brief rant about const-correctness</H2>
4622
4748
 
4623
4749
 
4624
4750
<p>
4676
4802
of your project.
4677
4803
</p>
4678
4804
 
4679
 
<H2><a name="SWIGPlus_nn42"></a>6.27 Where to go for more information</H2>
 
4805
<H2><a name="SWIGPlus_nn42"></a>6.28 Where to go for more information</H2>
4680
4806
 
4681
4807
 
4682
4808
<p>