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