PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/octave/octiterators.swg

#
Unknown | 357 lines | 294 code | 63 blank | 0 comment | 0 complexity | 1444632bfef3ef3354453c19650eefc9 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * octiterators.swg
  3. *
  4. * Users can derive form the OctSwigIterator to implemet their
  5. * own iterators. As an example (real one since we use it for STL/STD
  6. * containers), the template OctSwigIterator_T does the
  7. * implementation for generic C++ iterators.
  8. * ----------------------------------------------------------------------------- */
  9. %include <std_common.i>
  10. %fragment("OctSwigIterator","header",fragment="<stddef.h>") {
  11. namespace swig {
  12. struct stop_iteration {
  13. };
  14. struct OctSwigIterator {
  15. private:
  16. octave_value _seq;
  17. protected:
  18. OctSwigIterator(octave_value seq) : _seq(seq)
  19. {
  20. }
  21. public:
  22. virtual ~OctSwigIterator() {}
  23. virtual octave_value value() const = 0;
  24. virtual OctSwigIterator *incr(size_t n = 1) = 0;
  25. virtual OctSwigIterator *decr(size_t n = 1)
  26. {
  27. throw stop_iteration();
  28. }
  29. virtual ptrdiff_t distance(const OctSwigIterator &x) const
  30. {
  31. throw std::invalid_argument("operation not supported");
  32. }
  33. virtual bool equal (const OctSwigIterator &x) const
  34. {
  35. throw std::invalid_argument("operation not supported");
  36. }
  37. virtual OctSwigIterator *copy() const = 0;
  38. octave_value next()
  39. {
  40. octave_value obj = value();
  41. incr();
  42. return obj;
  43. }
  44. octave_value previous()
  45. {
  46. decr();
  47. return value();
  48. }
  49. OctSwigIterator *advance(ptrdiff_t n)
  50. {
  51. return (n > 0) ? incr(n) : decr(-n);
  52. }
  53. bool operator == (const OctSwigIterator& x) const
  54. {
  55. return equal(x);
  56. }
  57. bool operator != (const OctSwigIterator& x) const
  58. {
  59. return ! operator==(x);
  60. }
  61. OctSwigIterator* operator ++ () {
  62. incr();
  63. return this;
  64. }
  65. OctSwigIterator* operator -- () {
  66. decr();
  67. return this;
  68. }
  69. OctSwigIterator* operator + (ptrdiff_t n) const
  70. {
  71. return copy()->advance(n);
  72. }
  73. OctSwigIterator* operator - (ptrdiff_t n) const
  74. {
  75. return copy()->advance(-n);
  76. }
  77. ptrdiff_t operator - (const OctSwigIterator& x) const
  78. {
  79. return x.distance(*this);
  80. }
  81. static swig_type_info* descriptor() {
  82. static int init = 0;
  83. static swig_type_info* desc = 0;
  84. if (!init) {
  85. desc = SWIG_TypeQuery("swig::OctSwigIterator *");
  86. init = 1;
  87. }
  88. return desc;
  89. }
  90. };
  91. }
  92. }
  93. %fragment("OctSwigIterator_T","header",fragment="<stddef.h>",fragment="OctSwigIterator",fragment="StdTraits",fragment="StdIteratorTraits") {
  94. namespace swig {
  95. template<typename OutIterator>
  96. class OctSwigIterator_T : public OctSwigIterator
  97. {
  98. public:
  99. typedef OutIterator out_iterator;
  100. typedef typename std::iterator_traits<out_iterator>::value_type value_type;
  101. typedef OctSwigIterator_T<out_iterator> self_type;
  102. OctSwigIterator_T(out_iterator curr, octave_value seq)
  103. : OctSwigIterator(seq), current(curr)
  104. {
  105. }
  106. const out_iterator& get_current() const
  107. {
  108. return current;
  109. }
  110. bool equal (const OctSwigIterator &iter) const
  111. {
  112. const self_type *iters = dynamic_cast<const self_type *>(&iter);
  113. if (iters) {
  114. return (current == iters->get_current());
  115. } else {
  116. throw std::invalid_argument("bad iterator type");
  117. }
  118. }
  119. ptrdiff_t distance(const OctSwigIterator &iter) const
  120. {
  121. const self_type *iters = dynamic_cast<const self_type *>(&iter);
  122. if (iters) {
  123. return std::distance(current, iters->get_current());
  124. } else {
  125. throw std::invalid_argument("bad iterator type");
  126. }
  127. }
  128. protected:
  129. out_iterator current;
  130. };
  131. template <class ValueType>
  132. struct from_oper
  133. {
  134. typedef const ValueType& argument_type;
  135. typedef octave_value result_type;
  136. result_type operator()(argument_type v) const
  137. {
  138. return swig::from(v);
  139. }
  140. };
  141. template<typename OutIterator,
  142. typename ValueType = typename std::iterator_traits<OutIterator>::value_type,
  143. typename FromOper = from_oper<ValueType> >
  144. class OctSwigIteratorOpen_T : public OctSwigIterator_T<OutIterator>
  145. {
  146. public:
  147. FromOper from;
  148. typedef OutIterator out_iterator;
  149. typedef ValueType value_type;
  150. typedef OctSwigIterator_T<out_iterator> base;
  151. typedef OctSwigIteratorOpen_T<OutIterator, ValueType, FromOper> self_type;
  152. OctSwigIteratorOpen_T(out_iterator curr, octave_value seq)
  153. : OctSwigIterator_T<OutIterator>(curr, seq)
  154. {
  155. }
  156. octave_value value() const {
  157. return from(static_cast<const value_type&>(*(base::current)));
  158. }
  159. OctSwigIterator *copy() const
  160. {
  161. return new self_type(*this);
  162. }
  163. OctSwigIterator *incr(size_t n = 1)
  164. {
  165. while (n--) {
  166. ++base::current;
  167. }
  168. return this;
  169. }
  170. OctSwigIterator *decr(size_t n = 1)
  171. {
  172. while (n--) {
  173. --base::current;
  174. }
  175. return this;
  176. }
  177. };
  178. template<typename OutIterator,
  179. typename ValueType = typename std::iterator_traits<OutIterator>::value_type,
  180. typename FromOper = from_oper<ValueType> >
  181. class OctSwigIteratorClosed_T : public OctSwigIterator_T<OutIterator>
  182. {
  183. public:
  184. FromOper from;
  185. typedef OutIterator out_iterator;
  186. typedef ValueType value_type;
  187. typedef OctSwigIterator_T<out_iterator> base;
  188. typedef OctSwigIteratorClosed_T<OutIterator, ValueType, FromOper> self_type;
  189. OctSwigIteratorClosed_T(out_iterator curr, out_iterator first, out_iterator last, octave_value seq)
  190. : OctSwigIterator_T<OutIterator>(curr, seq), begin(first), end(last)
  191. {
  192. }
  193. octave_value value() const {
  194. if (base::current == end) {
  195. throw stop_iteration();
  196. } else {
  197. return from(static_cast<const value_type&>(*(base::current)));
  198. }
  199. }
  200. OctSwigIterator *copy() const
  201. {
  202. return new self_type(*this);
  203. }
  204. OctSwigIterator *incr(size_t n = 1)
  205. {
  206. while (n--) {
  207. if (base::current == end) {
  208. throw stop_iteration();
  209. } else {
  210. ++base::current;
  211. }
  212. }
  213. return this;
  214. }
  215. OctSwigIterator *decr(size_t n = 1)
  216. {
  217. while (n--) {
  218. if (base::current == begin) {
  219. throw stop_iteration();
  220. } else {
  221. --base::current;
  222. }
  223. }
  224. return this;
  225. }
  226. private:
  227. out_iterator begin;
  228. out_iterator end;
  229. };
  230. template<typename OutIter>
  231. inline OctSwigIterator*
  232. make_output_iterator(const OutIter& current, const OutIter& begin,const OutIter& end, octave_value seq = octave_value())
  233. {
  234. return new OctSwigIteratorClosed_T<OutIter>(current, begin, end, seq);
  235. }
  236. template<typename OutIter>
  237. inline OctSwigIterator*
  238. make_output_iterator(const OutIter& current, octave_value seq = octave_value())
  239. {
  240. return new OctSwigIteratorOpen_T<OutIter>(current, seq);
  241. }
  242. }
  243. }
  244. %fragment("OctSwigIterator");
  245. namespace swig
  246. {
  247. // Throw a StopIteration exception
  248. %ignore stop_iteration;
  249. struct stop_iteration {};
  250. %typemap(throws) stop_iteration {
  251. error("stop_iteration exception");
  252. SWIG_fail;
  253. }
  254. // Mark methods that return new objects
  255. %newobject OctSwigIterator::copy;
  256. %newobject OctSwigIterator::operator + (ptrdiff_t n) const;
  257. %newobject OctSwigIterator::operator - (ptrdiff_t n) const;
  258. %nodirector OctSwigIterator;
  259. %catches(swig::stop_iteration) OctSwigIterator::value() const;
  260. %catches(swig::stop_iteration) OctSwigIterator::incr(size_t n = 1);
  261. %catches(swig::stop_iteration) OctSwigIterator::decr(size_t n = 1);
  262. %catches(std::invalid_argument) OctSwigIterator::distance(const OctSwigIterator &x) const;
  263. %catches(std::invalid_argument) OctSwigIterator::equal (const OctSwigIterator &x) const;
  264. %catches(swig::stop_iteration) OctSwigIterator::next();
  265. %catches(swig::stop_iteration) OctSwigIterator::previous();
  266. %catches(swig::stop_iteration) OctSwigIterator::advance(ptrdiff_t n);
  267. %catches(swig::stop_iteration) OctSwigIterator::operator += (ptrdiff_t n);
  268. %catches(swig::stop_iteration) OctSwigIterator::operator -= (ptrdiff_t n);
  269. %catches(swig::stop_iteration) OctSwigIterator::operator + (ptrdiff_t n) const;
  270. %catches(swig::stop_iteration) OctSwigIterator::operator - (ptrdiff_t n) const;
  271. struct OctSwigIterator
  272. {
  273. protected:
  274. OctSwigIterator(octave_value seq);
  275. public:
  276. virtual ~OctSwigIterator();
  277. virtual octave_value value() const = 0;
  278. virtual OctSwigIterator *incr(size_t n = 1) = 0;
  279. virtual OctSwigIterator *decr(size_t n = 1);
  280. virtual ptrdiff_t distance(const OctSwigIterator &x) const;
  281. virtual bool equal (const OctSwigIterator &x) const;
  282. virtual OctSwigIterator *copy() const = 0;
  283. octave_value next();
  284. octave_value previous();
  285. OctSwigIterator *advance(ptrdiff_t n);
  286. bool operator == (const OctSwigIterator& x) const;
  287. bool operator != (const OctSwigIterator& x) const;
  288. OctSwigIterator* operator ++ ();
  289. OctSwigIterator* operator -- ();
  290. OctSwigIterator* operator + (ptrdiff_t n) const;
  291. OctSwigIterator* operator - (ptrdiff_t n) const;
  292. ptrdiff_t operator - (const OctSwigIterator& x) const;
  293. };
  294. }