PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-1-3-29/SWIG/Examples/test-suite/namespace_typemap.i

#
Swig | 229 lines | 192 code | 32 blank | 5 comment | 0 complexity | 98f616ac9eff227a7305b246bce83c06 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. // This tests SWIG's handling of typemaps and namespaces
  2. %module namespace_typemap
  3. %{
  4. namespace test {
  5. /* A minimalistic string class */
  6. class string_class {
  7. char *data;
  8. public:
  9. string_class() {
  10. data = 0;
  11. }
  12. string_class(const char *s) {
  13. data = new char[strlen(s)+1];
  14. strcpy(data,s);
  15. }
  16. ~string_class() {
  17. if (data) delete [] data;
  18. }
  19. char *c_str() {
  20. return data;
  21. }
  22. };
  23. /* A minimalistic complex class */
  24. class complex {
  25. double re;
  26. double im;
  27. public:
  28. complex(double r = 0, double i = 0) {
  29. re = r;
  30. im = i;
  31. }
  32. double real() {
  33. return re;
  34. }
  35. double imag() {
  36. return im;
  37. }
  38. };
  39. }
  40. %}
  41. /* SWIG interface tests */
  42. #ifdef SWIGPYTHON
  43. %typemap(in) test::complex * {
  44. if (PyComplex_Check($input)) {
  45. $1 = new complex(PyComplex_RealAsDouble($input),
  46. PyComplex_ImagAsDouble($input));
  47. } else {
  48. PyErr_SetString(PyExc_TypeError,"Expected complex.\n");
  49. return NULL;
  50. }
  51. }
  52. %typemap(freearg) test::complex * {
  53. delete $1;
  54. }
  55. #endif
  56. namespace test {
  57. class string_class;
  58. #ifdef SWIGPYTHON
  59. %typemap(in) string_class * {
  60. $1 = new string_class(PyString_AsString($input));
  61. }
  62. %typemap(freearg) string_class * {
  63. delete $1;
  64. }
  65. #endif
  66. #ifdef SWIGRUBY
  67. %typemap(in) string_class * {
  68. $1 = new string_class(STR2CSTR($input));
  69. }
  70. %typemap(freearg) string_class * {
  71. delete $1;
  72. }
  73. #endif
  74. }
  75. %inline %{
  76. namespace test {
  77. class string_class;
  78. class complex;
  79. /* Functions in the namespace itself */
  80. char *stest1(string_class *s) {
  81. return s->c_str();
  82. }
  83. double ctest1(complex *c) {
  84. return c->real();
  85. }
  86. }
  87. namespace test2 {
  88. using test::string_class;
  89. using test::complex;
  90. /* Functions in another namespace */
  91. char *stest2(string_class *s) {
  92. return s->c_str();
  93. }
  94. double ctest2(complex *c) {
  95. return c->real();
  96. }
  97. }
  98. namespace test3 {
  99. using namespace test;
  100. char *stest3(string_class *s) {
  101. return s->c_str();
  102. }
  103. double ctest3(complex *c) {
  104. return c->real();
  105. }
  106. }
  107. namespace test4 {
  108. using namespace test2;
  109. char *stest4(string_class *s) {
  110. return s->c_str();
  111. }
  112. double ctest4(complex *c) {
  113. return c->real();
  114. }
  115. }
  116. namespace test5 {
  117. using namespace test3;
  118. char *stest5(string_class *s) {
  119. return s->c_str();
  120. }
  121. double ctest5(complex *c) {
  122. return c->real();
  123. }
  124. }
  125. char *stest6(test::string_class *s) {
  126. return s->c_str();
  127. }
  128. double ctest6(test::complex *c) {
  129. return c->real();
  130. }
  131. char *stest7(test2::string_class *s) {
  132. return s->c_str();
  133. }
  134. double ctest7(test2::complex *c) {
  135. return c->real();
  136. }
  137. char *stest8(test3::string_class *s) {
  138. return s->c_str();
  139. }
  140. double ctest8(test3::complex *c) {
  141. return c->real();
  142. }
  143. char *stest9(test4::string_class *s) {
  144. return s->c_str();
  145. }
  146. double ctest9(test4::complex *c) {
  147. return c->real();
  148. }
  149. char *stest10(test5::string_class *s) {
  150. return s->c_str();
  151. }
  152. double ctest10(test5::complex *c) {
  153. return c->real();
  154. }
  155. namespace test11 = test;
  156. char *stest11(test11::string_class *s) {
  157. return s->c_str();
  158. }
  159. double ctest11(test11::complex *c) {
  160. return c->real();
  161. }
  162. using namespace test2;
  163. using test::complex;
  164. char *stest12(string_class *s) {
  165. return s->c_str();
  166. }
  167. double ctest12(complex *c) {
  168. return c->real();
  169. }
  170. %}
  171. namespace Split {
  172. #ifdef SWIGPYTHON
  173. %typemap(in) PosInteger {
  174. $1 = PyInt_AsLong($input);
  175. if ($1 < 0) {
  176. PyErr_SetString(PyExc_ValueError,"domain error\n");
  177. return NULL;
  178. }
  179. }
  180. #endif
  181. #ifdef SWIGRUBY
  182. %typemap(in) PosInteger {
  183. $1 = NUM2INT($input);
  184. if ($1 < 0) {
  185. rb_raise(rb_eRangeError, "domain error");
  186. }
  187. }
  188. #endif
  189. }
  190. %inline %{
  191. namespace Split {
  192. typedef int PosInteger;
  193. PosInteger ttest1(PosInteger x) {
  194. return x;
  195. }
  196. }
  197. %}