~ubuntu-branches/ubuntu/maverick/swig1.3/maverick

« back to all changes in this revision

Viewing changes to Examples/test-suite/default_args.i

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-09-01 18:35:55 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050901183555-eq59uwhq8b62e44c
Tags: 1.3.24-1ubuntu4
* Use php5-dev instead of php4-dev, to kick php4 out of main.
* Drop support for generation of pike bindings, as nothing uses it,
  and swig is the only thing keeping pike7.6 in main (Ubuntu #13796)

Show diffs side-by-side

added added

removed removed

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