/CableSwig-3.20.0/SWIG/Lib/perl5/std_vector.i

# · Swig · 438 lines · 403 code · 19 blank · 16 comment · 0 complexity · 554fd6e4735c5c09eba455fa9b0d787a MD5 · raw file

  1. //
  2. // SWIG typemaps for std::vector types
  3. // Luigi Ballabio
  4. // May 7, 2002
  5. // Chris Seatory
  6. // August 5, 2002
  7. // Igor Bely
  8. // May 16, 2003
  9. //
  10. // Perl implementation
  11. %include std_common.i
  12. %include exception.i
  13. // containers
  14. // methods which can raise are caused to throw an IndexError
  15. %exception std::vector::get {
  16. try {
  17. $action
  18. } catch (std::out_of_range& e) {
  19. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  20. }
  21. }
  22. %exception std::vector::set {
  23. try {
  24. $action
  25. } catch (std::out_of_range& e) {
  26. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  27. }
  28. }
  29. %exception std::vector::pop {
  30. try {
  31. $action
  32. } catch (std::out_of_range& e) {
  33. SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
  34. }
  35. }
  36. // ------------------------------------------------------------------------
  37. // std::vector
  38. //
  39. // The aim of all that follows would be to integrate std::vector with
  40. // Perl as much as possible, namely, to allow the user to pass and
  41. // be returned Perl arrays.
  42. // const declarations are used to guess the intent of the function being
  43. // exported; therefore, the following rationale is applied:
  44. //
  45. // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
  46. // the parameter being read-only, either a Perl sequence or a
  47. // previously wrapped std::vector<T> can be passed.
  48. // -- f(std::vector<T>&), f(std::vector<T>*):
  49. // the parameter must be modified; therefore, only a wrapped std::vector
  50. // can be passed.
  51. // -- std::vector<T> f():
  52. // the vector is returned by copy; therefore, a Perl sequence of T:s
  53. // is returned which is most easily used in other Perl functions
  54. // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
  55. // const std::vector<T>* f():
  56. // the vector is returned by reference; therefore, a wrapped std::vector
  57. // is returned
  58. // ------------------------------------------------------------------------
  59. %{
  60. #include <vector>
  61. #include <algorithm>
  62. #include <stdexcept>
  63. %}
  64. // exported class
  65. namespace std {
  66. template<class T> class vector {
  67. %typemap(in) vector<T> (std::vector<T>* v) {
  68. if (SWIG_ConvertPtr($input,(void **) &v,
  69. $&1_descriptor,1) != -1) {
  70. $1 = *v;
  71. } else if (SvROK($input)) {
  72. AV *av = (AV *)SvRV($input);
  73. if (SvTYPE(av) != SVt_PVAV)
  74. SWIG_croak("Type error in argument $argnum of $symname. "
  75. "Expected an array of " #T);
  76. SV **tv;
  77. I32 len = av_len(av) + 1;
  78. T* obj;
  79. for (int i=0; i<len; i++) {
  80. tv = av_fetch(av, i, 0);
  81. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  82. $descriptor(T *),0) != -1) {
  83. $1.push_back(*obj);
  84. } else {
  85. SWIG_croak("Type error in argument $argnum of "
  86. "$symname. "
  87. "Expected an array of " #T);
  88. }
  89. }
  90. } else {
  91. SWIG_croak("Type error in argument $argnum of $symname. "
  92. "Expected an array of " #T);
  93. }
  94. }
  95. %typemap(in) const vector<T>& (std::vector<T> temp,
  96. std::vector<T>* v),
  97. const vector<T>* (std::vector<T> temp,
  98. std::vector<T>* v) {
  99. if (SWIG_ConvertPtr($input,(void **) &v,
  100. $1_descriptor,1) != -1) {
  101. $1 = v;
  102. } else if (SvROK($input)) {
  103. AV *av = (AV *)SvRV($input);
  104. if (SvTYPE(av) != SVt_PVAV)
  105. SWIG_croak("Type error in argument $argnum of $symname. "
  106. "Expected an array of " #T);
  107. SV **tv;
  108. I32 len = av_len(av) + 1;
  109. T* obj;
  110. for (int i=0; i<len; i++) {
  111. tv = av_fetch(av, i, 0);
  112. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  113. $descriptor(T *),0) != -1) {
  114. temp.push_back(*obj);
  115. } else {
  116. SWIG_croak("Type error in argument $argnum of "
  117. "$symname. "
  118. "Expected an array of " #T);
  119. }
  120. }
  121. $1 = &temp;
  122. } else {
  123. SWIG_croak("Type error in argument $argnum of $symname. "
  124. "Expected an array of " #T);
  125. }
  126. }
  127. %typemap(out) vector<T> {
  128. int len = $1.size();
  129. SV **svs = new SV*[len];
  130. for (unsigned int i=0; i<len; i++) {
  131. svs[i] = sv_newmortal();
  132. SWIG_MakePtr(svs[i], (void*)&($1[i]),
  133. $descriptor(T *), $shadow|$owner);
  134. }
  135. AV *myav = av_make(len, svs);
  136. delete[] svs;
  137. $result = newRV_noinc((SV*) myav);
  138. sv_2mortal($result);
  139. argvi++;
  140. }
  141. %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
  142. {
  143. /* wrapped vector? */
  144. std::vector<T >* v;
  145. if (SWIG_ConvertPtr($input,(void **) &v,
  146. $1_&descriptor,0) != -1) {
  147. $1 = 1;
  148. } else if (SvROK($input)) {
  149. /* native sequence? */
  150. AV *av = (AV *)SvRV($input);
  151. if (SvTYPE(av) == SVt_PVAV) {
  152. SV **tv;
  153. I32 len = av_len(av) + 1;
  154. if (len == 0) {
  155. /* an empty sequence can be of any type */
  156. $1 = 1;
  157. } else {
  158. /* check the first element only */
  159. T* obj;
  160. tv = av_fetch(av, 0, 0);
  161. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  162. $descriptor(T *),0) != -1)
  163. $1 = 1;
  164. else
  165. $1 = 0;
  166. }
  167. }
  168. } else {
  169. $1 = 0;
  170. }
  171. }
  172. }
  173. %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
  174. const vector<T>* {
  175. {
  176. /* wrapped vector? */
  177. std::vector<T >* v;
  178. if (SWIG_ConvertPtr($input,(void **) &v,
  179. $1_descriptor,0) != -1) {
  180. $1 = 1;
  181. } else if (SvROK($input)) {
  182. /* native sequence? */
  183. AV *av = (AV *)SvRV($input);
  184. if (SvTYPE(av) == SVt_PVAV) {
  185. SV **tv;
  186. I32 len = av_len(av) + 1;
  187. if (len == 0) {
  188. /* an empty sequence can be of any type */
  189. $1 = 1;
  190. } else {
  191. /* check the first element only */
  192. T* obj;
  193. tv = av_fetch(av, 0, 0);
  194. if (SWIG_ConvertPtr(*tv, (void **)&obj,
  195. $descriptor(T *),0) != -1)
  196. $1 = 1;
  197. else
  198. $1 = 0;
  199. }
  200. }
  201. } else {
  202. $1 = 0;
  203. }
  204. }
  205. }
  206. public:
  207. vector(unsigned int size = 0);
  208. vector(unsigned int size, const T& value);
  209. vector(const vector<T> &);
  210. unsigned int size() const;
  211. bool empty() const;
  212. void clear();
  213. %rename(push) push_back;
  214. void push_back(const T& x);
  215. %extend {
  216. T pop() {
  217. if (self->size() == 0)
  218. throw std::out_of_range("pop from empty vector");
  219. T x = self->back();
  220. self->pop_back();
  221. return x;
  222. }
  223. T& get(int i) {
  224. int size = int(self->size());
  225. if (i>=0 && i<size)
  226. return (*self)[i];
  227. else
  228. throw std::out_of_range("vector index out of range");
  229. }
  230. void set(int i, const T& x) {
  231. int size = int(self->size());
  232. if (i>=0 && i<size)
  233. (*self)[i] = x;
  234. else
  235. throw std::out_of_range("vector index out of range");
  236. }
  237. }
  238. };
  239. // specializations for built-ins
  240. %define specialize_std_vector(T,CHECK_T,TO_T,FROM_T)
  241. template<> class vector<T> {
  242. %typemap(in) vector<T> (std::vector<T>* v) {
  243. if (SWIG_ConvertPtr($input,(void **) &v,
  244. $&1_descriptor,1) != -1){
  245. $1 = *v;
  246. } else if (SvROK($input)) {
  247. AV *av = (AV *)SvRV($input);
  248. if (SvTYPE(av) != SVt_PVAV)
  249. SWIG_croak("Type error in argument $argnum of $symname. "
  250. "Expected an array of " #T);
  251. SV **tv;
  252. I32 len = av_len(av) + 1;
  253. for (int i=0; i<len; i++) {
  254. tv = av_fetch(av, i, 0);
  255. if (CHECK_T(*tv)) {
  256. $1.push_back(TO_T(*tv));
  257. } else {
  258. SWIG_croak("Type error in argument $argnum of "
  259. "$symname. "
  260. "Expected an array of " #T);
  261. }
  262. }
  263. } else {
  264. SWIG_croak("Type error in argument $argnum of $symname. "
  265. "Expected an array of " #T);
  266. }
  267. }
  268. %typemap(in) const vector<T>& (std::vector<T> temp,
  269. std::vector<T>* v),
  270. const vector<T>* (std::vector<T> temp,
  271. std::vector<T>* v) {
  272. if (SWIG_ConvertPtr($input,(void **) &v,
  273. $1_descriptor,1) != -1) {
  274. $1 = v;
  275. } else if (SvROK($input)) {
  276. AV *av = (AV *)SvRV($input);
  277. if (SvTYPE(av) != SVt_PVAV)
  278. SWIG_croak("Type error in argument $argnum of $symname. "
  279. "Expected an array of " #T);
  280. SV **tv;
  281. I32 len = av_len(av) + 1;
  282. T* obj;
  283. for (int i=0; i<len; i++) {
  284. tv = av_fetch(av, i, 0);
  285. if (CHECK_T(*tv)) {
  286. temp.push_back(TO_T(*tv));
  287. } else {
  288. SWIG_croak("Type error in argument $argnum of "
  289. "$symname. "
  290. "Expected an array of " #T);
  291. }
  292. }
  293. $1 = &temp;
  294. } else {
  295. SWIG_croak("Type error in argument $argnum of $symname. "
  296. "Expected an array of " #T);
  297. }
  298. }
  299. %typemap(out) vector<T> {
  300. int len = $1.size();
  301. SV **svs = new SV*[len];
  302. for (unsigned int i=0; i<len; i++) {
  303. svs[i] = sv_newmortal();
  304. FROM_T(svs[i], $1[i]);
  305. }
  306. AV *myav = av_make(len, svs);
  307. delete[] svs;
  308. $result = newRV_noinc((SV*) myav);
  309. sv_2mortal($result);
  310. argvi++;
  311. }
  312. %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
  313. {
  314. /* wrapped vector? */
  315. std::vector<T >* v;
  316. if (SWIG_ConvertPtr($input,(void **) &v,
  317. $1_&descriptor,0) != -1) {
  318. $1 = 1;
  319. } else if (SvROK($input)) {
  320. /* native sequence? */
  321. AV *av = (AV *)SvRV($input);
  322. if (SvTYPE(av) == SVt_PVAV) {
  323. SV **tv;
  324. I32 len = av_len(av) + 1;
  325. if (len == 0) {
  326. /* an empty sequence can be of any type */
  327. $1 = 1;
  328. } else {
  329. /* check the first element only */
  330. tv = av_fetch(av, 0, 0);
  331. if (CHECK_T(*tv))
  332. $1 = 1;
  333. else
  334. $1 = 0;
  335. }
  336. }
  337. } else {
  338. $1 = 0;
  339. }
  340. }
  341. }
  342. %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
  343. const vector<T>* {
  344. {
  345. /* wrapped vector? */
  346. std::vector<T >* v;
  347. if (SWIG_ConvertPtr($input,(void **) &v,
  348. $1_descriptor,0) != -1) {
  349. $1 = 1;
  350. } else if (SvROK($input)) {
  351. /* native sequence? */
  352. AV *av = (AV *)SvRV($input);
  353. if (SvTYPE(av) == SVt_PVAV) {
  354. SV **tv;
  355. I32 len = av_len(av) + 1;
  356. if (len == 0) {
  357. /* an empty sequence can be of any type */
  358. $1 = 1;
  359. } else {
  360. /* check the first element only */
  361. tv = av_fetch(av, 0, 0);
  362. if (CHECK_T(*tv))
  363. $1 = 1;
  364. else
  365. $1 = 0;
  366. }
  367. }
  368. } else {
  369. $1 = 0;
  370. }
  371. }
  372. }
  373. public:
  374. vector(unsigned int size = 0);
  375. vector(unsigned int size, T value);
  376. vector(const vector<T> &);
  377. unsigned int size() const;
  378. bool empty() const;
  379. void clear();
  380. %rename(push) push_back;
  381. void push_back(T x);
  382. %extend {
  383. T pop() {
  384. if (self->size() == 0)
  385. throw std::out_of_range("pop from empty vector");
  386. T x = self->back();
  387. self->pop_back();
  388. return x;
  389. }
  390. T get(int i) {
  391. int size = int(self->size());
  392. if (i>=0 && i<size)
  393. return (*self)[i];
  394. else
  395. throw std::out_of_range("vector index out of range");
  396. }
  397. void set(int i, T x) {
  398. int size = int(self->size());
  399. if (i>=0 && i<size)
  400. (*self)[i] = x;
  401. else
  402. throw std::out_of_range("vector index out of range");
  403. }
  404. }
  405. };
  406. %enddef
  407. specialize_std_vector(bool,SvIOK,SvIVX,sv_setiv);
  408. specialize_std_vector(char,SvIOK,SvIVX,sv_setiv);
  409. specialize_std_vector(int,SvIOK,SvIVX,sv_setiv);
  410. specialize_std_vector(short,SvIOK,SvIVX,sv_setiv);
  411. specialize_std_vector(long,SvIOK,SvIVX,sv_setiv);
  412. specialize_std_vector(unsigned char,SvIOK,SvIVX,sv_setiv);
  413. specialize_std_vector(unsigned int,SvIOK,SvIVX,sv_setiv);
  414. specialize_std_vector(unsigned short,SvIOK,SvIVX,sv_setiv);
  415. specialize_std_vector(unsigned long,SvIOK,SvIVX,sv_setiv);
  416. specialize_std_vector(float,SvNIOK,SwigSvToNumber,sv_setnv);
  417. specialize_std_vector(double,SvNIOK,SwigSvToNumber,sv_setnv);
  418. specialize_std_vector(std::string,SvPOK,SvPVX,SwigSvFromString);
  419. }