summaryrefslogtreecommitdiff
path: root/Examples/test-suite/cpp11_type_aliasing.i
blob: 8bef712476a037db53a36c394dd01477975874cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
%module cpp11_type_aliasing

// Type aliasing seg fault : Github issue #424

%inline %{
namespace Halide {

struct Target {
  int bits;
  Target(int bits=32) : bits(bits) {}
};

class NamesInterface {
public:
  using Target = Halide::Target;
};

Target get_host_target() { 
  return Target();
}

namespace Internal {

template <typename T> class GeneratorParam {
  T value;
public:
  GeneratorParam(const char *name, const T &v) : value(v) {}

  T getValue() { 
    return value;
  }
};

class GeneratorBase : public NamesInterface {
public:
  GeneratorParam<Target> target{ "target", Halide::get_host_target() };
};

}
}
%}

%template(Halide_Target) Halide::Internal::GeneratorParam<Halide::Target>;


%inline %{
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

%{
Int mult2(Int x) { return x * 2; }
%}
%callback("%s_cb");
Int mult2(Int x);
%nocallback;

%inline %{
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 funk, int param) { return funk(param); }
%}


// Template template parameters - from #1021
%inline %{
#include <type_traits>

class Node {};
struct AnyVal { typedef AnyVal Super; };

template<template<typename D, typename O> class C, typename T, typename Super, typename Root, typename O>
  using DeriveToBase = typename std::conditional<std::is_same<T, AnyVal>::value, Root, C<Super, O> >::type;

template<class T, class Root, class RParent>
  using ImmediateBase = typename std::conditional<std::is_same<T, AnyVal>::value, Root, RParent >::type;

template<class D, typename _Super=AnyVal> class Expression {
  typedef _Super Super;
};

void TestInstantiationsPart4() {
  Expression<AnyVal, AnyVal::Super> express;
  DeriveToBase<Expression, AnyVal, AnyVal, AnyVal, AnyVal> derive_to_base = AnyVal();
}
%}

#if 0
// TODO define and instantiate std::conditional and std::is_same
%template(ExpressionInstantiation) Expression<AnyVal, AnyVal::Super>;
%template(AnyTypeInstantiation) DeriveToBase<Expression, AnyVal, AnyVal, AnyVal, AnyVal>;

%inline %{
AnyVal takeAnyVal(DeriveToBase<Expression, AnyVal, AnyVal, AnyVal, AnyVal> av) {
  return av;
}
%}
#endif