aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/default_args.i
blob: fd09152cdd80e811654d874022eaf65eb115539a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Lots of tests for methods with default parameters / default arguments

%module default_args

#ifdef SWIGOCAML
%warnfilter(SWIGWARN_PARSE_KEYWORD) val;
#endif

%{
#if defined(_MSC_VER)
  #pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
#endif
%}

// throw is invalid in C++17 and later, only SWIG to use it
#define TESTCASE_THROW1(T1) throw(T1)
#define TESTCASE_THROW2(T1, T2) throw(T1, T2)
%{
#define TESTCASE_THROW1(T1)
#define TESTCASE_THROW2(T1, T2)
#include <string.h>
%}

%include <std_string.i>

%inline %{
  #include <string>

  // All kinds of numbers: hex, octal (which pose special problems to Python), negative...

  class TrickyInPython {
  public:
    int value_m1(int first, int pos = -1) { return pos; }
    unsigned value_0xabcdef(int first, unsigned rgb = 0xabcdef) { return rgb; }
    int value_0644(int first, int mode = 0644) { return mode; }
    int value_perm(int first, int mode = 0640 | 0004) { return mode; }
    int value_m01(int first, int val = -01) { return val; }
    bool booltest2(bool x = 0 | 1) { return x; }
    int max_32bit_int1(int a = 0x7FFFFFFF) { return a; }
    int max_32bit_int2(int a = 2147483647) { return a; }
    int min_32bit_int1(int a = -0x80000000) { return a; }
    long long too_big_32bit_int1(long long a = 0x80000000) { return a; }
    long long too_big_32bit_int2(long long a = 2147483648LL) { return a; }
    long long too_small_32bit_int1(long long a = -0x80000001) { return a; }
    long long too_small_32bit_int2(long long a = -2147483649LL) { return a; }
  };

  void doublevalue1(int first, double num = 0.0e-1) {}
  void doublevalue2(int first, double num = -0.0E2) {}

  void seek(long long offset = 0LL) {}
  void seek2(unsigned long long offset = 0ULL) {}
  void seek3(long offset = 0L) {}
  void seek4(unsigned long offset = 0UL) {}
  void seek5(unsigned long offset = 0U) {}
  void seek6(unsigned long offset = 02U) {}
  void seek7(unsigned long offset = 00U) {}
  void seek8(unsigned long offset = 1U) {}
  void seek9(long offset = 1L) {}
  void seekA(long long offset = 1LL) {}
  void seekB(unsigned long long offset = 1ULL) {}

  // Anonymous arguments
  int anonymous(int = 7771);
  int anonymous(int x) { return x; }

  // Bug [548272] Default arguments
  bool booltest(bool x = true) { return x; }

  // scoped enums
  enum flavor { BITTER, SWEET };
  class EnumClass {
    public:
      enum speed { FAST, SLOW };
      // Note: default values should be EnumClass::FAST and SWEET
      bool blah(speed s = FAST, flavor f = SWEET) { return (s == FAST && f == SWEET); };
  };

  // using base class enum in a derived class
  class DerivedEnumClass : public EnumClass {
  public:
    void accelerate(speed s = SLOW) { }
  };

  // casts
  const char * casts1(const char *m = (const char *) NULL) {
    char *ret = NULL;
    if (m) {
      ret = new char[strlen(m)+1];
      strcpy(ret, m);
    }
    return ret;
  }
  const char * casts2(const char *m = (const char *) "Hello") {
    char *ret = NULL;
    if (m) {
      ret = new char[strlen(m)+1];
      strcpy(ret, m);
    }
    return ret;
  }

  // char
  char chartest1(char c = 'x') { return c; }
  char chartest2(char c = '\0') { return c; }
  char chartest3(char c = '\1') { return c; }
  char chartest4(char c = '\n') { return c; }
  char chartest5(char c = '\102') { return c; } // 'B'
  char chartest6(char c = '\x43') { return c; } // 'C'

  // namespaces
  namespace AType {
    enum AType { NoType };
  }
  void dummy(AType::AType aType = AType::NoType) {}
  namespace A {
    namespace B {
      int CONST_NUM = 10;
    }
    int afunction(int i = B::CONST_NUM) { return i; }
  }

  // references
  int reftest1(const int &x = 42) { return x; }
  std::string reftest2(const std::string &x = "hello") { return x; }

  // enum scope
  class Tree {
    public:
      enum types {Oak, Fir, Cedar};
      void chops(enum types type) {}
      void test(int x = Oak + Fir + Cedar) {}
  };
  enum Tree::types chops(enum Tree::types type) { return type; }

%}

// Rename a class member
%rename(bar2) Foo::bar;
%rename(newname) Foo::oldname(int x = 1234);
%ignore Foo::Foo(int x, int y = 0, int z = 0);
%ignore Foo::meth(int x, int y = 0, int z = 0);
%rename(renamed3arg) Foo::renameme(int x, double d) const;
%rename(renamed2arg) Foo::renameme(int x) const;
%rename(renamed1arg) Foo::renameme() const;

%typemap(default) double* null_by_default "$1=0;"

%inline %{
  typedef void* MyHandle;

  // Define a class
  class Foo {
    public:
      static int bar;
      static int spam;

      Foo(){}

      Foo(int x, int y = 0, int z = 0){}

      void meth(int x, int y = 0, int z = 0){}

      // Use a renamed member as a default argument.  SWIG has to resolve
      // bar to Foo::bar and not Foo::spam.  SWIG-1.3.11 got this wrong.
      // (Different default parameter wrapping in SWIG-1.3.23 ensures SWIG doesn't have to resolve these symbols).
      void method1(int x = bar) {}

      // Use unrenamed member as default
      void method2(int x = spam) {}

      // test the method itself being renamed
      void oldname(int x = 1234) {}
      void renameme(int x = 1234, double d=123.4) const {}

      // test default values for pointer arguments
      int double_if_void_ptr_is_null(int n, void* p = NULL) { return p ? n : 2*n; }
      int double_if_handle_is_null(int n, MyHandle h = 0) { return h ? n : 2*n; }
      int double_if_dbl_ptr_is_null(int n, double* null_by_default)
        { return null_by_default ? n : 2*n; }

      void defaulted1(unsigned offset = -1U) {} // minus unsigned!
      void defaulted2(int offset = -1U) {} // minus unsigned!
  };
  int Foo::bar = 1;
  int Foo::spam = 2;
%}


// tests valuewrapper
%feature("compactdefaultargs") MyClass2::set;
%inline %{
  enum MyType { Val1, Val2 };

  class MyClass1
  {
    public:
      MyClass1(MyType myType) {}
  };

  class MyClass2
  {
    public :
      void set(MyClass1 cl1 = Val1) {}
      // This could have been written : set(MyClass1 cl1 = MyClass1(Val1))
      // But it works in C++ since there is a "conversion" constructor in  MyClass1.
      void set2(MyClass1 cl1 = Val1) {}
  };
%}


// Default parameters with exception specifications
%inline %{
void exceptionspec(int a = -1) TESTCASE_THROW2(int, const char*) {
  if (a == -1)
    throw "ciao";
  else
    throw a;
}
struct Except {
  Except(bool throwException, int a = -1) TESTCASE_THROW1(int) {
    if (throwException)
      throw a;
  }
  void exspec(int a = 0) TESTCASE_THROW2(int, const char*) {
    ::exceptionspec(a);
  }
};
%}

// Default parameters in static class methods
#if defined(SWIGPYTHON) || defined(SWIGJAVASCRIPT)
%rename(staticMethod) staticmethod;
#endif

%inline %{
namespace SpaceName {
  struct Statics {
    static int staticmethod(int a=10, int b=20, int c=30) { return a+b+c; }
  };
}
%}


// Tests which could never be wrapped prior to changes in default argument wrapping implemented in SWIG-1.3.23:
%inline %{
class Tricky {
  static int getDefault() { return 500; }
  enum { privatevalue = 200 };
  static const char charvalue;
public:
  int privatedefault(int val = privatevalue) { return val; }
  int protectedint(int val = intvalue) { return val; }
  double protecteddouble(double val = doublevalue) { return val; }
  int functiondefault(int val = Tricky::getDefault()) { return val; }
  char contrived(const char *c = &charvalue) { return *c; }
protected:
  static const int intvalue = 2000;
  static const double doublevalue;
};
const char Tricky::charvalue = 'X';
const double Tricky::doublevalue = 987.654;


// tests default argument which is a constructor call within namespace
// also tests default constructor (from defaulted parameter)
namespace Space {
struct Klass {
  int val;
  Klass(int val = -1) : val(val) {}
  static Klass inc(int n = 1, const Klass& k = Klass()) { return Klass(k.val + n); }
};
Klass constructorcall(const Klass& k = Klass()) { return k; }

}
%}

%{
struct ConstMethods {
  int coo(double d = 0.0) { return 10; }
  int coo(double d = 0.0) const { return 20; }
};
%}

// const methods
// runtime test needed to check that the const method is called
struct ConstMethods {
  int coo(double d = 0.0) const;
};



// Default args with C linkage
%inline
%{
  extern "C" double cfunc1(double x,double p = 1) {
    return(x+p);
  }

  extern "C" {
    double cfunc2(double x,double p = 2) {
      return(x+p);
    }

    double cfunc3(double x,double p = 3) {
      return(x+p);
    }

    typedef struct Pointf {
      double		x,y;
    } Pointf;
  }
%}

// Default arguments after ignored ones.
%typemap(in, numinputs=0) int square_error { $1 = 2; };
%typemap(default, noblock=1) int def17 { $1 = 17; };

// Enabling autodoc feature has a side effect of disabling the generation of
// aliases for functions that can hide problems with default arguments at
// Python level.
%feature("autodoc","0") slightly_off_square;

%inline %{
  inline int slightly_off_square(int square_error, int def17) { return def17*def17 + square_error; }
%}

// Python C default args
%feature("python:cdefaultargs") CDA::cdefaultargs_test1;
%inline %{
struct CDA {
  int cdefaultargs_test1(int a = 1) { return a; }
  int cdefaultargs_test2(int a = 1) { return a; }
};
%}