Fix functors (wrapped as __call__) when using -builtin -modern -fastunpack.
This commit is contained in:
parent
bf85b6f7a9
commit
c6547ac44e
5 changed files with 57 additions and 1 deletions
33
Examples/test-suite/functors.i
Normal file
33
Examples/test-suite/functors.i
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
%module functors
|
||||
|
||||
// Rename operator() only if the language does not already do this by default
|
||||
#if defined(SWIGCSHARP) || defined(SWIGGO) || defined(SWIGGUILE) || defined(SWIGJAVA) || defined(SWIGJAVASCRIPT) || defined(SWIGPHP) || defined(SWIGSCILAB) || defined(SWIGTCL)
|
||||
%rename(Funktor) operator();
|
||||
#endif
|
||||
|
||||
%inline %{
|
||||
class Functor0 {
|
||||
int num;
|
||||
public:
|
||||
Functor0(int num) : num(num) {}
|
||||
int operator()() { return -num; }
|
||||
int operate() { return this->operator()(); }
|
||||
};
|
||||
|
||||
class Functor1 {
|
||||
int num;
|
||||
public:
|
||||
Functor1(int num) : num(num) {}
|
||||
int operator()(int x) { return num + x; }
|
||||
int operate(int x) { return this->operator()(x); }
|
||||
};
|
||||
|
||||
class Functor2 {
|
||||
int num;
|
||||
public:
|
||||
Functor2(int num) : num(num) {}
|
||||
int operator()(int x, int x2) { return num + x + x2; }
|
||||
int operate(int x, int x2) { return this->operator()(x, x2); }
|
||||
};
|
||||
%}
|
||||
|
||||
12
Examples/test-suite/python/functors_runme.py
Normal file
12
Examples/test-suite/python/functors_runme.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from functors import *
|
||||
|
||||
a = Functor0(10)
|
||||
b = Functor1(10)
|
||||
c = Functor2(10)
|
||||
|
||||
if a()!=-10:
|
||||
raise RuntimeError("a failed")
|
||||
if b(1)!=11:
|
||||
raise RuntimeError("b failed")
|
||||
if c(1, 2)!=13:
|
||||
raise RuntimeError("c failed")
|
||||
|
|
@ -75,3 +75,10 @@ if not -a==a:
|
|||
if not -b==Op(-5):
|
||||
raise RuntimeError("-b==Op(-5)")
|
||||
|
||||
# test functors
|
||||
if not b()==5:
|
||||
raise RuntimeError("functor")
|
||||
if not b(1)==6:
|
||||
raise RuntimeError("functor")
|
||||
if not b(1, 2)==8:
|
||||
raise RuntimeError("functor")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue