rubycontainer.swg 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /* -----------------------------------------------------------------------------
  2. * rubycontainer.swg
  3. *
  4. * Ruby sequence <-> C++ container wrapper
  5. *
  6. * This wrapper, and its iterator, allows a general use (and reuse) of
  7. * the mapping between C++ and Ruby, thanks to the C++ templates.
  8. *
  9. * Of course, it needs the C++ compiler to support templates, but
  10. * since we will use this wrapper with the STL containers, that should
  11. * be the case.
  12. * ----------------------------------------------------------------------------- */
  13. %{
  14. #include <iostream>
  15. %}
  16. #if !defined(SWIG_NO_EXPORT_ITERATOR_METHODS)
  17. # if !defined(SWIG_EXPORT_ITERATOR_METHODS)
  18. # define SWIG_EXPORT_ITERATOR_METHODS SWIG_EXPORT_ITERATOR_METHODS
  19. # endif
  20. #endif
  21. %include <rubyiterators.swg>
  22. /**** The RubySequence C++ Wrap ***/
  23. %fragment("<stdexcept>");
  24. %include <std_except.i>
  25. %fragment("RubySequence_Base","header")
  26. {
  27. %#include <functional>
  28. namespace swig {
  29. template < class T >
  30. struct yield : public std::unary_function< T, bool >
  31. {
  32. bool
  33. operator()( const T& v ) const
  34. {
  35. return RTEST( rb_yield( swig::from< T >(v) ) );
  36. }
  37. };
  38. inline size_t
  39. check_index(ptrdiff_t i, size_t size, bool insert = false) {
  40. if ( i < 0 ) {
  41. if ((size_t) (-i) <= size)
  42. return (size_t) (i + size);
  43. } else if ( (size_t) i < size ) {
  44. return (size_t) i;
  45. } else if (insert && ((size_t) i == size)) {
  46. return size;
  47. }
  48. throw std::out_of_range("index out of range");
  49. }
  50. inline size_t
  51. slice_index(ptrdiff_t i, size_t size) {
  52. if ( i < 0 ) {
  53. if ((size_t) (-i) <= size) {
  54. return (size_t) (i + size);
  55. } else {
  56. throw std::out_of_range("index out of range");
  57. }
  58. } else {
  59. return ( (size_t) i < size ) ? ((size_t) i) : size;
  60. }
  61. }
  62. template <class Sequence, class Difference>
  63. inline typename Sequence::iterator
  64. getpos(Sequence* self, Difference i) {
  65. typename Sequence::iterator pos = self->begin();
  66. std::advance(pos, check_index(i,self->size()));
  67. return pos;
  68. }
  69. template <class Sequence, class Difference>
  70. inline typename Sequence::const_iterator
  71. cgetpos(const Sequence* self, Difference i) {
  72. typename Sequence::const_iterator pos = self->begin();
  73. std::advance(pos, check_index(i,self->size()));
  74. return pos;
  75. }
  76. template <class Sequence>
  77. inline void
  78. resize(Sequence *seq, typename Sequence::size_type n, typename Sequence::value_type x) {
  79. seq->resize(n, x);
  80. }
  81. template <class Sequence, class Difference>
  82. inline Sequence*
  83. getslice(const Sequence* self, Difference i, Difference j) {
  84. typename Sequence::size_type size = self->size();
  85. typename Sequence::size_type ii = swig::check_index(i, size, (i == size && j == size));
  86. typename Sequence::size_type jj = swig::slice_index(j, size);
  87. if (jj > ii) {
  88. typename Sequence::const_iterator vb = self->begin();
  89. typename Sequence::const_iterator ve = self->begin();
  90. std::advance(vb,ii);
  91. std::advance(ve,jj);
  92. return new Sequence(vb, ve);
  93. } else {
  94. return new Sequence();
  95. }
  96. }
  97. template <class Sequence, class Difference, class InputSeq>
  98. inline void
  99. setslice(Sequence* self, Difference i, Difference j, const InputSeq& v) {
  100. typename Sequence::size_type size = self->size();
  101. typename Sequence::size_type ii = swig::check_index(i, size, true);
  102. typename Sequence::size_type jj = swig::slice_index(j, size);
  103. if (jj < ii) jj = ii;
  104. size_t ssize = jj - ii;
  105. if (ssize <= v.size()) {
  106. typename Sequence::iterator sb = self->begin();
  107. typename InputSeq::const_iterator vmid = v.begin();
  108. std::advance(sb,ii);
  109. std::advance(vmid, jj - ii);
  110. self->insert(std::copy(v.begin(), vmid, sb), vmid, v.end());
  111. } else {
  112. typename Sequence::iterator sb = self->begin();
  113. typename Sequence::iterator se = self->begin();
  114. std::advance(sb,ii);
  115. std::advance(se,jj);
  116. self->erase(sb,se);
  117. self->insert(sb, v.begin(), v.end());
  118. }
  119. }
  120. template <class Sequence, class Difference>
  121. inline void
  122. delslice(Sequence* self, Difference i, Difference j) {
  123. typename Sequence::size_type size = self->size();
  124. typename Sequence::size_type ii = swig::check_index(i, size, true);
  125. typename Sequence::size_type jj = swig::slice_index(j, size);
  126. if (jj > ii) {
  127. typename Sequence::iterator sb = self->begin();
  128. typename Sequence::iterator se = self->begin();
  129. std::advance(sb,ii);
  130. std::advance(se,jj);
  131. self->erase(sb,se);
  132. }
  133. }
  134. }
  135. }
  136. %fragment("RubySequence_Cont","header",
  137. fragment="<stddef.h>",
  138. fragment="StdTraits",
  139. fragment="RubySequence_Base",
  140. fragment="ConstIterator_T")
  141. {
  142. namespace swig
  143. {
  144. /**
  145. * This class is a proxy class for references, used to return and set values
  146. * of an element of a Ruby Array of stuff.
  147. * It can be used by RubySequence_InputIterator to make it work with STL
  148. * algorithms.
  149. */
  150. template <class T>
  151. struct RubySequence_Ref
  152. {
  153. RubySequence_Ref(VALUE seq, int index)
  154. : _seq(seq), _index(index)
  155. {
  156. }
  157. operator T () const
  158. {
  159. VALUE item = rb_ary_entry(_seq, _index );
  160. try {
  161. return swig::as<T>(item, true);
  162. } catch (std::exception& e) {
  163. char msg[1024];
  164. sprintf(msg, "in sequence element %d ", _index);
  165. VALUE lastErr = rb_gv_get("$!");
  166. if ( lastErr == Qnil ) {
  167. %type_error(swig::type_name<T>());
  168. }
  169. VALUE str = rb_str_new2(msg);
  170. str = rb_str_cat2( str, e.what() );
  171. SWIG_Ruby_ExceptionType( NULL, str );
  172. throw;
  173. }
  174. }
  175. RubySequence_Ref& operator=(const T& v)
  176. {
  177. rb_ary_set(_seq, _index, swig::from< T >(v));
  178. return *this;
  179. }
  180. private:
  181. VALUE _seq;
  182. int _index;
  183. };
  184. /**
  185. * This class is a proxy to return a pointer to a class, usually
  186. * RubySequence_Ref.
  187. * It can be used by RubySequence_InputIterator to make it work with STL
  188. * algorithms.
  189. */
  190. template <class T>
  191. struct RubySequence_ArrowProxy
  192. {
  193. RubySequence_ArrowProxy(const T& x): m_value(x) {}
  194. const T* operator->() const { return &m_value; }
  195. operator const T*() const { return &m_value; }
  196. T m_value;
  197. };
  198. /**
  199. * Input Iterator. This adapator class is a random access iterator that
  200. * allows you to use STL algorithms with a Ruby class (a Ruby Array by default).
  201. */
  202. template <class T, class Reference = RubySequence_Ref< T > >
  203. struct RubySequence_InputIterator
  204. {
  205. typedef RubySequence_InputIterator<T, Reference > self;
  206. typedef std::random_access_iterator_tag iterator_category;
  207. typedef Reference reference;
  208. typedef T value_type;
  209. typedef T* pointer;
  210. typedef ptrdiff_t difference_type;
  211. RubySequence_InputIterator()
  212. {
  213. }
  214. RubySequence_InputIterator(VALUE seq, int index)
  215. : _seq(seq), _index(index)
  216. {
  217. }
  218. reference operator*() const
  219. {
  220. return reference(_seq, _index);
  221. }
  222. RubySequence_ArrowProxy<T>
  223. operator->() const {
  224. return RubySequence_ArrowProxy<T>(operator*());
  225. }
  226. bool operator==(const self& ri) const
  227. {
  228. return (_index == ri._index) && (_seq == ri._seq);
  229. }
  230. bool operator!=(const self& ri) const
  231. {
  232. return !(operator==(ri));
  233. }
  234. self& operator ++ ()
  235. {
  236. ++_index;
  237. return *this;
  238. }
  239. self& operator -- ()
  240. {
  241. --_index;
  242. return *this;
  243. }
  244. self& operator += (difference_type n)
  245. {
  246. _index += n;
  247. return *this;
  248. }
  249. self operator +(difference_type n) const
  250. {
  251. return self(_seq, _index + n);
  252. }
  253. self& operator -= (difference_type n)
  254. {
  255. _index -= n;
  256. return *this;
  257. }
  258. self operator -(difference_type n) const
  259. {
  260. return self(_seq, _index - n);
  261. }
  262. difference_type operator - (const self& ri) const
  263. {
  264. return _index - ri._index;
  265. }
  266. bool operator < (const self& ri) const
  267. {
  268. return _index < ri._index;
  269. }
  270. reference
  271. operator[](difference_type n) const
  272. {
  273. return reference(_seq, _index + n);
  274. }
  275. private:
  276. VALUE _seq;
  277. difference_type _index;
  278. };
  279. /**
  280. * This adaptor class allows you to use a Ruby Array as if it was an STL
  281. * container, giving it begin(), end(), and iterators.
  282. */
  283. template <class T>
  284. struct RubySequence_Cont
  285. {
  286. typedef RubySequence_Ref<T> reference;
  287. typedef const RubySequence_Ref<T> const_reference;
  288. typedef T value_type;
  289. typedef T* pointer;
  290. typedef int difference_type;
  291. typedef int size_type;
  292. typedef const pointer const_pointer;
  293. typedef RubySequence_InputIterator<T, reference> iterator;
  294. typedef RubySequence_InputIterator<T, const_reference> const_iterator;
  295. RubySequence_Cont(VALUE seq) : _seq(0)
  296. {
  297. if (!rb_obj_is_kind_of(seq, rb_cArray)) {
  298. throw std::invalid_argument("an Array is expected");
  299. }
  300. _seq = seq;
  301. }
  302. ~RubySequence_Cont()
  303. {
  304. }
  305. size_type size() const
  306. {
  307. return RARRAY_LEN(_seq);
  308. }
  309. bool empty() const
  310. {
  311. return size() == 0;
  312. }
  313. iterator begin()
  314. {
  315. return iterator(_seq, 0);
  316. }
  317. const_iterator begin() const
  318. {
  319. return const_iterator(_seq, 0);
  320. }
  321. iterator end()
  322. {
  323. return iterator(_seq, size());
  324. }
  325. const_iterator end() const
  326. {
  327. return const_iterator(_seq, size());
  328. }
  329. reference operator[](difference_type n)
  330. {
  331. return reference(_seq, n);
  332. }
  333. const_reference operator[](difference_type n) const
  334. {
  335. return const_reference(_seq, n);
  336. }
  337. bool check(bool set_err = false) const
  338. {
  339. int s = (int) size();
  340. for (int i = 0; i < s; ++i) {
  341. VALUE item = rb_ary_entry(_seq, i );
  342. if (!swig::check<value_type>(item)) {
  343. if (set_err) {
  344. char msg[1024];
  345. sprintf(msg, "in sequence element %d", i);
  346. SWIG_Error(SWIG_RuntimeError, msg);
  347. }
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. private:
  354. VALUE _seq;
  355. };
  356. }
  357. }
  358. /**
  359. * Macros used to typemap an STL iterator -> SWIGIterator conversion.
  360. */
  361. %define %swig_sequence_iterator(Sequence...)
  362. #if defined(SWIG_EXPORT_ITERATOR_METHODS)
  363. %typemap(out,noblock=1,fragment="RubySequence_Cont")
  364. const_iterator, const_reverse_iterator {
  365. $result = SWIG_NewPointerObj(swig::make_const_iterator(%static_cast($1,const $type &),
  366. self),
  367. swig::ConstIterator::descriptor(),SWIG_POINTER_OWN);
  368. }
  369. %typemap(out,noblock=1,fragment="RubySequence_Cont")
  370. iterator, reverse_iterator {
  371. $result = SWIG_NewPointerObj(swig::make_nonconst_iterator(%static_cast($1,const $type &),
  372. self),
  373. swig::Iterator::descriptor(),SWIG_POINTER_OWN);
  374. }
  375. %typemap(out,noblock=1,fragment="RubySequence_Cont")
  376. std::pair<const_iterator, const_iterator> {
  377. $result = rb_ary_new2(2);
  378. rb_ary_push($result, SWIG_NewPointerObj(swig::make_const_iterator(%static_cast($1,const $type &).first),
  379. swig::ConstIterator::descriptor(),SWIG_POINTER_OWN));
  380. rb_ary_push($result, SWIG_NewPointerObj(swig::make_const_iterator(%static_cast($1,const $type &).second),
  381. swig::ConstIterator::descriptor(),SWIG_POINTER_OWN));
  382. }
  383. // std::map/multimap/set allow returning std::pair< iterator, iterator > from
  384. // equal_range, but we cannot still modify the key, so the iterator is
  385. // const.
  386. %typemap(out,noblock=1,fragment="RubySequence_Cont")
  387. std::pair<iterator, iterator> {
  388. $result = rb_ary_new2(2);
  389. rb_ary_push($result, SWIG_NewPointerObj(swig::make_const_iterator(%static_cast($1,const $type &).first),
  390. swig::ConstIterator::descriptor(),SWIG_POINTER_OWN));
  391. rb_ary_push($result, SWIG_NewPointerObj(swig::make_const_iterator(%static_cast($1,const $type &).second),
  392. swig::ConstIterator::descriptor(),SWIG_POINTER_OWN));
  393. }
  394. %typemap(in,noblock=1,fragment="RubySequence_Cont")
  395. const_iterator(swig::ConstIterator *iter = 0, int res),
  396. const_reverse_iterator(swig::ConstIterator *iter = 0, int res) {
  397. res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter),
  398. swig::ConstIterator::descriptor(), 0);
  399. if (!SWIG_IsOK(res) || !iter) {
  400. %argument_fail(SWIG_TypeError, "$type", $symname, $argnum);
  401. } else {
  402. swig::ConstIterator_T<$type > *iter_t = dynamic_cast<swig::ConstIterator_T<$type > *>(iter);
  403. if (iter_t) {
  404. $1 = iter_t->get_current();
  405. } else {
  406. %argument_fail(SWIG_TypeError, "$type", $symname, $argnum);
  407. }
  408. }
  409. }
  410. %typemap(in,noblock=1,fragment="RubySequence_Cont")
  411. iterator(swig::Iterator *iter = 0, int res),
  412. reverse_iterator(swig::Iterator *iter = 0, int res) {
  413. res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter), swig::Iterator::descriptor(), 0);
  414. if (!SWIG_IsOK(res) || !iter) {
  415. %argument_fail(SWIG_TypeError, "$type", $symname, $argnum);
  416. } else {
  417. swig::Iterator_T<$type > *iter_t = dynamic_cast<swig::Iterator_T<$type > *>(iter);
  418. if (iter_t) {
  419. $1 = iter_t->get_current();
  420. } else {
  421. %argument_fail(SWIG_TypeError, "$type", $symname, $argnum);
  422. }
  423. }
  424. }
  425. %typecheck(%checkcode(ITERATOR),noblock=1,fragment="RubySequence_Cont")
  426. const_iterator, const_reverse_iterator {
  427. swig::ConstIterator *iter = 0;
  428. int res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter),
  429. swig::ConstIterator::descriptor(), 0);
  430. $1 = (SWIG_IsOK(res) && iter && (dynamic_cast<swig::ConstIterator_T<$type > *>(iter) != 0));
  431. }
  432. %typecheck(%checkcode(ITERATOR),noblock=1,fragment="RubySequence_Cont")
  433. iterator, reverse_iterator {
  434. swig::ConstIterator *iter = 0;
  435. int res = SWIG_ConvertPtr($input, %as_voidptrptr(&iter),
  436. swig::Iterator::descriptor(), 0);
  437. $1 = (SWIG_IsOK(res) && iter && (dynamic_cast<swig::Iterator_T<$type > *>(iter) != 0));
  438. }
  439. %fragment("RubySequence_Cont");
  440. // %newobject iterator;
  441. // %newobject const_iterator;
  442. // %extend {
  443. // swig::Iterator* iterator(VALUE* RUBY_SELF) {
  444. // return swig::make_nonconst_iterator($self->begin(), $self->begin(),
  445. // $self->end(), *RUBY_SELF);
  446. // }
  447. // swig::ConstIterator* const_iterator(VALUE* RUBY_SELF) {
  448. // return swig::make_const_iterator($self->begin(), $self->begin(),
  449. // $self->end(), *RUBY_SELF);
  450. // }
  451. // }
  452. #endif //SWIG_EXPORT_ITERATOR_METHODS
  453. %enddef
  454. /**** The Ruby container methods ****/
  455. %define %swig_container_methods(Container...)
  456. %extend {
  457. %newobject dup;
  458. Container* dup()
  459. {
  460. return new Container(*$self);
  461. }
  462. }
  463. %enddef
  464. /**
  465. * Macro used to define common Ruby printing methods for STL container
  466. */
  467. %define %swig_sequence_printing_methods(Sequence...)
  468. %extend {
  469. VALUE inspect()
  470. {
  471. Sequence::const_iterator i = $self->begin();
  472. Sequence::const_iterator e = $self->end();
  473. const char *type_name = swig::type_name< Sequence >();
  474. VALUE str = rb_str_new2(type_name);
  475. str = rb_str_cat2( str, " [" );
  476. bool comma = false;
  477. VALUE tmp;
  478. for ( ; i != e; ++i, comma = true )
  479. {
  480. if (comma) str = rb_str_cat2( str, "," );
  481. tmp = swig::from< Sequence::value_type >( *i );
  482. tmp = rb_inspect( tmp );
  483. str = rb_str_buf_append( str, tmp );
  484. }
  485. str = rb_str_cat2( str, "]" );
  486. return str;
  487. }
  488. VALUE to_a()
  489. {
  490. Sequence::const_iterator i = $self->begin();
  491. Sequence::const_iterator e = $self->end();
  492. VALUE ary = rb_ary_new2( std::distance( i, e ) );
  493. VALUE tmp;
  494. for ( ; i != e; ++i )
  495. {
  496. tmp = swig::from< Sequence::value_type >( *i );
  497. rb_ary_push( ary, tmp );
  498. }
  499. return ary;
  500. }
  501. VALUE to_s()
  502. {
  503. Sequence::iterator i = $self->begin();
  504. Sequence::iterator e = $self->end();
  505. VALUE str = rb_str_new2( "" );
  506. VALUE tmp;
  507. for ( ; i != e; ++i )
  508. {
  509. tmp = swig::from< Sequence::value_type >( *i );
  510. tmp = rb_obj_as_string( tmp );
  511. str = rb_str_buf_append( str, tmp );
  512. }
  513. return str;
  514. }
  515. }
  516. %enddef
  517. /**
  518. * Macro used to add common methods to all STL sequence-type containers
  519. */
  520. %define %swig_sequence_methods_non_resizable_common(Sequence...)
  521. %swig_container_methods(%arg(Sequence))
  522. %swig_sequence_iterator(%arg(Sequence))
  523. %swig_sequence_printing_methods(%arg(Sequence))
  524. %fragment("RubySequence_Base");
  525. %extend {
  526. VALUE slice( difference_type i, difference_type length ) throw (std::invalid_argument) {
  527. if ( length < 0 )
  528. return Qnil;
  529. std::size_t len = $self->size();
  530. if ( i < 0 ) {
  531. if ( i + static_cast<Sequence::difference_type>(len) < 0 )
  532. return Qnil;
  533. else
  534. i = len + i;
  535. }
  536. Sequence::difference_type j = length + i;
  537. if ( j > static_cast<Sequence::difference_type>(len) )
  538. j = len;
  539. VALUE r = Qnil;
  540. try {
  541. r = swig::from< const Sequence* >( swig::getslice(self, i, j) );
  542. }
  543. catch( std::out_of_range ) {
  544. }
  545. return r;
  546. }
  547. Sequence* each()
  548. {
  549. if ( !rb_block_given_p() )
  550. rb_raise( rb_eArgError, "no block given");
  551. VALUE r;
  552. Sequence::const_iterator i = self->begin();
  553. Sequence::const_iterator e = self->end();
  554. for ( ; i != e; ++i )
  555. {
  556. r = swig::from< Sequence::value_type >(*i);
  557. rb_yield(r);
  558. }
  559. return self;
  560. }
  561. VALUE __delete2__(const value_type& i) {
  562. VALUE r = Qnil;
  563. return r;
  564. }
  565. }
  566. %enddef
  567. %define %swig_sequence_methods_resizable_common(Sequence...)
  568. %extend {
  569. %newobject select;
  570. Sequence* select() {
  571. if ( !rb_block_given_p() )
  572. rb_raise( rb_eArgError, "no block given" );
  573. Sequence* r = new Sequence();
  574. Sequence::const_iterator i = $self->begin();
  575. Sequence::const_iterator e = $self->end();
  576. for ( ; i != e; ++i )
  577. {
  578. VALUE v = swig::from< Sequence::value_type >(*i);
  579. if ( RTEST( rb_yield(v) ) )
  580. $self->insert( r->end(), *i);
  581. }
  582. return r;
  583. }
  584. VALUE delete_at(difference_type i) {
  585. VALUE r = Qnil;
  586. try {
  587. Sequence::iterator at = swig::getpos(self, i);
  588. r = swig::from< Sequence::value_type >( *(at) );
  589. $self->erase(at);
  590. }
  591. catch (std::out_of_range)
  592. {
  593. }
  594. return r;
  595. }
  596. }
  597. %enddef
  598. %define %swig_sequence_methods_common(Sequence...)
  599. %swig_sequence_methods_non_resizable_common(%arg(Sequence))
  600. %swig_sequence_methods_resizable_common(%arg(Sequence))
  601. %enddef
  602. /**
  603. * Macro used to add functions for back insertion of values in
  604. * STL sequence containers
  605. */
  606. %define %swig_sequence_back_inserters( Sequence... )
  607. %extend {
  608. VALUE pop() {
  609. if ($self->empty()) return Qnil;
  610. Sequence::value_type x = self->back();
  611. $self->pop_back();
  612. return swig::from< Sequence::value_type >( x );
  613. }
  614. %alias push "<<";
  615. const value_type push( const value_type& e ) {
  616. $self->push_back( e );
  617. return e;
  618. }
  619. %newobject reject;
  620. Sequence* reject() {
  621. if ( !rb_block_given_p() )
  622. rb_raise( rb_eArgError, "no block given" );
  623. Sequence* r = new Sequence();
  624. std::remove_copy_if( $self->begin(), $self->end(),
  625. std::back_inserter(*r),
  626. swig::yield< Sequence::value_type >() );
  627. return r;
  628. }
  629. }
  630. %enddef
  631. %define %swig_sequence_methods_extra(Sequence...)
  632. %extend {
  633. %alias reject_bang "delete_if";
  634. Sequence* reject_bang() {
  635. if ( !rb_block_given_p() )
  636. rb_raise( rb_eArgError, "no block given" );
  637. $self->erase( std::remove_if( $self->begin(), $self->end(),
  638. swig::yield< Sequence::value_type >() ), $self->end() );
  639. return $self;
  640. }
  641. }
  642. %enddef
  643. %define %swig_sequence_methods_non_resizable_accessors(Sequence...)
  644. %extend {
  645. VALUE at(difference_type i) const {
  646. VALUE r = Qnil;
  647. try {
  648. r = swig::from< Sequence::value_type >( *(swig::cgetpos(self, i)) );
  649. }
  650. catch( std::out_of_range ) {
  651. }
  652. return r;
  653. }
  654. VALUE __getitem__(difference_type i, difference_type length) const throw (std::invalid_argument) {
  655. if ( length < 0 )
  656. return Qnil;
  657. std::size_t len = $self->size();
  658. if ( i < 0 ) {
  659. if ( i + static_cast<Sequence::difference_type>(len) < 0 )
  660. return Qnil;
  661. else
  662. i = len + i;
  663. }
  664. Sequence::difference_type j = length + i;
  665. if ( j > static_cast<Sequence::difference_type>(len) )
  666. j = len;
  667. VALUE r = Qnil;
  668. try {
  669. r = swig::from< const Sequence* >( swig::getslice(self, i, j) );
  670. }
  671. catch( std::out_of_range ) {
  672. }
  673. return r;
  674. }
  675. VALUE __getitem__(difference_type i) const {
  676. VALUE r = Qnil;
  677. try {
  678. r = swig::from< Sequence::value_type >( *(swig::cgetpos(self, i)) );
  679. }
  680. catch( std::out_of_range ) {
  681. }
  682. return r;
  683. }
  684. VALUE __getitem__(VALUE i) const throw (std::invalid_argument) {
  685. if ( rb_obj_is_kind_of( i, rb_cRange ) == Qfalse ) {
  686. rb_raise( rb_eTypeError, "not a valid index or range" );
  687. }
  688. static ID id_end = rb_intern("end");
  689. static ID id_start = rb_intern("begin");
  690. static ID id_noend = rb_intern("exclude_end?");
  691. VALUE start = rb_funcall( i, id_start, 0 );
  692. VALUE end = rb_funcall( i, id_end, 0 );
  693. bool noend = ( rb_funcall( i, id_noend, 0 ) == Qtrue );
  694. int len = $self->size();
  695. int s = NUM2INT( start );
  696. if ( s < 0 ) {
  697. s = len + s;
  698. if ( s < 0 )
  699. return Qnil;
  700. } else if ( s > len )
  701. return Qnil;
  702. int e = NUM2INT( end );
  703. if ( e < 0 ) e = len + e;
  704. if ( noend ) e -= 1;
  705. if ( e < 0 ) e = -1;
  706. if ( e >= len ) e = len - 1;
  707. if ( s == len ) e = len - 1;
  708. return swig::from< Sequence* >( swig::getslice(self, s, e+1) );
  709. }
  710. VALUE __setitem__(difference_type i, const value_type& x) throw (std::invalid_argument, std::out_of_range)
  711. {
  712. if ( i >= static_cast<Sequence::difference_type>( $self->size()) )
  713. swig::resize( $self, i+1, x );
  714. else
  715. *(swig::getpos($self, i)) = x;
  716. return swig::from< Sequence::value_type >( x );
  717. }
  718. VALUE __setitem__(difference_type i, difference_type length, const Sequence& v) throw (std::invalid_argument) {
  719. if ( length < 0 )
  720. return Qnil;
  721. std::size_t len = $self->size();
  722. if ( i < 0 ) {
  723. if ( i + static_cast<Sequence::difference_type>(len) < 0 )
  724. return Qnil;
  725. else
  726. i = len + i;
  727. }
  728. Sequence::difference_type j = length + i;
  729. if ( j > static_cast<Sequence::difference_type>(len) ) {
  730. swig::resize( $self, j, *(v.begin()) );
  731. }
  732. VALUE r = Qnil;
  733. swig::setslice($self, i, j, v);
  734. r = swig::from< const Sequence* >( &v );
  735. return r;
  736. }
  737. }
  738. %enddef
  739. /**
  740. * Macro used to add functions for non resizable sequences
  741. */
  742. %define %swig_sequence_methods_non_resizable(Sequence...)
  743. %swig_sequence_methods_non_resizable_common(%arg(Sequence))
  744. %swig_sequence_methods_non_resizable_accessors(%arg(Sequence))
  745. %enddef
  746. /**
  747. * Macro used to add functions for sequences
  748. */
  749. %define %swig_sequence_methods(Sequence...)
  750. %swig_sequence_methods_non_resizable_common(%arg(Sequence))
  751. %swig_sequence_methods_resizable_common(%arg(Sequence))
  752. %swig_sequence_methods_non_resizable_accessors(%arg(Sequence))
  753. %swig_sequence_methods_extra(%arg(Sequence));
  754. %swig_sequence_back_inserters(%arg(Sequence));
  755. %enddef
  756. %define %swig_sequence_methods_non_resizable_val(Sequence...)
  757. %swig_sequence_methods_non_resizable(%arg(Sequence))
  758. %enddef
  759. %define %swig_sequence_methods_val(Sequence...)
  760. %swig_sequence_methods(%arg(Sequence))
  761. %enddef
  762. /**
  763. * Macro used to add functions for front insertion of
  764. * elements in STL sequence containers that support it.
  765. */
  766. %define %swig_sequence_front_inserters( Sequence... )
  767. %extend {
  768. VALUE shift()
  769. {
  770. if ($self->empty()) return Qnil;
  771. Sequence::value_type x = self->front();
  772. $self->erase( $self->begin() );
  773. return swig::from< Sequence::value_type >( x );
  774. }
  775. %typemap(in) (int argc, VALUE* argv) {
  776. $1 = argc - 1;
  777. $2 = argv + 1;
  778. }
  779. Sequence* insert( difference_type pos, int argc, VALUE* argv, ... )
  780. {
  781. std::size_t len = $self->size();
  782. std::size_t i = swig::check_index( pos, len, true );
  783. Sequence::iterator start;
  784. VALUE elem = argv[0];
  785. int idx = 0;
  786. try {
  787. Sequence::value_type val = swig::as<Sequence::value_type>( elem, true );
  788. if ( i >= len ) {
  789. $self->resize(i-1, val);
  790. return $self;
  791. }
  792. start = $self->begin();
  793. std::advance( start, i );
  794. $self->insert( start++, val );
  795. for ( ++idx; idx < argc; ++idx )
  796. {
  797. elem = argv[idx];
  798. val = swig::as<Sequence::value_type>( elem );
  799. $self->insert( start++, val );
  800. }
  801. }
  802. catch( std::invalid_argument )
  803. {
  804. rb_raise( rb_eArgError, "%s",
  805. Ruby_Format_TypeError( "",
  806. swig::type_name<Sequence::value_type>(),
  807. __FUNCTION__, idx+2, elem ));
  808. }
  809. return $self;
  810. }
  811. %typemap(in) (int argc, VALUE* argv) {
  812. $1 = argc;
  813. $2 = argv;
  814. }
  815. Sequence* unshift( int argc, VALUE* argv, ... )
  816. {
  817. for ( int idx = argc-1; idx >= 0; --idx )
  818. {
  819. Sequence::iterator start = $self->begin();
  820. VALUE elem = argv[idx];
  821. try {
  822. Sequence::value_type val = swig::as<Sequence::value_type>( elem, true );
  823. $self->insert( start, val );
  824. }
  825. catch( std::invalid_argument )
  826. {
  827. rb_raise( rb_eArgError, "%s",
  828. Ruby_Format_TypeError( "",
  829. swig::type_name<Sequence::value_type>(),
  830. __FUNCTION__, idx+2, elem ));
  831. }
  832. }
  833. return $self;
  834. }
  835. }
  836. %enddef
  837. //
  838. // Common fragments
  839. //
  840. %fragment("StdSequenceTraits","header",
  841. fragment="StdTraits",
  842. fragment="RubySequence_Cont",
  843. fragment="GC_VALUE_definition")
  844. {
  845. namespace swig {
  846. template <class RubySeq, class Seq>
  847. inline void
  848. assign(const RubySeq& rubyseq, Seq* seq) {
  849. // seq->assign(rubyseq.begin(), rubyseq.end()); // not used as not always implemented
  850. typedef typename RubySeq::value_type value_type;
  851. typename RubySeq::const_iterator it = rubyseq.begin();
  852. for (;it != rubyseq.end(); ++it) {
  853. seq->insert(seq->end(),(value_type)(*it));
  854. }
  855. }
  856. template <class Seq, class T = typename Seq::value_type >
  857. struct traits_asptr_stdseq {
  858. typedef Seq sequence;
  859. typedef T value_type;
  860. static int asptr(VALUE obj, sequence **seq) {
  861. if (rb_obj_is_kind_of(obj, rb_cArray) == Qtrue) {
  862. try {
  863. RubySequence_Cont<value_type> rubyseq(obj);
  864. if (seq) {
  865. sequence *pseq = new sequence();
  866. assign(rubyseq, pseq);
  867. *seq = pseq;
  868. return SWIG_NEWOBJ;
  869. } else {
  870. return rubyseq.check() ? SWIG_OK : SWIG_ERROR;
  871. }
  872. } catch (std::exception& e) {
  873. if (seq) {
  874. VALUE lastErr = rb_gv_get("$!");
  875. if (lastErr == Qnil) {
  876. rb_raise(rb_eTypeError, "%s", e.what());
  877. }
  878. }
  879. return SWIG_ERROR;
  880. }
  881. } else {
  882. sequence *p;
  883. if (SWIG_ConvertPtr(obj,(void**)&p,
  884. swig::type_info<sequence>(),0) == SWIG_OK) {
  885. if (seq) *seq = p;
  886. return SWIG_OLDOBJ;
  887. }
  888. }
  889. return SWIG_ERROR;
  890. }
  891. };
  892. // Partial specialization for GC_VALUE's. No need to typecheck each
  893. // element.
  894. template< class Seq >
  895. struct traits_asptr_stdseq< Seq, swig::GC_VALUE > {
  896. typedef Seq sequence;
  897. typedef swig::GC_VALUE value_type;
  898. static int asptr(VALUE obj, sequence **seq) {
  899. if (rb_obj_is_kind_of(obj, rb_cArray) == Qtrue) {
  900. try {
  901. if (seq) {
  902. RubySequence_Cont<value_type> rubyseq(obj);
  903. sequence *pseq = new sequence();
  904. assign(rubyseq, pseq);
  905. *seq = pseq;
  906. return SWIG_NEWOBJ;
  907. } else {
  908. return true;
  909. }
  910. } catch (std::exception& e) {
  911. if (seq) {
  912. VALUE lastErr = rb_gv_get("$!");
  913. if (lastErr == Qnil) {
  914. rb_raise(rb_eTypeError, "%s", e.what());
  915. }
  916. }
  917. return SWIG_ERROR;
  918. }
  919. } else {
  920. sequence *p;
  921. if (SWIG_ConvertPtr(obj,(void**)&p,
  922. swig::type_info<sequence>(),0) == SWIG_OK) {
  923. if (seq) *seq = p;
  924. return SWIG_OLDOBJ;
  925. }
  926. }
  927. return SWIG_ERROR;
  928. }
  929. };
  930. template <class Seq, class T = typename Seq::value_type >
  931. struct traits_from_stdseq {
  932. typedef Seq sequence;
  933. typedef T value_type;
  934. typedef typename Seq::size_type size_type;
  935. typedef typename sequence::const_iterator const_iterator;
  936. static VALUE from(const sequence& seq) {
  937. #ifdef SWIG_RUBY_EXTRA_NATIVE_CONTAINERS
  938. swig_type_info *desc = swig::type_info<sequence>();
  939. if (desc && desc->clientdata) {
  940. return SWIG_NewPointerObj(new sequence(seq), desc, SWIG_POINTER_OWN);
  941. }
  942. #endif
  943. size_type size = seq.size();
  944. if (size <= (size_type)INT_MAX) {
  945. VALUE obj = rb_ary_new2((int)size);
  946. int i = 0;
  947. for (const_iterator it = seq.begin();
  948. it != seq.end(); ++it, ++i) {
  949. rb_ary_push(obj, swig::from< value_type >(*it));
  950. }
  951. rb_obj_freeze(obj); // treat as immutable result
  952. return obj;
  953. } else {
  954. rb_raise(rb_eRangeError,"sequence size not valid in ruby");
  955. return Qnil;
  956. }
  957. }
  958. };
  959. }
  960. }
  961. %include <rubycontainer_extended.swg>