any_iterator_wrapper.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2010. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_DETAIL_ANY_ITERATOR_WRAPPER_HPP_INCLUDED
  11. #define BOOST_RANGE_DETAIL_ANY_ITERATOR_WRAPPER_HPP_INCLUDED
  12. #include <boost/polymorphic_cast.hpp>
  13. #include <boost/range/config.hpp>
  14. #include <boost/range/detail/any_iterator_interface.hpp>
  15. #include <boost/range/concepts.hpp>
  16. namespace boost
  17. {
  18. namespace range_detail
  19. {
  20. template<typename TargetT, typename SourceT>
  21. TargetT& polymorphic_ref_downcast(SourceT& source)
  22. {
  23. #ifdef BOOST_NO_RTTI
  24. return static_cast<TargetT&>(source);
  25. #else
  26. return *boost::polymorphic_downcast<TargetT*>(&source);
  27. #endif
  28. }
  29. template<class Reference, class T>
  30. Reference dereference_cast(T& x)
  31. {
  32. return static_cast<Reference>(x);
  33. }
  34. template<class Reference, class T>
  35. Reference dereference_cast(const T& x)
  36. {
  37. return static_cast<Reference>(const_cast<T&>(x));
  38. }
  39. template<
  40. class WrappedIterator
  41. , class Reference
  42. , class Buffer
  43. >
  44. class any_incrementable_iterator_wrapper
  45. : public any_incrementable_iterator_interface<
  46. Reference
  47. , Buffer
  48. >
  49. {
  50. BOOST_RANGE_CONCEPT_ASSERT(( IncrementableIteratorConcept<WrappedIterator> ));
  51. public:
  52. typedef WrappedIterator wrapped_type;
  53. BOOST_STATIC_ASSERT(( is_convertible<
  54. typename iterator_reference<WrappedIterator>::type
  55. , Reference
  56. >::value ));
  57. any_incrementable_iterator_wrapper()
  58. : m_it()
  59. {}
  60. explicit any_incrementable_iterator_wrapper(wrapped_type it)
  61. : m_it(it)
  62. {}
  63. // any_incrementable_iterator implementation
  64. virtual any_incrementable_iterator_wrapper* clone(
  65. typename any_incrementable_iterator_wrapper::buffer_type& buffer
  66. ) const
  67. {
  68. return new (buffer.allocate(sizeof(*this)))
  69. any_incrementable_iterator_wrapper(m_it);
  70. }
  71. virtual any_incrementable_iterator_wrapper<
  72. WrappedIterator
  73. , typename any_incrementable_iterator_wrapper::const_reference
  74. , Buffer
  75. >* clone_const_ref(
  76. typename any_incrementable_iterator_wrapper::buffer_type& buffer
  77. ) const
  78. {
  79. typedef any_incrementable_iterator_wrapper<
  80. WrappedIterator
  81. , typename any_incrementable_iterator_wrapper::const_reference
  82. , Buffer
  83. > result_type;
  84. return new (buffer.allocate(sizeof(result_type)))
  85. result_type(m_it);
  86. }
  87. virtual any_incrementable_iterator_wrapper<
  88. WrappedIterator
  89. , typename any_incrementable_iterator_wrapper::reference_as_value_type
  90. , Buffer
  91. >* clone_reference_as_value(
  92. typename any_incrementable_iterator_wrapper::buffer_type& buffer
  93. ) const
  94. {
  95. typedef any_incrementable_iterator_wrapper<
  96. WrappedIterator
  97. , typename any_incrementable_iterator_wrapper::reference_as_value_type
  98. , Buffer
  99. > result_type;
  100. return new (buffer.allocate(sizeof(result_type)))
  101. result_type(m_it);
  102. }
  103. virtual void increment()
  104. {
  105. ++m_it;
  106. }
  107. private:
  108. wrapped_type m_it;
  109. };
  110. template<
  111. class WrappedIterator
  112. , class Reference
  113. , class Buffer
  114. >
  115. class any_single_pass_iterator_wrapper
  116. : public any_single_pass_iterator_interface<
  117. Reference
  118. , Buffer
  119. >
  120. {
  121. struct disabler {};
  122. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassIteratorConcept<WrappedIterator> ));
  123. typedef any_single_pass_iterator_interface<
  124. Reference,
  125. Buffer
  126. > base_type;
  127. public:
  128. typedef typename base_type::reference reference;
  129. any_single_pass_iterator_wrapper()
  130. : m_it()
  131. {}
  132. explicit any_single_pass_iterator_wrapper(const WrappedIterator& it)
  133. : m_it(it)
  134. {}
  135. // any_single_pass_iterator_interface<Reference> implementation
  136. virtual any_single_pass_iterator_wrapper* clone(
  137. typename any_single_pass_iterator_wrapper::buffer_type& buffer
  138. ) const
  139. {
  140. return new (buffer.allocate(sizeof(*this)))
  141. any_single_pass_iterator_wrapper(m_it);
  142. }
  143. virtual any_single_pass_iterator_wrapper<
  144. WrappedIterator
  145. , typename any_single_pass_iterator_wrapper::const_reference
  146. , Buffer
  147. >* clone_const_ref(
  148. typename any_single_pass_iterator_wrapper::buffer_type& buffer
  149. ) const
  150. {
  151. typedef any_single_pass_iterator_wrapper<
  152. WrappedIterator
  153. , typename any_single_pass_iterator_wrapper::const_reference
  154. , Buffer
  155. > result_type;
  156. return new (buffer.allocate(sizeof(result_type)))
  157. result_type(m_it);
  158. }
  159. virtual any_single_pass_iterator_wrapper<
  160. WrappedIterator
  161. , typename any_single_pass_iterator_wrapper::reference_as_value_type
  162. , Buffer
  163. >* clone_reference_as_value(
  164. typename any_single_pass_iterator_wrapper::buffer_type& buffer
  165. ) const
  166. {
  167. typedef any_single_pass_iterator_wrapper<
  168. WrappedIterator
  169. , typename any_single_pass_iterator_wrapper::reference_as_value_type
  170. , Buffer
  171. > result_type;
  172. return new (buffer.allocate(sizeof(result_type)))
  173. result_type(m_it);
  174. }
  175. virtual void increment()
  176. {
  177. ++m_it;
  178. }
  179. virtual bool equal(const any_single_pass_iterator_interface<Reference, Buffer>& other) const
  180. {
  181. return m_it == range_detail::polymorphic_ref_downcast<const any_single_pass_iterator_wrapper>(other).m_it;
  182. }
  183. virtual reference dereference() const
  184. {
  185. return dereference_cast<reference>(*m_it);
  186. }
  187. private:
  188. WrappedIterator m_it;
  189. };
  190. template<
  191. class WrappedIterator
  192. , class Reference
  193. , class Buffer
  194. >
  195. class any_forward_iterator_wrapper
  196. : public any_forward_iterator_interface<
  197. Reference
  198. , Buffer
  199. >
  200. {
  201. BOOST_RANGE_CONCEPT_ASSERT(( ForwardIteratorConcept<WrappedIterator> ));
  202. typedef any_forward_iterator_interface<
  203. Reference,
  204. Buffer
  205. > base_type;
  206. public:
  207. typedef typename base_type::reference reference;
  208. any_forward_iterator_wrapper()
  209. : m_it()
  210. {}
  211. explicit any_forward_iterator_wrapper(const WrappedIterator& it)
  212. : m_it(it)
  213. {}
  214. // any_forward_iterator_interface<Reference> implementation
  215. virtual any_forward_iterator_wrapper* clone(
  216. typename any_forward_iterator_wrapper::buffer_type& buffer
  217. ) const
  218. {
  219. return new (buffer.allocate(sizeof(*this)))
  220. any_forward_iterator_wrapper(m_it);
  221. }
  222. virtual any_forward_iterator_wrapper<
  223. WrappedIterator
  224. , typename any_forward_iterator_wrapper::const_reference
  225. , Buffer
  226. >* clone_const_ref(
  227. typename any_forward_iterator_wrapper::buffer_type& buffer
  228. ) const
  229. {
  230. typedef any_forward_iterator_wrapper<
  231. WrappedIterator
  232. , typename any_forward_iterator_wrapper::const_reference
  233. , Buffer
  234. > result_type;
  235. return new (buffer.allocate(sizeof(result_type)))
  236. result_type(m_it);
  237. }
  238. virtual any_forward_iterator_wrapper<
  239. WrappedIterator
  240. , typename any_forward_iterator_wrapper::reference_as_value_type
  241. , Buffer
  242. >* clone_reference_as_value(
  243. typename any_forward_iterator_wrapper::buffer_type& buffer
  244. ) const
  245. {
  246. typedef any_forward_iterator_wrapper<
  247. WrappedIterator
  248. , typename any_forward_iterator_wrapper::reference_as_value_type
  249. , Buffer
  250. > result_type;
  251. return new (buffer.allocate(sizeof(result_type)))
  252. result_type(m_it);
  253. }
  254. virtual void increment()
  255. {
  256. ++m_it;
  257. }
  258. virtual bool equal(const any_single_pass_iterator_interface<Reference, Buffer>& other) const
  259. {
  260. return m_it == range_detail::polymorphic_ref_downcast<const any_forward_iterator_wrapper>(other).m_it;
  261. }
  262. virtual reference dereference() const
  263. {
  264. return dereference_cast<reference>(*m_it);
  265. }
  266. private:
  267. WrappedIterator m_it;
  268. };
  269. template<
  270. class WrappedIterator
  271. , class Reference
  272. , class Buffer
  273. >
  274. class any_bidirectional_iterator_wrapper
  275. : public any_bidirectional_iterator_interface<
  276. Reference
  277. , Buffer
  278. >
  279. {
  280. BOOST_RANGE_CONCEPT_ASSERT(( BidirectionalIteratorConcept<WrappedIterator> ));
  281. typedef any_bidirectional_iterator_interface<
  282. Reference,
  283. Buffer
  284. > base_type;
  285. public:
  286. typedef typename base_type::reference reference;
  287. any_bidirectional_iterator_wrapper()
  288. : m_it()
  289. {
  290. }
  291. explicit any_bidirectional_iterator_wrapper(const WrappedIterator& it)
  292. : m_it(it)
  293. {
  294. }
  295. virtual any_bidirectional_iterator_wrapper* clone(
  296. typename any_bidirectional_iterator_wrapper::buffer_type& buffer
  297. ) const
  298. {
  299. return new (buffer.allocate(sizeof(*this)))
  300. any_bidirectional_iterator_wrapper(*this);
  301. }
  302. virtual any_bidirectional_iterator_wrapper<
  303. WrappedIterator
  304. , typename any_bidirectional_iterator_wrapper::const_reference
  305. , Buffer
  306. >* clone_const_ref(
  307. typename any_bidirectional_iterator_wrapper::buffer_type& buffer
  308. ) const
  309. {
  310. typedef any_bidirectional_iterator_wrapper<
  311. WrappedIterator
  312. , typename any_bidirectional_iterator_wrapper::const_reference
  313. , Buffer
  314. > result_type;
  315. return new (buffer.allocate(sizeof(result_type)))
  316. result_type(m_it);
  317. }
  318. virtual any_bidirectional_iterator_wrapper<
  319. WrappedIterator
  320. , typename any_bidirectional_iterator_wrapper::reference_as_value_type
  321. , Buffer
  322. >* clone_reference_as_value(
  323. typename any_bidirectional_iterator_wrapper::buffer_type& buffer
  324. ) const
  325. {
  326. typedef any_bidirectional_iterator_wrapper<
  327. WrappedIterator
  328. , typename any_bidirectional_iterator_wrapper::reference_as_value_type
  329. , Buffer
  330. > result_type;
  331. return new (buffer.allocate(sizeof(result_type)))
  332. result_type(m_it);
  333. }
  334. virtual void increment()
  335. {
  336. ++m_it;
  337. }
  338. virtual void decrement()
  339. {
  340. --m_it;
  341. }
  342. virtual bool equal(const any_single_pass_iterator_interface<Reference, Buffer>& other) const
  343. {
  344. return m_it == range_detail::polymorphic_ref_downcast<const any_bidirectional_iterator_wrapper>(other).m_it;
  345. }
  346. virtual reference dereference() const
  347. {
  348. return dereference_cast<reference>(*m_it);
  349. }
  350. private:
  351. WrappedIterator m_it;
  352. };
  353. template<
  354. class WrappedIterator
  355. , class Reference
  356. , class Difference
  357. , class Buffer
  358. >
  359. class any_random_access_iterator_wrapper
  360. : public any_random_access_iterator_interface<
  361. Reference
  362. , Difference
  363. , Buffer
  364. >
  365. {
  366. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessIteratorConcept<WrappedIterator> ));
  367. typedef any_random_access_iterator_interface<
  368. Reference,
  369. Difference,
  370. Buffer
  371. > base_type;
  372. public:
  373. typedef typename base_type::reference reference;
  374. typedef Difference difference_type;
  375. any_random_access_iterator_wrapper()
  376. : m_it()
  377. {
  378. }
  379. explicit any_random_access_iterator_wrapper(const WrappedIterator& other)
  380. : m_it(other)
  381. {
  382. }
  383. virtual any_random_access_iterator_wrapper* clone(
  384. typename any_random_access_iterator_wrapper::buffer_type& buffer
  385. ) const
  386. {
  387. return new (buffer.allocate(sizeof(*this)))
  388. any_random_access_iterator_wrapper(*this);
  389. }
  390. virtual any_random_access_iterator_wrapper<
  391. WrappedIterator
  392. , typename any_random_access_iterator_wrapper::const_reference
  393. , Difference
  394. , Buffer
  395. >* clone_const_ref(
  396. typename any_random_access_iterator_wrapper::buffer_type& buffer
  397. ) const
  398. {
  399. typedef any_random_access_iterator_wrapper<
  400. WrappedIterator
  401. , typename any_random_access_iterator_wrapper::const_reference
  402. , Difference
  403. , Buffer
  404. > result_type;
  405. return new (buffer.allocate(sizeof(result_type)))
  406. result_type(m_it);
  407. }
  408. virtual any_random_access_iterator_wrapper<
  409. WrappedIterator
  410. , typename any_random_access_iterator_wrapper::reference_as_value_type
  411. , Difference
  412. , Buffer
  413. >* clone_reference_as_value(
  414. typename any_random_access_iterator_wrapper::buffer_type& buffer
  415. ) const
  416. {
  417. typedef any_random_access_iterator_wrapper<
  418. WrappedIterator
  419. , typename any_random_access_iterator_wrapper::reference_as_value_type
  420. , Difference
  421. , Buffer
  422. > result_type;
  423. return new (buffer.allocate(sizeof(result_type)))
  424. result_type(m_it);
  425. }
  426. virtual void increment()
  427. {
  428. ++m_it;
  429. }
  430. virtual bool equal(const any_single_pass_iterator_interface<Reference, Buffer>& other) const
  431. {
  432. return m_it == range_detail::polymorphic_ref_downcast<const any_random_access_iterator_wrapper>(other).m_it;
  433. }
  434. virtual void decrement()
  435. {
  436. --m_it;
  437. }
  438. virtual void advance(Difference offset)
  439. {
  440. m_it += offset;
  441. }
  442. virtual reference dereference() const
  443. {
  444. return dereference_cast<reference>(*m_it);
  445. }
  446. virtual Difference distance_to(const any_random_access_iterator_interface<Reference, Difference, Buffer>& other) const
  447. {
  448. return range_detail::polymorphic_ref_downcast<const any_random_access_iterator_wrapper>(other).m_it - m_it;
  449. }
  450. private:
  451. WrappedIterator m_it;
  452. };
  453. template<
  454. class WrappedIterator
  455. , class Traversal
  456. , class Reference
  457. , class Difference
  458. , class Buffer
  459. >
  460. struct any_iterator_wrapper_type_generator;
  461. template<
  462. class WrappedIterator
  463. , class Reference
  464. , class Difference
  465. , class Buffer
  466. >
  467. struct any_iterator_wrapper_type_generator<
  468. WrappedIterator
  469. , incrementable_traversal_tag
  470. , Reference
  471. , Difference
  472. , Buffer
  473. >
  474. {
  475. typedef any_incrementable_iterator_wrapper<
  476. WrappedIterator
  477. , Reference
  478. , Buffer
  479. > type;
  480. };
  481. template<
  482. class WrappedIterator
  483. , class Reference
  484. , class Difference
  485. , class Buffer
  486. >
  487. struct any_iterator_wrapper_type_generator<
  488. WrappedIterator
  489. , single_pass_traversal_tag
  490. , Reference
  491. , Difference
  492. , Buffer
  493. >
  494. {
  495. typedef any_single_pass_iterator_wrapper<
  496. WrappedIterator
  497. , Reference
  498. , Buffer
  499. > type;
  500. };
  501. template<
  502. class WrappedIterator
  503. , class Reference
  504. , class Difference
  505. , class Buffer
  506. >
  507. struct any_iterator_wrapper_type_generator<
  508. WrappedIterator
  509. , forward_traversal_tag
  510. , Reference
  511. , Difference
  512. , Buffer
  513. >
  514. {
  515. typedef any_forward_iterator_wrapper<
  516. WrappedIterator
  517. , Reference
  518. , Buffer
  519. > type;
  520. };
  521. template<
  522. class WrappedIterator
  523. , class Reference
  524. , class Difference
  525. , class Buffer
  526. >
  527. struct any_iterator_wrapper_type_generator<
  528. WrappedIterator
  529. , bidirectional_traversal_tag
  530. , Reference
  531. , Difference
  532. , Buffer
  533. >
  534. {
  535. typedef any_bidirectional_iterator_wrapper<
  536. WrappedIterator
  537. , Reference
  538. , Buffer
  539. > type;
  540. };
  541. template<
  542. class WrappedIterator
  543. , class Reference
  544. , class Difference
  545. , class Buffer
  546. >
  547. struct any_iterator_wrapper_type_generator<
  548. WrappedIterator
  549. , random_access_traversal_tag
  550. , Reference
  551. , Difference
  552. , Buffer
  553. >
  554. {
  555. typedef any_random_access_iterator_wrapper<
  556. WrappedIterator
  557. , Reference
  558. , Difference
  559. , Buffer
  560. > type;
  561. };
  562. } // namespace range_detail
  563. } // namespace boost
  564. #endif // include guard