PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. %module overload_template
  2. #ifdef SWIGLUA
  3. // lua only has one numeric type, so most of the overloads shadow each other creating warnings
  4. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) foo;
  5. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) maximum;
  6. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) specialization;
  7. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) overload;
  8. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) space::nsoverload;
  9. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) fooT;
  10. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) barT;
  11. #endif
  12. %inline %{
  13. int foo() {
  14. return 3;
  15. }
  16. template <class T>
  17. int foo(T x) {
  18. return (int)x;
  19. }
  20. template<class T>
  21. T maximum(T a, T b) { return (a > b) ? a : b; }
  22. %}
  23. %template(foo) foo<int>;
  24. %template(foo) foo<double>;
  25. %template(maximum) maximum<int>;
  26. %template(maximum) maximum<double>;
  27. // Mix template overloading with plain function overload
  28. // Mix 1
  29. %inline %{
  30. int mix1(const char* msg) { return 101; }
  31. template<typename T> int mix1(T t, const T& tt) { return 102; }
  32. template<typename T> int mix1(T t) { return 103; }
  33. %}
  34. %template(mix1) mix1<double>;
  35. // Mix 2
  36. %inline %{
  37. template<typename T> int mix2(T t, const T& tt) { return 102; }
  38. int mix2(const char* msg) { return 101; }
  39. template<typename T> int mix2(T t) { return 103; }
  40. %}
  41. %template(mix2) mix2<double>;
  42. // Mix 3
  43. %inline %{
  44. template<typename T> int mix3(T t, const T& tt) { return 102; }
  45. template<typename T> int mix3(T t) { return 103; }
  46. int mix3(const char* msg) { return 101; }
  47. %}
  48. %template(mix3) mix3<double>;
  49. // overloaded by number of templated parameters
  50. // Combination 1
  51. %inline %{
  52. template<typename T> int overtparams1(T t) { return 10; }
  53. template<typename T, typename U> int overtparams1(T t, U u) { return 20; }
  54. %}
  55. %template(overtparams1) overtparams1<int>;
  56. %template(overtparams1) overtparams1<double, int>;
  57. // Combination 2
  58. %inline %{
  59. template<typename T> int overtparams2(T t) { return 30; }
  60. template<typename T, typename U> int overtparams2(T t, U u) { return 40; }
  61. %}
  62. %template(overtparams2) overtparams2<double, int>;
  63. // Combination 3
  64. %inline %{
  65. template<typename T> int overloaded(T t) { return 50; }
  66. int overloaded() { return 60; }
  67. template<typename T, typename U> int overloaded(T t, U u) { return 70; }
  68. %}
  69. %template(overloaded) overloaded<double, int>;
  70. // Combination 4
  71. %inline %{
  72. int overloadedagain(const char* msg) { return 80; }
  73. template<typename T> int overloadedagain() { return 90; }
  74. template<typename T, typename U> int overloadedagain(T t, U u) { return 100; }
  75. %}
  76. %template(overloadedagain) overloadedagain<double>;
  77. // simple specialization
  78. %inline %{
  79. template<typename T> void xyz() {}
  80. template<> void xyz<double>() {}
  81. void xyz() {}
  82. %}
  83. // 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
  84. %template(xyz_double) xyz<double>;
  85. %template(xyz_int) xyz<int>;
  86. // specializations
  87. %inline %{
  88. template<typename T> int specialization(T t) { return 200; }
  89. template<typename T, typename U> int specialization(T t, U u) { return 201; }
  90. template<> int specialization(int t) { return 202; }
  91. template<> int specialization<double>(double t) { return 203; }
  92. template<> int specialization(int t, int u) { return 204; }
  93. template<> int specialization<double,double>(double t, double u) { return 205; }
  94. %}
  95. %template(specialization) specialization<int>;
  96. %template(specialization) specialization<double>;
  97. %template(specialization) specialization<int, int>;
  98. %template(specialization) specialization<double, double>;
  99. %template(specialization) specialization<const char *, const char *>;
  100. // a bit of everything
  101. %inline %{
  102. int overload(const char *c) { return 0; }
  103. template<typename T> int overload(T t) { return 10; }
  104. template<typename T> int overload(T t, const T &tref) { return 20; }
  105. template<typename T> int overload(T t, const char *c) { return 30; }
  106. template<> int overload<double>(double t, const char *c) { return 40; }
  107. int overload() { return 50; }
  108. class Klass {};
  109. %}
  110. %template(overload) overload<int>;
  111. %template(overload) overload<Klass>;
  112. %template(overload) overload<double>;
  113. // everything put in a namespace
  114. %inline %{
  115. namespace space {
  116. int nsoverload(const char *c) { return 1000; }
  117. template<typename T> int nsoverload(T t) { return 1010; }
  118. template<typename T> int nsoverload(T t, const T &tref) { return 1020; }
  119. template<typename T> int nsoverload(T t, const char *c) { return 1030; }
  120. template<> int nsoverload<double>(double t, const char *c) { return 1040; }
  121. int nsoverload() { return 1050; }
  122. }
  123. %}
  124. %template(nsoverload) space::nsoverload<int>;
  125. %template(nsoverload) space::nsoverload<Klass>;
  126. %template(nsoverload) space::nsoverload<double>;
  127. %inline %{
  128. namespace space
  129. {
  130. template <class T>
  131. struct Foo
  132. {
  133. void bar(T t1) { }
  134. void bar(T t1, T t2) { }
  135. void bar(int a, int b, int c) { }
  136. };
  137. struct A
  138. {
  139. template <class Y>
  140. static void fooT(Y y) { }
  141. };
  142. }
  143. template <class T>
  144. struct Bar
  145. {
  146. void foo(T t1) { }
  147. void foo(T t1, T t2) { }
  148. void foo(int a, int b, int c) { }
  149. template <class Y>
  150. void fooT(Y y) { }
  151. };
  152. struct B
  153. {
  154. template <class Y>
  155. void barT(Y y) { }
  156. };
  157. %}
  158. %template(Bar_d) Bar<double>;
  159. %template(Foo_d) space::Foo<double>;
  160. %template(foo) space::A::fooT<double>;
  161. %template(foo) space::A::fooT<int>;
  162. %template(foo) space::A::fooT<char>;
  163. %template(foo) B::barT<double>;
  164. %template(foo) B::barT<int>;
  165. %template(foo) B::barT<char>;