Improve documentation for multi-argument typemaps and overloading

This commit is contained in:
William S Fulton 2016-04-02 13:44:44 +01:00
commit 355f2623c7
2 changed files with 55 additions and 5 deletions

View file

@ -1161,7 +1161,7 @@ When wrapped, you will be able to use the functions in a natural way from Python
<div class="targetlang"> <div class="targetlang">
<pre> <pre>
&gt;&gt;&gt; import example &gt;&gt;&gt; import example
&gt;&gt;&gt; f = example.fopen("junk","w") &gt;&gt;&gt; f = example.fopen("junk", "w")
&gt;&gt;&gt; example.fputs("Hello World\n", f) &gt;&gt;&gt; example.fputs("Hello World\n", f)
&gt;&gt;&gt; example.fclose(f) &gt;&gt;&gt; example.fclose(f)
</pre> </pre>
@ -4725,7 +4725,7 @@ follows :
<div class="targetlang"><pre> <div class="targetlang"><pre>
&gt;&gt;&gt; from argv import * &gt;&gt;&gt; from argv import *
&gt;&gt;&gt; print_args(["Dave","Mike","Mary","Jane","John"]) &gt;&gt;&gt; print_args(["Dave", "Mike", "Mary", "Jane", "John"])
argv[0] = Dave argv[0] = Dave
argv[1] = Mike argv[1] = Mike
argv[2] = Mary argv[2] = Mary
@ -4763,7 +4763,7 @@ allows the function to be used from Python as follows:
<div class="targetlang"> <div class="targetlang">
<pre> <pre>
&gt;&gt;&gt; foo(4, ["foo","bar","spam","1"]) &gt;&gt;&gt; foo(4, ["foo", "bar", "spam", "1"])
</pre> </pre>
</div> </div>
@ -4817,7 +4817,47 @@ to supply the argument count. This is automatically set by the typemap code. F
<div class="targetlang"> <div class="targetlang">
<pre> <pre>
&gt;&gt;&gt; foo(["foo","bar","spam","1"]) &gt;&gt;&gt; foo(["foo", "bar", "spam", "1"])
</pre>
</div>
<p>
If your function is overloaded in C++, for example:
</p>
<div class="code">
<pre>
int foo(int argc, char **argv);
int foo();
</pre>
</div>
<p>
don't forget to also provide a suitable <a href="Typemaps.html#Typemaps_overloading">typecheck typemap for overloading</a>
such as:
</p>
<div class="code">
<pre>
%typecheck(SWIG_TYPECHECK_STRING_ARRAY) (int argc, char **argv) {
$1 = PyList_Check($input) ? 1 : 0;
}
</pre>
</div>
<p>
If you don't you'll get an error message along the lines of:
</p>
<div class="shell">
<pre>
Traceback (most recent call last):
File "runme.py", line 3, in <module>
example.foo(["foo", "bar", "spam", "1"])
NotImplementedError: Wrong number or type of arguments for overloaded function 'foo'.
Possible C/C++ prototypes are:
foo(int,char **)
foo()
</pre> </pre>
</div> </div>

View file

@ -3444,7 +3444,7 @@ list of strings like this:
<div class="targetlang"> <div class="targetlang">
<pre> <pre>
&gt;&gt;&gt; foo(["ale","lager","stout"]) &gt;&gt;&gt; foo(["ale", "lager", "stout"])
</pre> </pre>
</div> </div>
@ -3487,6 +3487,11 @@ maps perform the conversion described for the above example:
%typemap(freearg) (int argc, char *argv[]) { %typemap(freearg) (int argc, char *argv[]) {
if ($2) free($2); if ($2) free($2);
} }
/* Required for C++ method overloading */
%typecheck(SWIG_TYPECHECK_STRING_ARRAY) (int argc, char *argv[]) {
$1 = PyList_Check($input) ? 1 : 0;
}
</pre> </pre>
</div> </div>
@ -3554,6 +3559,11 @@ Other directives such as <tt>%apply</tt> and <tt>%clear</tt> also work with mult
</pre> </pre>
</div> </div>
<p>
Don't forget to also provide a suitable <a href="#Typemaps_overloading">typemap for overloaded functions</a>, such as <tt>%typecheck</tt> shown for foo above.
This is only required if the function is overloaded in C++.
</p>
<p> <p>
Although multi-argument typemaps may seem like an exotic, little used feature, there Although multi-argument typemaps may seem like an exotic, little used feature, there
are several situations where they make sense. First, suppose you wanted to wrap are several situations where they make sense. First, suppose you wanted to wrap