PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Swig | 284 lines | 247 code | 32 blank | 5 comment | 0 complexity | fcc6e29b4c60a689ab8a9b2489ff74eb 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 test_complex class */
  24. class test_complex {
  25. double re;
  26. double im;
  27. public:
  28. test_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::test_complex * {
  44. if (PyComplex_Check($input)) {
  45. $1 = new test_complex(PyComplex_RealAsDouble($input),
  46. PyComplex_ImagAsDouble($input));
  47. } else {
  48. PyErr_SetString(PyExc_TypeError,"Expected test_complex.\n");
  49. return NULL;
  50. }
  51. }
  52. %typemap(freearg) test::test_complex * {
  53. delete $1;
  54. }
  55. #endif
  56. #ifdef SWIGOCTAVE
  57. %typemap(in) test::test_complex * {
  58. if ($input.is_complex_scalar()) {
  59. $1 = new test_complex($input.complex_value().real(),
  60. $input.complex_value().imag());
  61. } else {
  62. error("Expected test_complex.");
  63. }
  64. }
  65. %typemap(freearg) test::test_complex * {
  66. delete $1;
  67. }
  68. #endif
  69. #ifdef SWIGGO
  70. %typemap(gotype) test::test_complex * "complex128"
  71. %typemap(in) test::test_complex * {
  72. $1 = new test_complex(__real__ $input, __imag__ $input);
  73. }
  74. %typemap(freearg) test::test_complex * {
  75. delete $1;
  76. }
  77. #endif
  78. namespace test {
  79. class string_class;
  80. #ifdef SWIGPYTHON
  81. %typemap(in) string_class * {
  82. $1 = new string_class(SWIG_Python_str_AsChar($input));
  83. }
  84. %typemap(freearg) string_class * {
  85. delete $1;
  86. }
  87. #endif
  88. #ifdef SWIGOCTAVE
  89. %typemap(in) string_class * {
  90. $1 = new string_class($input.string_value().c_str());
  91. }
  92. %typemap(freearg) string_class * {
  93. delete $1;
  94. }
  95. #endif
  96. #ifdef SWIGRUBY
  97. %typemap(in) string_class * {
  98. $1 = new string_class(STR2CSTR($input));
  99. }
  100. %typemap(freearg) string_class * {
  101. delete $1;
  102. }
  103. #endif
  104. #ifdef SWIGGO
  105. %typemap(gotype) string_class * "string"
  106. %typemap(in) string_class * {
  107. $1 = new string_class($input.p);
  108. }
  109. %typemap(freearg) string_class * {
  110. delete $1;
  111. }
  112. #endif
  113. }
  114. %inline %{
  115. namespace test {
  116. class string_class;
  117. class test_complex;
  118. /* Functions in the namespace itself */
  119. char *stest1(string_class *s) {
  120. return s->c_str();
  121. }
  122. double ctest1(test_complex *c) {
  123. return c->real();
  124. }
  125. }
  126. namespace test2 {
  127. using test::string_class;
  128. using test::test_complex;
  129. /* Functions in another namespace */
  130. char *stest2(string_class *s) {
  131. return s->c_str();
  132. }
  133. double ctest2(test_complex *c) {
  134. return c->real();
  135. }
  136. }
  137. namespace test3 {
  138. using namespace test;
  139. char *stest3(string_class *s) {
  140. return s->c_str();
  141. }
  142. double ctest3(test_complex *c) {
  143. return c->real();
  144. }
  145. }
  146. namespace test4 {
  147. using namespace test2;
  148. char *stest4(string_class *s) {
  149. return s->c_str();
  150. }
  151. double ctest4(test_complex *c) {
  152. return c->real();
  153. }
  154. }
  155. namespace test5 {
  156. using namespace test3;
  157. char *stest5(string_class *s) {
  158. return s->c_str();
  159. }
  160. double ctest5(test_complex *c) {
  161. return c->real();
  162. }
  163. }
  164. char *stest6(test::string_class *s) {
  165. return s->c_str();
  166. }
  167. double ctest6(test::test_complex *c) {
  168. return c->real();
  169. }
  170. char *stest7(test2::string_class *s) {
  171. return s->c_str();
  172. }
  173. double ctest7(test2::test_complex *c) {
  174. return c->real();
  175. }
  176. char *stest8(test3::string_class *s) {
  177. return s->c_str();
  178. }
  179. double ctest8(test3::test_complex *c) {
  180. return c->real();
  181. }
  182. char *stest9(test4::string_class *s) {
  183. return s->c_str();
  184. }
  185. double ctest9(test4::test_complex *c) {
  186. return c->real();
  187. }
  188. char *stest10(test5::string_class *s) {
  189. return s->c_str();
  190. }
  191. double ctest10(test5::test_complex *c) {
  192. return c->real();
  193. }
  194. namespace test11 = test;
  195. char *stest11(test11::string_class *s) {
  196. return s->c_str();
  197. }
  198. double ctest11(test11::test_complex *c) {
  199. return c->real();
  200. }
  201. using namespace test2;
  202. using test::test_complex;
  203. char *stest12(string_class *s) {
  204. return s->c_str();
  205. }
  206. double ctest12(test_complex *c) {
  207. return c->real();
  208. }
  209. %}
  210. namespace Split {
  211. #ifdef SWIGPYTHON
  212. %typemap(in) PosInteger {
  213. $1 = PyInt_AsLong($input);
  214. if ($1 < 0) {
  215. PyErr_SetString(PyExc_ValueError,"domain error\n");
  216. return NULL;
  217. }
  218. }
  219. #endif
  220. #ifdef SWIGOCTAVE
  221. %typemap(in) PosInteger {
  222. $1 = $input.long_value();
  223. if ($1 < 0) {
  224. error("domain error");
  225. }
  226. }
  227. #endif
  228. #ifdef SWIGRUBY
  229. %typemap(in) PosInteger {
  230. $1 = NUM2INT($input);
  231. if ($1 < 0) {
  232. rb_raise(rb_eRangeError, "domain error");
  233. }
  234. }
  235. #endif
  236. #ifdef SWIGGO
  237. %typemap(in) PosInteger {
  238. $1 = $input;
  239. if ($1 < 0) {
  240. _swig_gopanic("domain error");
  241. }
  242. }
  243. #endif
  244. }
  245. %inline %{
  246. namespace Split {
  247. typedef int PosInteger;
  248. PosInteger ttest1(PosInteger x) {
  249. return x;
  250. }
  251. }
  252. %}