PageRenderTime 28ms CodeModel.GetById 18ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Swig | 243 lines | 200 code | 43 blank | 0 comment | 0 complexity | 4efb149d7d2549e0ba859ec141076670 MD5 | raw file
  1// Lots of tests for methods with default parameters / default arguments
  2
  3%module default_args
  4
  5%{
  6#if defined(_MSC_VER)
  7  #pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
  8#endif
  9%}
 10
 11%include <std_string.i>
 12
 13%inline %{
 14  #include <string>
 15
 16  // Anonymous arguments
 17  int anonymous(int = 7771);
 18  int anonymous(int x) { return x; }
 19
 20  // Bug [548272] Default arguments
 21  bool booltest(bool x = true) { return x; }
 22
 23  // scoped enums
 24  enum flavor { BITTER, SWEET };
 25  class EnumClass {
 26    public:
 27      enum speed { FAST, SLOW };
 28      // Note: default values should be EnumClass::FAST and SWEET 
 29      bool blah(speed s = FAST, flavor f = SWEET) { return (s == FAST && f == SWEET); };
 30  };
 31
 32  // casts
 33  const char * casts1(const char *m = (const char *) NULL) {
 34    char *ret = NULL; 
 35    if (m) { 
 36      ret = new char[strlen(m)+1];
 37      strcpy(ret, m);
 38    }
 39    return ret;
 40  }
 41  const char * casts2(const char *m = (const char *) "Hello") {
 42    char *ret = NULL; 
 43    if (m) { 
 44      ret = new char[strlen(m)+1];
 45      strcpy(ret, m);
 46    }
 47    return ret;
 48  }
 49
 50  // char
 51  char chartest1(char c = 'x') { return c; }
 52  char chartest2(char c = '\0') { return c; }
 53
 54  // namespaces
 55  namespace AType { 
 56    enum AType { NoType }; 
 57  } 
 58  void dummy(AType::AType aType = AType::NoType) {}
 59  namespace A { 
 60    namespace B { 
 61      int CONST_NUM = 10; 
 62    } 
 63    int afunction(int i = B::CONST_NUM) { return i; }
 64  } 
 65
 66  // references
 67  int reftest1(const int &x = 42) { return x; }
 68  std::string reftest2(const std::string &x = "hello") { return x; }
 69
 70  // enum scope
 71  class Tree {
 72    public:
 73      enum types {Oak, Fir, Cedar};
 74      void chops(enum types type) {}
 75      void test(int x = Oak + Fir + Cedar) {}
 76  };
 77  enum Tree::types chops(enum Tree::types type) { return type; }
 78 
 79%}
 80
 81// Rename a class member
 82%rename(bar2) Foo::bar;
 83%rename(newname) Foo::oldname(int x = 1234);
 84%ignore Foo::Foo(int x, int y = 0, int z = 0);
 85%ignore Foo::meth(int x, int y = 0, int z = 0);
 86%rename(renamed3arg) Foo::renameme(int x, double d) const;
 87%rename(renamed2arg) Foo::renameme(int x) const;
 88%rename(renamed1arg) Foo::renameme() const;
 89
 90%inline %{
 91
 92  // Define a class
 93  class Foo {
 94    public:
 95      static int bar;
 96      static int spam;
 97
 98      Foo(){}
 99     
100      Foo(int x, int y = 0, int z = 0){}
101
102      void meth(int x, int y = 0, int z = 0){}
103    
104      // Use a renamed member as a default argument.  SWIG has to resolve
105      // bar to Foo::bar and not Foo::spam.  SWIG-1.3.11 got this wrong.
106      // (Different default parameter wrapping in SWIG-1.3.23 ensures SWIG doesn't have to resolve these symbols).
107      void method1(int x = bar) {}
108
109      // Use unrenamed member as default
110      void method2(int x = spam) {}
111
112      // test the method itself being renamed
113      void oldname(int x = 1234) {}
114      void renameme(int x = 1234, double d=123.4) const {}
115  };
116  int Foo::bar = 1;
117  int Foo::spam = 2;
118%}
119
120
121// tests valuewrapper
122%feature("compactdefaultargs") MyClass2::set;
123%inline %{
124  enum MyType { Val1, Val2 }; 
125
126  class MyClass1 
127  { 
128    public: 
129      MyClass1(MyType myType) {}
130  }; 
131
132  class MyClass2 
133  { 
134    public : 
135      void set(MyClass1 cl1 = Val1) {}
136      // This could have been written : set(MyClass1 cl1 = MyClass1(Val1)) 
137      // But it works in C++ since there is a "conversion" constructor in  MyClass1. 
138      void set2(MyClass1 cl1 = Val1) {}
139  };
140%}
141
142
143// Default parameters with exception specifications
144%inline %{
145void exceptionspec(int a = -1) throw (int, const char*) {
146  if (a == -1)
147    throw "ciao";
148  else
149    throw a;
150}
151struct Except {
152  Except(bool throwException, int a = -1) throw (int) {
153    if (throwException)
154      throw a;
155  }
156  void exspec(int a = 0) throw (int, const char*) {
157    ::exceptionspec(a);
158  }
159};
160%}
161
162// Default parameters in static class methods
163#ifdef SWIGPYTHON
164%rename(staticMethod) staticmethod;
165#endif
166
167%inline %{
168namespace SpaceName {
169  struct Statics {
170    static int staticmethod(int a=10, int b=20, int c=30) { return a+b+c; }
171  };
172}
173%}
174
175
176// Tests which could never be wrapped prior to changes in default argument wrapping implemented in SWIG-1.3.23:
177%inline %{
178class Tricky {
179  static int getDefault() { return 500; }
180  enum { privatevalue = 200 };
181  static const char charvalue;
182public:
183  int privatedefault(int val = privatevalue) { return val; }
184  int protectedint(int val = intvalue) { return val; }
185  double protecteddouble(double val = doublevalue) { return val; }
186  int functiondefault(int val = Tricky::getDefault()) { return val; }
187  char contrived(const char *c = &charvalue) { return *c; }
188protected:
189  static const int intvalue = 2000;
190  static const double doublevalue;
191};
192const char Tricky::charvalue = 'X';
193const double Tricky::doublevalue = 987.654;
194
195
196// tests default argument which is a constructor call within namespace
197// also tests default constructor (from defaulted parameter)
198namespace Space {
199struct Klass {
200  int val;
201  Klass(int val = -1) : val(val) {}
202};
203Klass constructorcall(const Klass& k = Klass()) { return k; }
204
205}
206%}
207
208%{
209struct ConstMethods {
210  int coo(double d = 0.0) { return 10; }
211  int coo(double d = 0.0) const { return 20; }
212};
213%}
214
215// const methods 
216// runtime test needed to check that the const method is called
217struct ConstMethods {
218  int coo(double d = 0.0) const;
219};
220
221
222
223// Default args with C linkage
224%inline
225%{
226  extern "C" double cfunc1(double x,double p = 1) {
227    return(x+p);
228  }
229
230  extern "C" {
231    double cfunc2(double x,double p = 2) {
232      return(x+p);
233    }
234
235    double cfunc3(double x,double p = 3) {
236      return(x+p);
237    }
238
239    typedef struct Pointf { 
240      double		x,y; 
241    } Pointf;
242  }
243%}