PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/test-suite/overload_template.i

#
Swig | 210 lines | 165 code | 45 blank | 0 comment | 0 complexity | ed4b427caf80a8dd3d3084c61afb901a MD5 | raw file
  1%module overload_template
  2
  3#ifdef SWIGLUA
  4// lua only has one numeric type, so most of the overloads shadow each other creating warnings
  5%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) foo;
  6%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) maximum;
  7%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) specialization;
  8%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) overload;
  9%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) space::nsoverload;
 10%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) fooT;
 11%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) barT;
 12#endif
 13
 14%inline %{
 15
 16int foo() {
 17  return 3;
 18}
 19
 20template <class T>
 21  int foo(T x) {
 22     return (int)x;
 23  }
 24
 25template<class T>
 26  T maximum(T a, T b) { return  (a > b) ? a : b; }
 27%}                                     
 28
 29
 30%template(foo) foo<int>;
 31%template(foo) foo<double>;
 32
 33%template(maximum) maximum<int>;
 34%template(maximum) maximum<double>;
 35
 36// Mix template overloading with plain function overload
 37// Mix 1
 38%inline %{
 39  int mix1(const char* msg) { return 101; }
 40  template<typename T> int mix1(T t, const T& tt) { return 102; }
 41  template<typename T> int mix1(T t) { return 103; }
 42%}
 43%template(mix1) mix1<double>;
 44
 45// Mix 2
 46%inline %{
 47  template<typename T> int mix2(T t, const T& tt) { return 102; }
 48  int mix2(const char* msg) { return 101; }
 49  template<typename T> int mix2(T t) { return 103; }
 50%}
 51%template(mix2) mix2<double>;
 52
 53// Mix 3
 54%inline %{
 55  template<typename T> int mix3(T t, const T& tt) { return 102; }
 56  template<typename T> int mix3(T t) { return 103; }
 57  int mix3(const char* msg) { return 101; }
 58%}
 59%template(mix3) mix3<double>;
 60
 61
 62// overloaded by number of templated parameters
 63// Combination 1
 64%inline %{
 65template<typename T> int overtparams1(T t) { return 10; }
 66template<typename T, typename U> int overtparams1(T t, U u) { return 20; }
 67%}
 68
 69%template(overtparams1) overtparams1<int>;
 70%template(overtparams1) overtparams1<double, int>;
 71
 72
 73// Combination 2
 74%inline %{
 75template<typename T> int overtparams2(T t) { return 30; }
 76template<typename T, typename U> int overtparams2(T t, U u) { return 40; }
 77%}
 78
 79%template(overtparams2) overtparams2<double, int>;
 80
 81
 82// Combination 3
 83%inline %{
 84template<typename T> int overloaded(T t) { return 50; }
 85int overloaded() { return 60; }
 86template<typename T, typename U> int overloaded(T t, U u) { return 70; }
 87%}
 88
 89%template(overloaded) overloaded<double, int>;
 90
 91// Combination 4
 92%inline %{
 93int overloadedagain(const char* msg) { return 80; }
 94template<typename T> int overloadedagain() { return 90; }
 95template<typename T, typename U> int overloadedagain(T t, U u) { return 100; }
 96%}
 97
 98%template(overloadedagain) overloadedagain<double>;
 99
100// simple specialization
101%inline %{
102template<typename T> void xyz() {}
103template<> void xyz<double>() {}
104void xyz() {}
105%}
106
107// We can have xyz(); xyz<double>(); xyz<int>(); in C++, but can't have this type of overloading in target language, so we need to do some renaming
108%template(xyz_double) xyz<double>;
109%template(xyz_int) xyz<int>;
110
111
112// specializations
113%inline %{
114template<typename T> int specialization(T t) { return 200; }
115template<typename T, typename U> int specialization(T t, U u) { return 201; }
116template<> int specialization(int t) { return 202; }
117template<> int specialization<double>(double t) { return 203; }
118template<> int specialization(int t, int u) { return 204; }
119template<> int specialization<double,double>(double t, double u) { return 205; }
120%}
121
122%template(specialization) specialization<int>;
123%template(specialization) specialization<double>;
124%template(specialization) specialization<int, int>;
125%template(specialization) specialization<double, double>;
126%template(specialization) specialization<const char *, const char *>;
127
128
129// a bit of everything
130%inline %{
131int overload(const char *c) { return 0; }
132template<typename T> int overload(T t) { return 10; }
133template<typename T> int overload(T t, const T &tref) { return 20; }
134template<typename T> int overload(T t, const char *c) { return 30; }
135template<> int overload<double>(double t, const char *c) { return 40; }
136int overload() { return 50; }
137
138class Klass {};
139%}
140
141%template(overload) overload<int>;
142%template(overload) overload<Klass>;
143%template(overload) overload<double>;
144
145
146// everything put in a namespace
147%inline %{
148namespace space {
149  int nsoverload(const char *c) { return 1000; }
150  template<typename T> int nsoverload(T t) { return 1010; }
151  template<typename T> int nsoverload(T t, const T &tref) { return 1020; }
152  template<typename T> int nsoverload(T t, const char *c) { return 1030; }
153  template<> int nsoverload<double>(double t, const char *c) { return 1040; }
154  int nsoverload() { return 1050; }
155}
156%}
157
158%template(nsoverload) space::nsoverload<int>;
159%template(nsoverload) space::nsoverload<Klass>;
160%template(nsoverload) space::nsoverload<double>;
161
162
163%inline %{
164  namespace space 
165  {
166    template <class T>
167    struct Foo 
168    {
169      void bar(T t1) { }
170      void bar(T t1, T t2) { }
171      void bar(int a, int b, int c) { }
172    };
173    struct A
174    {
175      template <class Y>
176      static void fooT(Y y) { }
177
178    };
179
180  }
181  template <class T>
182    struct Bar
183    {
184      void foo(T t1) { }
185      void foo(T t1, T t2) { }
186      void foo(int a, int b, int c) { }
187      template <class Y>
188      void fooT(Y y) { }
189    };
190
191
192  struct B
193  {
194    template <class Y>
195    void barT(Y y) { }
196    
197  };
198  
199%}
200
201
202%template(Bar_d) Bar<double>;
203%template(Foo_d) space::Foo<double>;
204%template(foo) space::A::fooT<double>;
205%template(foo) space::A::fooT<int>;
206%template(foo) space::A::fooT<char>;
207
208%template(foo) B::barT<double>;
209%template(foo) B::barT<int>;
210%template(foo) B::barT<char>;