Merge branch 'LindleyF-typemap-in-descriptor'

* LindleyF-typemap-in-descriptor:
  Add documentation and CHANGES for special variables and typemap attributes.
  Support special variable expansion in special variable macros in typemap attributes.
  Enable variable and typemap substitution in typemap kwargs, and a test that verifies this works for directorin:descriptor.
This commit is contained in:
William S Fulton 2015-07-22 23:44:58 +01:00
commit beabf61ef6
10 changed files with 516 additions and 6 deletions

View file

@ -5,6 +5,41 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.7 (in progress)
===========================
2015-07-22: wsfulton
Support for special variable expansion in typemap attributes. Example usage expansion
in the 'out' attribute (C# specific):
%typemap(ctype, out="$*1_ltype") unsigned int& "$*1_ltype"
is equivalent to the following as $*1_ltype expands to 'unsigned int':
%typemap(ctype, out="unsigned int") unsigned int& "unsigned int"
Special variables can be used within special variable macros too. Example usage expansion:
%typemap(cstype) unsigned int "uint"
%typemap(cstype, out="$typemap(cstype, $*1_ltype)") unsigned int& "$typemap(cstype, $*1_ltype)"
Special variables are expanded first and hence the above is equivalent to:
%typemap(cstype, out="$typemap(cstype, unsigned int)") unsigned int& "$typemap(cstype, unsigned int)"
which then expands to:
%typemap(cstype, out="uint") unsigned int& "uint"
2015-07-22: lindleyf
Apply patch #439 - support for $typemap() (aka embedded typemaps or special variable
macros) in typemap attributes. A simple example where $typemap() is expanded in the
'out' attribute (C# specific):
%typemap(cstype) unsigned int "uint"
%typemap(cstype, out="$typemap(cstype, unsigned int)") unsigned int& "$typemap(cstype, unsigned int)"
is equivalent to:
%typemap(cstype, out="uint") unsigned int& "uint"
2015-07-18: m7thon
[Python] Docstrings provided via %feature("docstring") are now quoted and added to
the tp_doc slot when using python builtin classes (-builtin). When no docstring is

View file

@ -441,6 +441,8 @@
<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>
<li><a href="Typemaps.html#Typemaps_special_variable_attributes">Special variables and typemap attributes</a>
<li><a href="Typemaps.html#Typemaps_special_variables_and_macros">Special variables combined with special variable macros</a>
</ul>
<li><a href="Typemaps.html#Typemaps_nn25">Common typemap methods</a>
<ul>

View file

@ -48,6 +48,8 @@
<li><a href="#Typemaps_special_macro_descriptor">$descriptor(type)</a>
<li><a href="#Typemaps_special_macro_typemap">$typemap(method, typepattern)</a>
</ul>
<li><a href="#Typemaps_special_variable_attributes">Special variables and typemap attributes</a>
<li><a href="#Typemaps_special_variables_and_macros">Special variables combined with special variable macros</a>
</ul>
<li><a href="#Typemaps_nn25">Common typemap methods</a>
<ul>
@ -2425,6 +2427,76 @@ The result is the following expansion
</pre>
</div>
<H3><a name="Typemaps_special_variable_attributes"></a>11.4.5 Special variables and typemap attributes</H3>
<p>
As of SWIG-3.0.7 typemap attributes will also expand special variables and special variable macros.
</p>
<p>
Example usage showing the expansion in the 'out' attribute (C# specific) as well as the main typemap body:
</p>
<div class="code">
<pre>
%typemap(ctype, out="$*1_ltype") unsigned int&amp; "$*1_ltype"
</pre>
</div>
<p>
is equivalent to the following as <tt>$*1_ltype</tt> expands to <tt>unsigned int</tt>:
</p>
<div class="code">
<pre>
%typemap(ctype, out="unsigned int") unsigned int&amp; "unsigned int"
</pre>
</div>
<H3><a name="Typemaps_special_variables_and_macros"></a>11.4.6 Special variables combined with special variable macros</H3>
<p>
Special variables can also be used within special variable macros.
The special variables are expanded before they are used in the special variable macros.
</p>
<p>
Consider the following C# typemaps:
</p>
<div class="code">
<pre>
%typemap(cstype) unsigned int "uint"
%typemap(cstype, out="$typemap(cstype, $*1_ltype)") unsigned int&amp; "$typemap(cstype, $*1_ltype)"
</pre>
</div>
<p>
Special variables are expanded first and hence the above is equivalent to:
</p>
<div class="code">
<pre>
%typemap(cstype) unsigned int "uint"
%typemap(cstype, out="$typemap(cstype, unsigned int)") unsigned int&amp; "$typemap(cstype, unsigned int)"
</pre>
</div>
<p>
which then expands to:
</p>
<div class="code">
<pre>
%typemap(cstype) unsigned int "uint"
%typemap(cstype, out="uint") unsigned int& "uint"
</pre>
</div>
<H2><a name="Typemaps_nn25"></a>11.5 Common typemap methods</H2>

View file

@ -374,6 +374,7 @@ CPP_TEST_CASES += \
smart_pointer_templatevariables \
smart_pointer_typedef \
special_variables \
special_variable_attributes \
special_variable_macros \
static_array_member \
static_const_member \

View file

@ -0,0 +1,32 @@
// This is the bool runtime testcase. It checks that the C++ bool type works.
using System;
using special_variable_attributesNamespace;
public class special_variable_attributes_runme {
public static void Main() {
if (special_variable_attributes.getNumber1() != 111)
throw new ApplicationException("getNumber1 failed");
if (special_variable_attributes.getNumber2() != 222)
throw new ApplicationException("getNumber2 failed");
if (special_variable_attributes.getNumber3() != 333)
throw new ApplicationException("getNumber3 failed");
if (special_variable_attributes.bounceNumber1(10) != 110)
throw new ApplicationException("bounceNumber1 failed");
if (special_variable_attributes.bounceNumber2(10) != 220)
throw new ApplicationException("bounceNumber2 failed");
if (special_variable_attributes.bounceNumber3(10) != 330)
throw new ApplicationException("bounceNumber3 failed");
if (special_variable_attributes.multi1(12.34) != 12+34)
throw new ApplicationException("multi1 failed");
if (special_variable_attributes.multi2(12.34) != 12+34+55)
throw new ApplicationException("multi2 failed");
if (special_variable_attributes.multi3(12.34) != 12+34+77)
throw new ApplicationException("multi3 failed");
}
}

View file

@ -27,6 +27,7 @@ CPP_TEST_CASES = \
java_director_assumeoverride \
java_director_exception_feature \
java_director_exception_feature_nspace \
java_director_ptrclass \
java_enums \
java_jnitypes \
java_lib_arrays_dimensionless \

View file

@ -0,0 +1,47 @@
import java_director_ptrclass.*;
public class java_director_ptrclass_runme {
static {
try {
System.loadLibrary("java_director_ptrclass");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void main(String argv[]) {
Foo f = new Foo();
Foo ft = new TouchingFoo();
Baz b = new Baz();
if (b.GetTouched()) {
throw new RuntimeException ( "Baz should not have been touched yet." );
}
Baz b2 = f.FinalMaybeTouch(b);
if (b2.GetTouched() || b.GetTouched()) {
throw new RuntimeException ( "Baz should not have been touched by Foo." );
}
Baz b3 = ft.FinalMaybeTouch(b);
if (!b.GetTouched() || !b3.GetTouched() || !b2.GetTouched()) {
throw new RuntimeException ( "Baz was not touched by TouchingFoo. This" +
" might mean the directorin typemap is not" +
" parsing the typemap(jstype, Bar) in its" +
" 'descriptor' kwarg correctly." );
}
}
}
class TouchingFoo extends Foo {
@Override
public Baz MaybeTouch(Baz baz_ptr) {
baz_ptr.SetTouched();
return baz_ptr;
}
}

View file

@ -0,0 +1,107 @@
%module(directors="1") java_director_ptrclass
// Tests that custom director typemaps can be used with C++ types that
// represent a pointer, in such a way that Java perceives this class as
// equivalent to the underlying type. In particular, this verifies that
// a typemap lookup within a typemap kwarg, in this case
// directorin:descriptor, works as expected.
%{
namespace bar {
class Baz {
public:
Baz() : touched(false) {}
void SetTouched() { touched = true; }
bool GetTouched() { return touched; }
private:
bool touched;
};
template <typename T>
class Ptr {
public:
Ptr(T* b) : b_(b) {}
T* Get() const { return b_; }
private:
T* b_;
};
class Foo {
public:
// Calling FinalMaybeTouch from Java unambiguously goes through C++ to
// reach MaybeTouch.
Ptr< bar::Baz > FinalMaybeTouch(Baz* b) {
return MaybeTouch(Ptr< bar::Baz >(b));
}
virtual Ptr< bar::Baz > MaybeTouch(Ptr< bar::Baz > f) {
return f; /* Don't touch */
}
virtual ~Foo() {}
};
}
%}
%feature("director") Foo;
%typemap(jni) bar::Ptr< bar::Baz > "jlong"
%typemap(jtype) bar::Ptr< bar::Baz > "long"
%typemap(jstype) bar::Ptr< bar::Baz > "Baz"
%typemap(in) bar::Ptr< bar::Baz > {
$1 = bar::Ptr< bar::Baz >(*( bar::Baz**)&$input);
}
%typemap(out) bar::Ptr< bar::Baz > {
const bar::Ptr< bar::Baz >& ptr = $1;
if (ptr.Get()) {
$result = ($typemap(jni, bar::Baz))ptr.Get();
} else {
$result = 0;
}
}
%typemap(javain) bar::Ptr< bar::Baz > "$typemap(jstype, bar::Baz).getCPtr($javainput)"
%typemap(javaout) bar::Ptr< bar::Baz > {
long cPtr = $jnicall;
return (cPtr == 0) ? null : new $typemap(jstype, bar::Baz)(cPtr, false);
}
%typemap(directorin, descriptor="L$packagepath/$typemap(jstype, bar::Baz);") bar::Ptr< bar::Baz >
%{ *((bar::Baz**)&$input) = ((bar::Ptr< bar::Baz >&)$1).Get(); %}
%typemap(directorout) bar::Ptr< bar::Baz > {
$result = bar::Ptr< bar::Baz >(*( bar::Baz**)&$input);
}
%typemap(javadirectorin) bar::Ptr< bar::Baz > %{
((long)$jniinput == 0) ? null : new $typemap(jstype, bar::Baz)($jniinput, false)
%}
%typemap(javadirectorout) bar::Ptr< bar::Baz > "$typemap(jstype, bar::Baz).getCPtr($javacall)"
namespace bar {
class Baz {
public:
Baz() : touched(false) {}
void SetTouched() { touched = true; }
bool GetTouched() { return touched; }
private:
bool touched;
};
template <typename T>
class Ptr {
public:
Ptr(T* b) : b_(b) {}
T* Get() { return b_; }
private:
T* b_;
};
class Foo {
public:
// Calling FinalMaybeTouch from Java unambiguously goes through C++ to
// reach MaybeTouch.
Ptr< bar::Baz > FinalMaybeTouch(Baz* b) {
return MaybeTouch(Ptr< bar::Baz >(b));
}
virtual Ptr< bar::Baz > MaybeTouch(Ptr< bar::Baz > f) {
return f; /* Don't touch */
}
virtual ~Foo() {}
};
}

View file

@ -0,0 +1,170 @@
%module special_variable_attributes
// Special variable expansion and special variable macros, aka embedded typemaps - expansion tests
// Tests these are expanded within typemap attributes.
// Also tests that these are expanded when used together, so that the special variables
// can be used as the type passed to the special variable macro.
// C# is used for testing as it is one of the few languages that uses a lot of typemap attributes.
// Attributes in both 'in' and 'out' typemaps are needed, that is,
// typemaps targeting both parameters and return values respectively).
#ifdef SWIGCSHARP
// Check special variable expansion in typemap attributes.
// This changes return by reference into return by value.
// Takes advantage of the fact that 'int' is a valid type in both C and C#.
// This is not a realistic use, just a way to test the variable expansion in the 'out' attribute.
%typemap(ctype, out="$*1_ltype") int& getNumber1 "_not_used_"
%typemap(imtype, out="$*1_ltype") int& getNumber1 "_not_used_"
%typemap(cstype, out="$*1_ltype") int& getNumber1 "_not_used_"
%typemap(out) int& getNumber1 "$result = *$1;"
%typemap(csout, excode=SWIGEXCODE) int & getNumber1 {
int ret = $imcall;$excode
return ret;
}
#endif
%inline %{
int& getNumber1() {
static int num = 111;
return num;
}
%}
#ifdef SWIGCSHARP
// Check special variable macro expansion in typemap attributes.
// This changes return by reference into return by value.
%typemap(ctype, out="$typemap(ctype, int)") int& getNumber2 "_not_used_"
%typemap(imtype, out="$typemap(imtype, int)") int& getNumber2 "_not_used_"
%typemap(cstype, out="$typemap(cstype, int)") int& getNumber2 "_not_used_"
%typemap(out) int& getNumber2 "$result = *$1;"
%typemap(csout, excode=SWIGEXCODE) int & getNumber2 {
int ret = $imcall;$excode
return ret;
}
#endif
%inline %{
int& getNumber2() {
static int num = 222;
return num;
}
%}
#ifdef SWIGCSHARP
// Check special variable macro expansion and special variable expansion in typemap attributes.
// This changes return by reference into return by value.
%typemap(ctype, out="$typemap(ctype, $*1_ltype)") int& getNumber3 "_not_used_"
%typemap(imtype, out="$typemap(imtype, $*1_ltype)") int& getNumber3 "_not_used_"
%typemap(cstype, out="$typemap(cstype, $*1_ltype)") int& getNumber3 "_not_used_"
%typemap(out) int& getNumber3 "$result = *$1;"
%typemap(csout, excode=SWIGEXCODE) int & getNumber3 {
int ret = $imcall;$excode
return ret;
}
#endif
%inline %{
int& getNumber3() {
static int num = 333;
return num;
}
%}
#ifdef SWIGCSHARP
// Check special variable macro expansion in typemap attributes.
%typemap(csin,
pre=" $typemap(cstype, int) $csinput_scaled = 11;"
) int num1
%{$csinput * $csinput_scaled %}
#endif
%inline %{
int bounceNumber1(int num1) {
return num1;
}
%}
#ifdef SWIGCSHARP
// Check special variable expansion in typemap attributes.
%typemap(csin,
pre=" $1_type $csinput_scaled = 22;"
) int num2
%{$csinput * $csinput_scaled %}
#endif
%inline %{
int bounceNumber2(int num2) {
return num2;
}
%}
#ifdef SWIGCSHARP
// Check special variable and special variable macro expansion in typemap attributes.
%typemap(csin,
pre=" $typemap(cstype, $1_type) $csinput_scaled = 33;"
) int num3
%{$csinput * $csinput_scaled %}
#endif
%inline %{
int bounceNumber3(int num3) {
return num3;
}
%}
/////////////////////////////////
//// Multi-argument typemaps ////
/////////////////////////////////
// Test expansion of special variables
#ifdef SWIGCSHARP
%typemap(ctype) (int intvar, char charvar) "double"
%typemap(imtype) (int intvar, char charvar) "double"
%typemap(cstype) (int intvar, char charvar) "double"
%typemap(in) (int intvar, char charvar)
%{
// split double value a.b into two numbers, a and b*100
$1 = (int)$input;
$2 = ($input - $1 + 0.005) * 100;
%}
%typemap(csin,
pre=" $1_type $csinput_$1_type = 50;\n" // $1_type should expand to int
" $2_type $csinput_$2_type = 'A';" // $2_type should expand to char
) (int intvar, char charvar)
%{$csinput + ($csinput_int - 50 + $csinput_char - 'A') + ($csinput_$1_type - 50 + $csinput_$2_type - 'A')%}
#endif
%inline %{
int multi1(int intvar, char charvar) {
return intvar + charvar;
}
%}
#ifdef SWIGCSHARP
%typemap(csin,
pre=" $typemap(cstype, int) $csinput_$typemap(cstype, int) = 50;\n" // also should expand to int
" $typemap(cstype, char) $csinput_$typemap(cstype, char) = 'A';" // also should expand to char
) (int intvar, char charvar)
%{55 + $csinput + ($csinput_int - 50 + $csinput_char - 'A') + ($csinput_$typemap(cstype, int) - 50 + $csinput_$typemap(cstype, char) - 'A')%}
#endif
%inline %{
int multi2(int intvar, char charvar) {
return intvar + charvar;
}
%}
#ifdef SWIGCSHARP
%typemap(csin,
pre=" $typemap(cstype, $1_type) $csinput_$typemap(cstype, $1_type) = 50;\n" // also should expand to int
" $typemap(cstype, $2_type) $csinput_$typemap(cstype, $2_type) = 'A';" // also should expand to char
) (int intvar, char charvar)
%{77 + $csinput + ($csinput_int - 50 + $csinput_char - 'A') + ($csinput_$typemap(cstype, $1_type) - 50 + $csinput_$typemap(cstype, $2_type) - 'A')%}
#endif
%inline %{
int multi3(int intvar, char charvar) {
return intvar + charvar;
}
%}

View file

@ -1397,6 +1397,22 @@ static String *Swig_typemap_lookup_impl(const_String_or_char_ptr tmap_method, No
String *value = Copy(Getattr(kw, "value"));
String *kwtype = Getattr(kw, "type");
char *ckwname = Char(Getattr(kw, "name"));
{
/* Expand special variables in typemap attributes. */
SwigType *ptype = Getattr(node, "type");
String *pname = Getattr(node, "name");
String *lname = Getattr(node, "lname");
SwigType *mtype = Getattr(node, "tmap:match");
SwigType *matchtype = mtype ? mtype : ptype;
ParmList *parm_sublist;
typemap_replace_vars(value, NULL, matchtype, ptype, pname, lname, 1);
/* Expand special variable macros (embedded typemaps) in typemap attributes. */
parm_sublist = NewParmWithoutFileLineInfo(ptype, pname);
Setattr(parm_sublist, "lname", lname);
replace_embedded_typemap(value, parm_sublist, NULL, tm);
Delete(parm_sublist);
}
if (kwtype) {
String *mangle = Swig_string_mangle(kwtype);
Append(value, mangle);
@ -1558,17 +1574,44 @@ String *Swig_typemap_lookup(const_String_or_char_ptr tmap_method, Node *node, co
* If this hash (tm) contains a linked list of parameters under its "kwargs"
* attribute, add keys for each of those named keyword arguments to this
* parameter for later use.
* For example, attach the typemap attributes to p:
* For example, attach the typemap attributes to firstp (first parameter in parameter list):
* %typemap(in, foo="xyz") ...
* A new attribute called "tmap:in:foo" with value "xyz" is attached to p.
* A new attribute called "tmap:in:foo" with value "xyz" is attached to firstp.
* Also expands special variables and special variable macros in the typemap attributes.
* ----------------------------------------------------------------------------- */
static void typemap_attach_kwargs(Hash *tm, const_String_or_char_ptr tmap_method, Parm *p) {
static void typemap_attach_kwargs(Hash *tm, const_String_or_char_ptr tmap_method, Parm *firstp, int nmatch) {
String *temp = NewStringEmpty();
Parm *kw = Getattr(tm, "kwargs");
while (kw) {
String *value = Copy(Getattr(kw, "value"));
String *type = Getattr(kw, "type");
int i;
Parm *p = firstp;
/* Expand special variables */
for (i = 0; i < nmatch; i++) {
SwigType *type = Getattr(p, "type");
String *pname = Getattr(p, "name");
String *lname = Getattr(p, "lname");
SwigType *mtype = Getattr(p, "tmap:match");
SwigType *matchtype = mtype ? mtype : type;
typemap_replace_vars(value, NULL, matchtype, type, pname, lname, i + 1);
p = nextSibling(p);
}
/* Expand special variable macros (embedded typemaps).
* Special variable are expanded first above as they might be used in the special variable macros.
* For example: $typemap(imtype, $2_type). */
p = firstp;
for (i = 0; i < nmatch; i++) {
SwigType *type = Getattr(p, "type");
String *pname = Getattr(p, "name");
String *lname = Getattr(p, "lname");
ParmList *parm_sublist = NewParmWithoutFileLineInfo(type, pname);
Setattr(parm_sublist, "lname", lname);
replace_embedded_typemap(value, parm_sublist, NULL, tm);
p = nextSibling(p);
}
if (type) {
Hash *v = NewHash();
Setattr(v, "type", type);
@ -1578,13 +1621,13 @@ static void typemap_attach_kwargs(Hash *tm, const_String_or_char_ptr tmap_method
}
Clear(temp);
Printf(temp, "%s:%s", tmap_method, Getattr(kw, "name"));
Setattr(p, typemap_method_name(temp), value);
Setattr(firstp, typemap_method_name(temp), value);
Delete(value);
kw = nextSibling(kw);
}
Clear(temp);
Printf(temp, "%s:match_type", tmap_method);
Setattr(p, typemap_method_name(temp), Getattr(tm, "type"));
Setattr(firstp, typemap_method_name(temp), Getattr(tm, "type"));
Delete(temp);
}
@ -1779,7 +1822,7 @@ void Swig_typemap_attach_parms(const_String_or_char_ptr tmap_method, ParmList *p
Setattr(firstp, typemap_method_name(temp), p);
/* Attach kwargs */
typemap_attach_kwargs(tm, tmap_method, firstp);
typemap_attach_kwargs(tm, tmap_method, firstp, nmatch);
/* Replace the argument number */
sprintf(temp, "%d", argnum);