PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Swig | 126 lines | 101 code | 25 blank | 0 comment | 0 complexity | 286546e3de863b9b92a14f82a7a23cfa MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. %module template_default_arg
  2. %warnfilter(SWIGWARN_RUBY_WRONG_NAME) Hello; /* Ruby, wrong class name */
  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) X;
  6. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) Z;
  7. %warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) meth;
  8. #endif
  9. %inline %{
  10. template <class T>
  11. struct Foo
  12. {
  13. typedef unsigned int size_type;
  14. Foo(size_type n = size_type(0) ) { }
  15. };
  16. int foob(Foo<int> h = Foo<int>()) {return 1; }
  17. template <class T>
  18. struct Hello
  19. {
  20. typedef unsigned int size_type;
  21. // This works
  22. // Hello(size_type n = Hello<T>::size_type(0) ) { }
  23. // This doesn't
  24. Hello(size_type n = size_type(0) ) { }
  25. enum Hi { hi, hello };
  26. void foo(Hi h = hi) { }
  27. };
  28. template <typename T> struct X {
  29. X(const T& t = T()) {}
  30. X(double a, const T& t = T(0)) {}
  31. T meth(double a, const T& t = T(0)) { return t; }
  32. const T& meth(const T& t = T(0)) { return t; }
  33. };
  34. template <typename TT> class Y : private X<TT> {
  35. public:
  36. // test using on templated class with default args in the method
  37. using X<TT>::meth;
  38. };
  39. template <int V> struct Z
  40. {
  41. Z(int t = V) {}
  42. // and also:
  43. Z(double a, int t = V){}
  44. };
  45. %}
  46. %template(Foo_int) Foo<int>;
  47. %template(Hello_int) Hello<int>;
  48. %template(X_int) X<int>;
  49. %template(X_longlong) X<long long>;
  50. %template(X_unsigned) X<unsigned>;
  51. %template(Y_unsigned) Y<unsigned>;
  52. %template(X_hello_unsigned) X<Hello<int> >;
  53. %template(Y_hello_unsigned) Y<Hello<int> >;
  54. %template(X_Foo_Foo_int) X<Foo<Foo<int> > >;
  55. %template(Z_8) Z<8>;
  56. %template(Foo_Z_8) Foo<Z<8> >;
  57. %template(X_Foo_Z_8) X<Foo<Z<8> > >;
  58. %inline %{
  59. struct Bar : Hello<int>
  60. {
  61. Bar(size_type n) : Hello<int>(n)
  62. {
  63. }
  64. };
  65. %}
  66. // Templated functions
  67. %inline %{
  68. // Templated methods which are overloaded and have default args, and %template which
  69. // uses the same name as the C++ functions and overload on the template parameters and
  70. // specialization thrown in too. Wow, SWIG can handle this insane stuff!
  71. template<typename T, typename U> int ott(T t = 0, const U& u = U()) { return 10; }
  72. template<typename T, typename U> int ott(const char *msg, T t = 0, const U& u = U()) { return 20; }
  73. int ott(Foo<int>) { return 30; }
  74. template<typename T> int ott(Hello<int> h, T t = 0) { return 40; }
  75. template<> int ott<int>(Hello<int> h, int t) { return 50; }
  76. template<> int ott(Hello<int> h, double t) { return 60; }
  77. %}
  78. %template(ott) ott<int, int>;
  79. %template(ott) ott<double>;
  80. %template(ottint) ott<int>; // default arg requires a rename
  81. %template(ottstring) ott<const char *>; // default arg requires a rename
  82. // Above test in namespaces
  83. %inline %{
  84. namespace OuterSpace {
  85. namespace InnerSpace {
  86. // Templated methods which are overloaded and have default args, and %template which
  87. // uses the same name as the C++ functions and overload on the template parameters and
  88. // specialization thrown in too. Wow, SWIG can handle this insane stuff!
  89. template<typename T, typename U> int nsott(T t = 0, const U& u = U()) { return 110; }
  90. template<typename T, typename U> int nsott(const char *msg, T t = 0, const U& u = U()) { return 120; }
  91. int nsott(Foo<int>) { return 130; }
  92. template<typename T> int nsott(Hello<int> h, T t = 0) { return 140; }
  93. template<> int nsott<int>(Hello<int> h, int t) { return 150; }
  94. template<> int nsott(Hello<int> h, double t) { return 160; }
  95. }
  96. }
  97. %}
  98. %template(nsott) OuterSpace::InnerSpace::nsott<int, int>;
  99. %template(nsott) OuterSpace::InnerSpace::nsott<double>;
  100. %template(nsottint) OuterSpace::InnerSpace::nsott<int>; // default arg requires a rename
  101. %template(nsottstring) OuterSpace::InnerSpace::nsott<const char *>; // default arg requires a rename