PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms 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
  1%module template_default_arg
  2
  3%warnfilter(SWIGWARN_RUBY_WRONG_NAME) Hello;	/* Ruby, wrong class name */
  4#ifdef SWIGLUA
  5// lua only has one numeric type, so most of the overloads shadow each other creating warnings
  6%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) X;
  7%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) Z;
  8%warnfilter(SWIGWARN_LANG_OVERLOAD_SHADOW) meth;
  9#endif
 10
 11%inline %{
 12  template <class T>
 13    struct Foo 
 14    {
 15      typedef unsigned int size_type;
 16      Foo(size_type n = size_type(0) ) { }
 17    };
 18  
 19  int foob(Foo<int> h = Foo<int>()) {return 1; }
 20
 21  template <class T>
 22    struct Hello
 23    {
 24      typedef unsigned int size_type;
 25
 26      // This works
 27      // Hello(size_type n = Hello<T>::size_type(0) ) { }
 28
 29      // This doesn't
 30      Hello(size_type n = size_type(0) ) { }
 31
 32      enum Hi { hi, hello };
 33
 34      void foo(Hi h = hi) { }
 35    };
 36
 37  template <typename T> struct X {
 38      X(const T& t = T()) {}
 39      X(double a, const T& t = T(0)) {}
 40      T meth(double a, const T& t = T(0)) { return t; }
 41      const T& meth(const T& t = T(0)) { return t; }
 42    };
 43
 44  template <typename TT> class Y : private X<TT> {
 45  public:
 46    // test using on templated class with default args in the method
 47    using X<TT>::meth;
 48  };
 49
 50  template <int V> struct Z 
 51  {
 52    Z(int t = V) {}    
 53    // and also:
 54    Z(double a, int t = V){}
 55  };
 56  
 57  
 58%}
 59
 60%template(Foo_int) Foo<int>;
 61%template(Hello_int) Hello<int>;
 62%template(X_int) X<int>;
 63%template(X_longlong) X<long long>;
 64%template(X_unsigned) X<unsigned>;
 65%template(Y_unsigned) Y<unsigned>;
 66
 67%template(X_hello_unsigned) X<Hello<int> >;
 68%template(Y_hello_unsigned) Y<Hello<int> >;
 69%template(X_Foo_Foo_int) X<Foo<Foo<int> > >;
 70%template(Z_8) Z<8>;
 71%template(Foo_Z_8) Foo<Z<8> >;
 72%template(X_Foo_Z_8) X<Foo<Z<8> > >;
 73
 74%inline %{
 75
 76  struct Bar : Hello<int>
 77  {
 78    Bar(size_type n) : Hello<int>(n)
 79    {
 80    }
 81    
 82  };
 83%}
 84
 85
 86// Templated functions
 87%inline %{
 88  // Templated methods which are overloaded and have default args, and %template which
 89  // uses the same name as the C++ functions and overload on the template parameters and
 90  // specialization thrown in too. Wow, SWIG can handle this insane stuff!
 91  template<typename T, typename U> int ott(T t = 0, const U& u = U()) { return 10; }
 92  template<typename T, typename U> int ott(const char *msg, T t = 0, const U& u = U()) { return 20; }
 93  int ott(Foo<int>) { return 30; }
 94  template<typename T> int ott(Hello<int> h, T t = 0) { return 40; }
 95  template<> int ott<int>(Hello<int> h, int t) { return 50; }
 96  template<> int ott(Hello<int> h, double t) { return 60; }
 97%}
 98
 99%template(ott) ott<int, int>;
100%template(ott) ott<double>;
101%template(ottint) ott<int>; // default arg requires a rename
102%template(ottstring) ott<const char *>; // default arg requires a rename
103
104
105// Above test in namespaces
106%inline %{
107namespace OuterSpace {
108  namespace InnerSpace {
109    // Templated methods which are overloaded and have default args, and %template which
110    // uses the same name as the C++ functions and overload on the template parameters and
111    // specialization thrown in too. Wow, SWIG can handle this insane stuff!
112    template<typename T, typename U> int nsott(T t = 0, const U& u = U()) { return 110; }
113    template<typename T, typename U> int nsott(const char *msg, T t = 0, const U& u = U()) { return 120; }
114    int nsott(Foo<int>) { return 130; }
115    template<typename T> int nsott(Hello<int> h, T t = 0) { return 140; }
116    template<> int nsott<int>(Hello<int> h, int t) { return 150; }
117    template<> int nsott(Hello<int> h, double t) { return 160; }
118  }
119}
120%}
121
122%template(nsott) OuterSpace::InnerSpace::nsott<int, int>;
123%template(nsott) OuterSpace::InnerSpace::nsott<double>;
124%template(nsottint) OuterSpace::InnerSpace::nsott<int>; // default arg requires a rename
125%template(nsottstring) OuterSpace::InnerSpace::nsott<const char *>; // default arg requires a rename
126