File: template_templated_constructors.i

package info (click to toggle)
swig 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,232 kB
  • sloc: cpp: 54,631; ansic: 29,122; java: 17,530; python: 12,505; cs: 10,369; ruby: 7,232; yacc: 6,477; makefile: 5,965; javascript: 5,520; sh: 5,415; perl: 4,187; php: 3,693; ml: 2,187; lisp: 2,056; tcl: 1,991; xml: 115
file content (95 lines) | stat: -rw-r--r-- 2,439 bytes parent folder | download | duplicates (2)
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
%module template_templated_constructors

%inline %{
namespace ConstructSpace {

class TConstructor1 {
public:
  template<typename T> TConstructor1(T val) {}
  ~TConstructor1() {}
};

class TConstructor2 {
public:
  TConstructor2() {}
  template<typename T> TConstructor2(T val) {}
  ~TConstructor2() {}
};

class TConstructor3 {
public:
  // No implicit default constructor available
  template<typename T> TConstructor3(T val) {}
  ~TConstructor3() {}
};

class TConstructor4 {
public:
  // No constructors available from wrappers when there is no %template to instantiate templated constructor
  template<typename T> TConstructor4() {}
  ~TConstructor4() {}
};

template<typename T> class TClass1 {
public:
  template<typename Y> TClass1(Y t) {}
};

template<typename T> class TClass2 {
public:
  TClass2() {}
  template<typename Y> TClass2(Y t) {}
};

}
%}

%extend ConstructSpace::TConstructor1 {
  %template(TConstructor1) TConstructor1<int>;
}

%template(TConstructor2) ConstructSpace::TConstructor2::TConstructor2<int>;

%template(TClass1Int) ConstructSpace::TClass1<int>;
%extend ConstructSpace::TClass1<int> {
  %template(TClass1Int) TClass1<double>;
}

%template(TClass2Int) ConstructSpace::TClass2<int>;
%extend ConstructSpace::TClass2<int> {
  %template(TClass2Int) TClass2<double>;
}

%inline %{
// Simple version of std::pair
namespace Standard {
  template <class T, class U > struct Pair {
    typedef T first_type;
    typedef U second_type;
    Pair() {}
    Pair(const T& first, const U& second) {}
    Pair(const Pair& other) {}

    template <class U1, class U2> Pair(const Pair< U1, U2 > &otherone) {}
  };
}
%}

%include <std_string.i>

namespace Standard {
  %template(StringPair) Pair<std::string, std::string>;
  %template(ShortPair) Pair<short, short>;
  %template(IntPair) Pair<int, int>;
  %template(DoublePair) Pair<double, double>;
  %extend Pair<int, int> {
    // Templated constructor which uses 'correct' name of the containing class (IntPair)
    %template(IntPair) Pair<short, short>;
    // Templated constructors that behave differently in different languages as the template name
    // does not match IntPair, the instantiated name for Pair<int, int>.
    // Some languages wrap as a factory style function (Python), 
    // others ignore the name and wrap as regular constructor (Java).
    %template(Pair) Pair<double, double>;
    %template(MakeStringPair) Pair<std::string, std::string>;
  }
}