Fix wrapping of function pointers and member function pointers when the function returns by reference

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12157 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2010-07-16 18:45:22 +00:00
commit 568607c5ea
7 changed files with 67 additions and 4 deletions

View file

@ -5,6 +5,10 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 2.0.1 (in progress) Version 2.0.1 (in progress)
=========================== ===========================
2010-07-16: wsfulton
Fix wrapping of function pointers and member function pointers when the function
returns by reference.
2010-07-08: wsfulton 2010-07-08: wsfulton
Fix #3024875 - shared_ptr of classes with non-public destructors. This also fixes Fix #3024875 - shared_ptr of classes with non-public destructors. This also fixes
the "unref" feature when used on classes with non-public destructors. the "unref" feature when used on classes with non-public destructors.

View file

@ -201,6 +201,7 @@ CPP_TEST_CASES += \
features \ features \
fragments \ fragments \
friends \ friends \
funcptr_cpp \
fvirtual \ fvirtual \
global_namespace \ global_namespace \
global_ns_arg \ global_ns_arg \

View file

@ -0,0 +1,16 @@
%module funcptr_cpp
%inline %{
int addByValue(const int &a, int b) { return a+b; }
int * addByPointer(const int &a, int b) { static int val; val = a+b; return &val; }
int & addByReference(const int &a, int b) { static int val; val = a+b; return val; }
int call1(int (*d)(const int &, int), int a, int b) { return d(a, b); }
int call2(int * (*d)(const int &, int), int a, int b) { return *d(a, b); }
int call3(int & (*d)(const int &, int), int a, int b) { return d(a, b); }
%}
%constant int (*ADD_BY_VALUE)(const int &, int) = addByValue;
%constant int * (*ADD_BY_POINTER)(const int &, int) = addByPointer;
%constant int & (*ADD_BY_REFERENCE)(const int &, int) = addByReference;

View file

@ -99,3 +99,29 @@ double (Shape::*perimetervar)(void) = &Shape::perimeter;
%constant double (Shape::*PERIMPT)(void) = &Shape::perimeter; %constant double (Shape::*PERIMPT)(void) = &Shape::perimeter;
%constant double (Shape::*NULLPT)(void) = 0; %constant double (Shape::*NULLPT)(void) = 0;
/*
%inline %{
struct Funktions {
void retByRef(int & (*d)(double)) {}
};
void byRef(int & (Funktions::*d)(double)) {}
%}
*/
%inline %{
struct Funktions {
int addByValue(const int &a, int b) { return a+b; }
int * addByPointer(const int &a, int b) { static int val; val = a+b; return &val; }
int & addByReference(const int &a, int b) { static int val; val = a+b; return val; }
};
int call1(int (Funktions::*d)(const int &, int), int a, int b) { Funktions f; return (f.*d)(a, b); }
int call2(int * (Funktions::*d)(const int &, int), int a, int b) { Funktions f; return *(f.*d)(a, b); }
int call3(int & (Funktions::*d)(const int &, int), int a, int b) { Funktions f; return (f.*d)(a, b); }
%}
%constant int (Funktions::*ADD_BY_VALUE)(const int &, int) = &Funktions::addByValue;
%constant int * (Funktions::*ADD_BY_POINTER)(const int &, int) = &Funktions::addByPointer;
%constant int & (Funktions::*ADD_BY_REFERENCE)(const int &, int) = &Funktions::addByReference;

View file

@ -0,0 +1,9 @@
from funcptr_cpp import *
if call1(ADD_BY_VALUE, 10, 11) != 21:
raise RuntimeError
if call2(ADD_BY_POINTER, 12, 13) != 25:
raise RuntimeError
if call3(ADD_BY_REFERENCE, 14, 15) != 29:
raise RuntimeError

View file

@ -41,3 +41,6 @@ memberPtr = NULLPT
check ("Square area ", 100.0, do_op(s,AREAPT)) check ("Square area ", 100.0, do_op(s,AREAPT))
check ("Square perim", 40.0, do_op(s,PERIMPT)) check ("Square perim", 40.0, do_op(s,PERIMPT))
check ("Add by value", 3, call1(ADD_BY_VALUE, 1, 2))
check ("Add by pointer", 7, call2(ADD_BY_POINTER, 3, 4))
check ("Add by reference", 11, call3(ADD_BY_REFERENCE, 5, 6))

View file

@ -735,6 +735,7 @@ String *SwigType_rcaststr(SwigType *s, const_String_or_char_ptr name) {
int clear = 1; int clear = 1;
int firstarray = 1; int firstarray = 1;
int isreference = 0; int isreference = 0;
int isfunction = 0;
int isarray = 0; int isarray = 0;
result = NewStringEmpty(); result = NewStringEmpty();
@ -835,6 +836,7 @@ String *SwigType_rcaststr(SwigType *s, const_String_or_char_ptr name) {
} }
Append(result, ")"); Append(result, ")");
Delete(parms); Delete(parms);
isfunction = 1;
} else { } else {
String *bs = SwigType_namestr(element); String *bs = SwigType_namestr(element);
Insert(result, 0, " "); Insert(result, 0, " ");
@ -850,10 +852,12 @@ String *SwigType_rcaststr(SwigType *s, const_String_or_char_ptr name) {
cast = NewStringf("(%s)", result); cast = NewStringf("(%s)", result);
} }
if (name) { if (name) {
if (isreference) { if (!isfunction) {
if (isarray) if (isreference) {
Clear(cast); if (isarray)
Append(cast, "*"); Clear(cast);
Append(cast, "*");
}
} }
Append(cast, name); Append(cast, name);
} }