PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-25/SWIG/Lib/perl5/std_list.i

#
Swig | 367 lines | 334 code | 17 blank | 16 comment | 0 complexity | 0dc1cc747be44e333ea7faf6bb3313eb MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. //
  2. // SWIG typemaps for std::list types
  3. // Lluis Padro
  4. // June 3, 2003
  5. // based on existing std_vector.i
  6. // Perl implementation
  7. %include std_common.i
  8. %include exception.i
  9. // containers
  10. // ------------------------------------------------------------------------
  11. // std::list
  12. //
  13. // The aim of all that follows would be to integrate std::list with
  14. // Perl as much as possible, namely, to allow the user to pass and
  15. // be returned Perl arrays.
  16. // const declarations are used to guess the intent of the function being
  17. // exported; therefore, the following rationale is applied:
  18. //
  19. // -- f(std::list<T>), f(const std::list<T>&), f(const std::list<T>*):
  20. // the parameter being read-only, either a Perl sequence or a
  21. // previously wrapped std::list<T> can be passed.
  22. // -- f(std::list<T>&), f(std::list<T>*):
  23. // the parameter must be modified; therefore, only a wrapped std::list
  24. // can be passed.
  25. // -- std::list<T> f():
  26. // the list is returned by copy; therefore, a Perl sequence of T:s
  27. // is returned which is most easily used in other Perl functions
  28. // -- std::list<T>& f(), std::list<T>* f(), const std::list<T>& f(),
  29. // const std::list<T>* f():
  30. // the list is returned by reference; therefore, a wrapped std::list
  31. // is returned
  32. // ------------------------------------------------------------------------
  33. %{
  34. #include <list>
  35. #include <algorithm>
  36. #include <stdexcept>
  37. %}
  38. // exported class
  39. namespace std {
  40. template<class T> class list {
  41. %typemap(in) list<T> (std::list<T>* v) {
  42. if (SWIG_ConvertPtr($input,(void **) &v,
  43. $&1_descriptor,1) != -1) {
  44. $1 = *v;
  45. } else if (SvROK($input)) {
  46. AV *av = (AV *)SvRV($input);
  47. if (SvTYPE(av) != SVt_PVAV)
  48. SWIG_croak("Type error in argument $argnum of $symname. "
  49. "Expected an array of " #T);
  50. SV **tv;
  51. I32 len = av_len(av) + 1;
  52. T* obj;
  53. for (int i=0; i<len; i++) {
  54. tv = av_fetch(av, i, 0);
  55. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  56. $descriptor(T *),0) != -1) {
  57. $1.push_back(*obj);
  58. } else {
  59. SWIG_croak("Type error in argument $argnum of "
  60. "$symname. "
  61. "Expected an array of " #T);
  62. }
  63. }
  64. } else {
  65. SWIG_croak("Type error in argument $argnum of $symname. "
  66. "Expected an array of " #T);
  67. }
  68. }
  69. %typemap(in) const list<T>& (std::list<T> temp,
  70. std::list<T>* v),
  71. const list<T>* (std::list<T> temp,
  72. std::list<T>* v) {
  73. if (SWIG_ConvertPtr($input,(void **) &v,
  74. $1_descriptor,1) != -1) {
  75. $1 = v;
  76. } else if (SvROK($input)) {
  77. AV *av = (AV *)SvRV($input);
  78. if (SvTYPE(av) != SVt_PVAV)
  79. SWIG_croak("Type error in argument $argnum of $symname. "
  80. "Expected an array of " #T);
  81. SV **tv;
  82. I32 len = av_len(av) + 1;
  83. T* obj;
  84. for (int i=0; i<len; i++) {
  85. tv = av_fetch(av, i, 0);
  86. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  87. $descriptor(T *),0) != -1) {
  88. temp.push_back(*obj);
  89. } else {
  90. SWIG_croak("Type error in argument $argnum of "
  91. "$symname. "
  92. "Expected an array of " #T);
  93. }
  94. }
  95. $1 = &temp;
  96. } else {
  97. SWIG_croak("Type error in argument $argnum of $symname. "
  98. "Expected an array of " #T);
  99. }
  100. }
  101. %typemap(out) list<T> {
  102. std::list<T>::const_iterator i;
  103. unsigned int j;
  104. int len = $1.size();
  105. SV **svs = new SV*[len];
  106. for (i=$1.begin(), j=0; i!=$1.end(); i++, j++) {
  107. T* ptr = new T(*i);
  108. svs[j] = sv_newmortal();
  109. SWIG_MakePtr(svs[j], (void*) ptr,
  110. $descriptor(T *), $shadow|$owner);
  111. }
  112. AV *myav = av_make(len, svs);
  113. delete[] svs;
  114. $result = newRV_noinc((SV*) myav);
  115. sv_2mortal($result);
  116. argvi++;
  117. }
  118. %typecheck(SWIG_TYPECHECK_LIST) list<T> {
  119. {
  120. /* wrapped list? */
  121. std::list<T >* v;
  122. if (SWIG_ConvertPtr($input,(void **) &v,
  123. $1_&descriptor,0) != -1) {
  124. $1 = 1;
  125. } else if (SvROK($input)) {
  126. /* native sequence? */
  127. AV *av = (AV *)SvRV($input);
  128. if (SvTYPE(av) == SVt_PVAV) {
  129. SV **tv;
  130. I32 len = av_len(av) + 1;
  131. if (len == 0) {
  132. /* an empty sequence can be of any type */
  133. $1 = 1;
  134. } else {
  135. /* check the first element only */
  136. T* obj;
  137. tv = av_fetch(av, 0, 0);
  138. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  139. $descriptor(T *),0) != -1)
  140. $1 = 1;
  141. else
  142. $1 = 0;
  143. }
  144. }
  145. } else {
  146. $1 = 0;
  147. }
  148. }
  149. }
  150. %typecheck(SWIG_TYPECHECK_LIST) const list<T>&,
  151. const list<T>* {
  152. {
  153. /* wrapped list? */
  154. std::list<T >* v;
  155. if (SWIG_ConvertPtr($input,(void **) &v,
  156. $1_descriptor,0) != -1) {
  157. $1 = 1;
  158. } else if (SvROK($input)) {
  159. /* native sequence? */
  160. AV *av = (AV *)SvRV($input);
  161. if (SvTYPE(av) == SVt_PVAV) {
  162. SV **tv;
  163. I32 len = av_len(av) + 1;
  164. if (len == 0) {
  165. /* an empty sequence can be of any type */
  166. $1 = 1;
  167. } else {
  168. /* check the first element only */
  169. T* obj;
  170. tv = av_fetch(av, 0, 0);
  171. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  172. $descriptor(T *),0) != -1)
  173. $1 = 1;
  174. else
  175. $1 = 0;
  176. }
  177. }
  178. } else {
  179. $1 = 0;
  180. }
  181. }
  182. }
  183. public:
  184. list();
  185. list(const list<T> &);
  186. unsigned int size() const;
  187. bool empty() const;
  188. void clear();
  189. %rename(push) push_back;
  190. void push_back(const T& x);
  191. };
  192. // specializations for built-ins
  193. %define specialize_std_list(T,CHECK_T,TO_T,FROM_T)
  194. template<> class list<T> {
  195. %typemap(in) list<T> (std::list<T>* v) {
  196. if (SWIG_ConvertPtr($input,(void **) &v,
  197. $&1_descriptor,1) != -1){
  198. $1 = *v;
  199. } else if (SvROK($input)) {
  200. AV *av = (AV *)SvRV($input);
  201. if (SvTYPE(av) != SVt_PVAV)
  202. SWIG_croak("Type error in argument $argnum of $symname. "
  203. "Expected an array of " #T);
  204. SV **tv;
  205. I32 len = av_len(av) + 1;
  206. for (int i=0; i<len; i++) {
  207. tv = av_fetch(av, i, 0);
  208. if (CHECK_T(*tv)) {
  209. $1.push_back(TO_T(*tv));
  210. } else {
  211. SWIG_croak("Type error in argument $argnum of "
  212. "$symname. "
  213. "Expected an array of " #T);
  214. }
  215. }
  216. } else {
  217. SWIG_croak("Type error in argument $argnum of $symname. "
  218. "Expected an array of " #T);
  219. }
  220. }
  221. %typemap(in) const list<T>& (std::list<T> temp,
  222. std::list<T>* v),
  223. const list<T>* (std::list<T> temp,
  224. std::list<T>* v) {
  225. if (SWIG_ConvertPtr($input,(void **) &v,
  226. $1_descriptor,1) != -1) {
  227. $1 = v;
  228. } else if (SvROK($input)) {
  229. AV *av = (AV *)SvRV($input);
  230. if (SvTYPE(av) != SVt_PVAV)
  231. SWIG_croak("Type error in argument $argnum of $symname. "
  232. "Expected an array of " #T);
  233. SV **tv;
  234. I32 len = av_len(av) + 1;
  235. T* obj;
  236. for (int i=0; i<len; i++) {
  237. tv = av_fetch(av, i, 0);
  238. if (CHECK_T(*tv)) {
  239. temp.push_back(TO_T(*tv));
  240. } else {
  241. SWIG_croak("Type error in argument $argnum of "
  242. "$symname. "
  243. "Expected an array of " #T);
  244. }
  245. }
  246. $1 = &temp;
  247. } else {
  248. SWIG_croak("Type error in argument $argnum of $symname. "
  249. "Expected an array of " #T);
  250. }
  251. }
  252. %typemap(out) list<T> {
  253. std::list<T>::const_iterator i;
  254. unsigned int j;
  255. int len = $1.size();
  256. SV **svs = new SV*[len];
  257. for (i=$1.begin(), j=0; i!=$1.end(); i++, j++) {
  258. svs[j] = sv_newmortal();
  259. FROM_T(svs[j], *i);
  260. }
  261. AV *myav = av_make(len, svs);
  262. delete[] svs;
  263. $result = newRV_noinc((SV*) myav);
  264. sv_2mortal($result);
  265. argvi++;
  266. }
  267. %typecheck(SWIG_TYPECHECK_LIST) list<T> {
  268. {
  269. /* wrapped list? */
  270. std::list<T >* v;
  271. if (SWIG_ConvertPtr($input,(void **) &v,
  272. $1_&descriptor,0) != -1) {
  273. $1 = 1;
  274. } else if (SvROK($input)) {
  275. /* native sequence? */
  276. AV *av = (AV *)SvRV($input);
  277. if (SvTYPE(av) == SVt_PVAV) {
  278. SV **tv;
  279. I32 len = av_len(av) + 1;
  280. if (len == 0) {
  281. /* an empty sequence can be of any type */
  282. $1 = 1;
  283. } else {
  284. /* check the first element only */
  285. tv = av_fetch(av, 0, 0);
  286. if (CHECK_T(*tv))
  287. $1 = 1;
  288. else
  289. $1 = 0;
  290. }
  291. }
  292. } else {
  293. $1 = 0;
  294. }
  295. }
  296. }
  297. %typecheck(SWIG_TYPECHECK_LIST) const list<T>&,
  298. const list<T>* {
  299. {
  300. /* wrapped list? */
  301. std::list<T >* v;
  302. if (SWIG_ConvertPtr($input,(void **) &v,
  303. $1_descriptor,0) != -1) {
  304. $1 = 1;
  305. } else if (SvROK($input)) {
  306. /* native sequence? */
  307. AV *av = (AV *)SvRV($input);
  308. if (SvTYPE(av) == SVt_PVAV) {
  309. SV **tv;
  310. I32 len = av_len(av) + 1;
  311. if (len == 0) {
  312. /* an empty sequence can be of any type */
  313. $1 = 1;
  314. } else {
  315. /* check the first element only */
  316. tv = av_fetch(av, 0, 0);
  317. if (CHECK_T(*tv))
  318. $1 = 1;
  319. else
  320. $1 = 0;
  321. }
  322. }
  323. } else {
  324. $1 = 0;
  325. }
  326. }
  327. }
  328. public:
  329. list();
  330. list(const list<T> &);
  331. unsigned int size() const;
  332. bool empty() const;
  333. void clear();
  334. %rename(push) push_back;
  335. void push_back(T x);
  336. };
  337. %enddef
  338. specialize_std_list(bool,SvIOK,SvIVX,sv_setiv);
  339. specialize_std_list(char,SvIOK,SvIVX,sv_setiv);
  340. specialize_std_list(int,SvIOK,SvIVX,sv_setiv);
  341. specialize_std_list(short,SvIOK,SvIVX,sv_setiv);
  342. specialize_std_list(long,SvIOK,SvIVX,sv_setiv);
  343. specialize_std_list(unsigned char,SvIOK,SvIVX,sv_setiv);
  344. specialize_std_list(unsigned int,SvIOK,SvIVX,sv_setiv);
  345. specialize_std_list(unsigned short,SvIOK,SvIVX,sv_setiv);
  346. specialize_std_list(unsigned long,SvIOK,SvIVX,sv_setiv);
  347. specialize_std_list(float,SvNIOK,SwigSvToNumber,sv_setnv);
  348. specialize_std_list(double,SvNIOK,SwigSvToNumber,sv_setnv);
  349. specialize_std_list(std::string,SvPOK,SvPVX,SwigSvFromString);
  350. }