Added support for type alias
This commit is contained in:
parent
86fd5c9858
commit
c363a93d69
5 changed files with 121 additions and 53 deletions
|
|
@ -2,15 +2,6 @@
|
|||
|
||||
// Type aliasing seg fault : Github issue #424
|
||||
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) Target;
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) Int;
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntRef;
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntPtrRef;
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntRValueRef;
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) IntArray;
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) HalideTargetPtr1;
|
||||
%warnfilter(SWIGWARN_CPP11_ALIAS_DECLARATION) HalideTargetPtr2;
|
||||
|
||||
%inline %{
|
||||
namespace Halide {
|
||||
|
||||
|
|
@ -53,14 +44,69 @@ public:
|
|||
|
||||
|
||||
%inline %{
|
||||
using Int = int;
|
||||
using IntRef = int&;
|
||||
using IntPtrRef = int*&;
|
||||
using IntRValueRef = int&&;
|
||||
using IntArray = int[];
|
||||
|
||||
using HalideTargetPtr1 = Halide::Target*;
|
||||
namespace Halide {
|
||||
using HalideTargetPtr2 = Target*;
|
||||
}
|
||||
%}
|
||||
|
||||
// Define some types
|
||||
|
||||
%inline %{
|
||||
using Int = int;
|
||||
using IntPtr = Int*;
|
||||
using IntRef = Int&;
|
||||
using IntPtrRef = Int*&;
|
||||
using IntRValueRef = Int&&;
|
||||
using IntArray = Int[];
|
||||
%}
|
||||
|
||||
// Test that SWIG understands these new types
|
||||
|
||||
%callback("%s_cb");
|
||||
Int mult2(Int x);
|
||||
%nocallback;
|
||||
|
||||
%inline %{
|
||||
Int mult2(Int x) { return x * 2; }
|
||||
IntPtr allocate_int() { return new Int(12); }
|
||||
void free_int(int* ptr) { delete ptr; }
|
||||
void inplace_mult2(IntRef x) { x *= 2; }
|
||||
Int read_int(IntPtr ptr) { return *ptr; }
|
||||
|
||||
template <typename T> class Pair {
|
||||
public:
|
||||
using data_t = T;
|
||||
|
||||
data_t a, b;
|
||||
|
||||
Pair() : a(), b() { }
|
||||
Pair(data_t a, data_t b) : a(a), b(b) { }
|
||||
data_t first() { return a; }
|
||||
data_t second() { return b; }
|
||||
};
|
||||
%}
|
||||
|
||||
%template(int_pair) Pair<Int>;
|
||||
|
||||
%inline %{
|
||||
using PairInt = Pair<Int>;
|
||||
|
||||
class PairSubclass : public PairInt {
|
||||
public:
|
||||
PairSubclass(data_t a, data_t b) : PairInt(a, b) { }
|
||||
|
||||
using const_ref_data_t = const data_t&;
|
||||
};
|
||||
|
||||
PairSubclass::data_t plus1(PairSubclass::const_ref_data_t x) { return x + 1; }
|
||||
%}
|
||||
|
||||
// Test function pointers
|
||||
|
||||
%inline %{
|
||||
using callback_t = int(*)(int);
|
||||
|
||||
callback_t get_callback() { return mult2; }
|
||||
int call(callback_t func, int param) { return func(param); }
|
||||
%}
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
cpp_using_type_aliasing.i:8: Warning 341: The 'using' keyword in type aliasing is not fully supported yet.
|
||||
cpp_using_type_aliasing.i:8: Warning 315: Nothing known about 'Okay< int >'.
|
||||
cpp_using_type_aliasing.i:8: Warning 315: Nothing known about 'Okay< int >'.
|
||||
32
Examples/test-suite/python/cpp11_type_aliasing_runme.py
Normal file
32
Examples/test-suite/python/cpp11_type_aliasing_runme.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from cpp11_type_aliasing import *
|
||||
|
||||
if get_host_target().bits != 32:
|
||||
raise RuntimeError("get_host_target().bits should return 32")
|
||||
|
||||
if mult2(10) != 20:
|
||||
raise RuntimeError("mult2(10) should return 20")
|
||||
|
||||
int_ptr = allocate_int()
|
||||
inplace_mult2(int_ptr)
|
||||
if read_int(int_ptr) != 24:
|
||||
raise RuntimeError("read_int should return 24")
|
||||
free_int(int_ptr)
|
||||
|
||||
pair = PairSubclass(3, 4)
|
||||
if pair.first() != 3:
|
||||
raise RuntimeError("pair.first() should return 3")
|
||||
|
||||
if pair.second() != 4:
|
||||
raise RuntimeError("pair.second() should return 4")
|
||||
|
||||
if pair.a != 3:
|
||||
raise RuntimeError("pair.a should be 3")
|
||||
|
||||
if plus1(5) != 6:
|
||||
raise RuntimeError("plus1(5) should return 6")
|
||||
|
||||
if call(mult2_cb, 7) != 14:
|
||||
raise RuntimeError("call(mult2_cb, 7) should return 14")
|
||||
|
||||
if call(get_callback(), 7) != 14:
|
||||
raise RuntimeError("call(get_callback(), 7) should return 14")
|
||||
Loading…
Add table
Add a link
Reference in a new issue