any_iterator.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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_HPP_INCLUDED
  11. #define BOOST_RANGE_DETAIL_ANY_ITERATOR_HPP_INCLUDED
  12. #include <boost/mpl/and.hpp>
  13. #include <boost/mpl/or.hpp>
  14. #include <boost/mpl/not.hpp>
  15. #include <boost/mpl/bool.hpp>
  16. #include <boost/iterator/iterator_facade.hpp>
  17. #include <boost/type_traits/is_const.hpp>
  18. #include <boost/type_traits/is_reference.hpp>
  19. #include <boost/type_traits/remove_reference.hpp>
  20. #include <boost/range/detail/any_iterator_buffer.hpp>
  21. #include <boost/range/detail/any_iterator_interface.hpp>
  22. #include <boost/range/detail/any_iterator_wrapper.hpp>
  23. #include <boost/core/enable_if.hpp>
  24. namespace boost
  25. {
  26. namespace range_detail
  27. {
  28. // metafunction to determine if T is a const reference
  29. template<class T>
  30. struct is_const_reference
  31. {
  32. typedef typename mpl::and_<
  33. typename is_reference<T>::type,
  34. typename is_const<
  35. typename remove_reference<T>::type
  36. >::type
  37. >::type type;
  38. };
  39. // metafunction to determine if T is a mutable reference
  40. template<class T>
  41. struct is_mutable_reference
  42. {
  43. typedef typename mpl::and_<
  44. typename is_reference<T>::type,
  45. typename mpl::not_<
  46. typename is_const<
  47. typename remove_reference<T>::type
  48. >::type
  49. >::type
  50. >::type type;
  51. };
  52. // metafunction to evaluate if a source 'reference' can be
  53. // converted to a target 'reference' as a value.
  54. //
  55. // This is true, when the target reference type is actually
  56. // not a reference, and the source reference is convertible
  57. // to the target type.
  58. template<class SourceReference, class TargetReference>
  59. struct is_convertible_to_value_as_reference
  60. {
  61. typedef typename mpl::and_<
  62. typename mpl::not_<
  63. typename is_reference<TargetReference>::type
  64. >::type
  65. , typename is_convertible<
  66. SourceReference
  67. , TargetReference
  68. >::type
  69. >::type type;
  70. };
  71. template<
  72. class Value
  73. , class Traversal
  74. , class Reference
  75. , class Difference
  76. , class Buffer = any_iterator_default_buffer
  77. >
  78. class any_iterator;
  79. // metafunction to determine if SomeIterator is an
  80. // any_iterator.
  81. //
  82. // This is the general implementation which evaluates to false.
  83. template<class SomeIterator>
  84. struct is_any_iterator
  85. : mpl::bool_<false>
  86. {
  87. };
  88. // specialization of is_any_iterator to return true for
  89. // any_iterator classes regardless of template parameters.
  90. template<
  91. class Value
  92. , class Traversal
  93. , class Reference
  94. , class Difference
  95. , class Buffer
  96. >
  97. struct is_any_iterator<
  98. any_iterator<
  99. Value
  100. , Traversal
  101. , Reference
  102. , Difference
  103. , Buffer
  104. >
  105. >
  106. : mpl::bool_<true>
  107. {
  108. };
  109. } // namespace range_detail
  110. namespace iterators
  111. {
  112. namespace detail
  113. {
  114. // Rationale:
  115. // These are specialized since the iterator_facade versions lack
  116. // the requisite typedefs to allow wrapping to determine the types
  117. // if a user copy constructs from a postfix increment.
  118. template<
  119. class Value
  120. , class Traversal
  121. , class Reference
  122. , class Difference
  123. , class Buffer
  124. >
  125. class postfix_increment_proxy<
  126. range_detail::any_iterator<
  127. Value
  128. , Traversal
  129. , Reference
  130. , Difference
  131. , Buffer
  132. >
  133. >
  134. {
  135. typedef range_detail::any_iterator<
  136. Value
  137. , Traversal
  138. , Reference
  139. , Difference
  140. , Buffer
  141. > any_iterator_type;
  142. public:
  143. typedef Value value_type;
  144. typedef typename std::iterator_traits<any_iterator_type>::iterator_category iterator_category;
  145. typedef Difference difference_type;
  146. typedef typename iterator_pointer<any_iterator_type>::type pointer;
  147. typedef Reference reference;
  148. explicit postfix_increment_proxy(any_iterator_type const& x)
  149. : stored_value(*x)
  150. {}
  151. value_type&
  152. operator*() const
  153. {
  154. return this->stored_value;
  155. }
  156. private:
  157. mutable value_type stored_value;
  158. };
  159. template<
  160. class Value
  161. , class Traversal
  162. , class Reference
  163. , class Difference
  164. , class Buffer
  165. >
  166. class writable_postfix_increment_proxy<
  167. range_detail::any_iterator<
  168. Value
  169. , Traversal
  170. , Reference
  171. , Difference
  172. , Buffer
  173. >
  174. >
  175. {
  176. typedef range_detail::any_iterator<
  177. Value
  178. , Traversal
  179. , Reference
  180. , Difference
  181. , Buffer
  182. > any_iterator_type;
  183. public:
  184. typedef Value value_type;
  185. typedef typename std::iterator_traits<any_iterator_type>::iterator_category iterator_category;
  186. typedef Difference difference_type;
  187. typedef typename iterator_pointer<any_iterator_type>::type pointer;
  188. typedef Reference reference;
  189. explicit writable_postfix_increment_proxy(any_iterator_type const& x)
  190. : stored_value(*x)
  191. , stored_iterator(x)
  192. {}
  193. // Dereferencing must return a proxy so that both *r++ = o and
  194. // value_type(*r++) can work. In this case, *r is the same as
  195. // *r++, and the conversion operator below is used to ensure
  196. // readability.
  197. writable_postfix_increment_proxy const&
  198. operator*() const
  199. {
  200. return *this;
  201. }
  202. // Provides readability of *r++
  203. operator value_type&() const
  204. {
  205. return stored_value;
  206. }
  207. // Provides writability of *r++
  208. template <class T>
  209. T const& operator=(T const& x) const
  210. {
  211. *this->stored_iterator = x;
  212. return x;
  213. }
  214. // This overload just in case only non-const objects are writable
  215. template <class T>
  216. T& operator=(T& x) const
  217. {
  218. *this->stored_iterator = x;
  219. return x;
  220. }
  221. // Provides X(r++)
  222. operator any_iterator_type const&() const
  223. {
  224. return stored_iterator;
  225. }
  226. private:
  227. mutable value_type stored_value;
  228. any_iterator_type stored_iterator;
  229. };
  230. } //namespace detail
  231. } //namespace iterators
  232. namespace range_detail
  233. {
  234. template<
  235. class Value
  236. , class Traversal
  237. , class Reference
  238. , class Difference
  239. , class Buffer
  240. >
  241. class any_iterator
  242. : public iterator_facade<
  243. any_iterator<
  244. Value
  245. , Traversal
  246. , Reference
  247. , Difference
  248. , Buffer
  249. >
  250. , Value
  251. , Traversal
  252. , Reference
  253. , Difference
  254. >
  255. {
  256. template<
  257. class OtherValue
  258. , class OtherTraversal
  259. , class OtherReference
  260. , class OtherDifference
  261. , class OtherBuffer
  262. >
  263. friend class any_iterator;
  264. struct enabler {};
  265. struct disabler {};
  266. typedef typename any_iterator_interface_type_generator<
  267. Traversal
  268. , Reference
  269. , Difference
  270. , Buffer
  271. >::type abstract_base_type;
  272. typedef iterator_facade<
  273. any_iterator<
  274. Value
  275. , Traversal
  276. , Reference
  277. , Difference
  278. , Buffer
  279. >
  280. , Value
  281. , Traversal
  282. , Reference
  283. , Difference
  284. > base_type;
  285. typedef Buffer buffer_type;
  286. public:
  287. typedef typename base_type::value_type value_type;
  288. typedef typename base_type::reference reference;
  289. typedef typename base_type::difference_type difference_type;
  290. // Default constructor
  291. any_iterator()
  292. : m_impl(0) {}
  293. // Simple copy construction without conversion
  294. any_iterator(const any_iterator& other)
  295. : base_type(other)
  296. , m_impl(other.m_impl
  297. ? other.m_impl->clone(m_buffer)
  298. : 0)
  299. {
  300. }
  301. // Simple assignment operator without conversion
  302. any_iterator& operator=(const any_iterator& other)
  303. {
  304. if (this != &other)
  305. {
  306. if (m_impl)
  307. m_impl->~abstract_base_type();
  308. m_buffer.deallocate();
  309. m_impl = 0;
  310. if (other.m_impl)
  311. m_impl = other.m_impl->clone(m_buffer);
  312. }
  313. return *this;
  314. }
  315. // Implicit conversion from another any_iterator where the
  316. // conversion is from a non-const reference to a const reference
  317. template<
  318. class OtherValue
  319. , class OtherTraversal
  320. , class OtherReference
  321. , class OtherDifference
  322. >
  323. any_iterator(const any_iterator<
  324. OtherValue,
  325. OtherTraversal,
  326. OtherReference,
  327. OtherDifference,
  328. Buffer
  329. >& other,
  330. typename ::boost::enable_if<
  331. typename mpl::and_<
  332. typename is_mutable_reference<OtherReference>::type,
  333. typename is_const_reference<Reference>::type
  334. >::type,
  335. enabler
  336. >::type* = 0
  337. )
  338. : m_impl(other.m_impl
  339. ? other.m_impl->clone_const_ref(m_buffer)
  340. : 0
  341. )
  342. {
  343. }
  344. // Implicit conversion from another any_iterator where the
  345. // reference types of the source and the target are references
  346. // that are either both const, or both non-const.
  347. template<
  348. class OtherValue
  349. , class OtherTraversal
  350. , class OtherReference
  351. , class OtherDifference
  352. >
  353. any_iterator(const any_iterator<
  354. OtherValue
  355. , OtherTraversal
  356. , OtherReference
  357. , OtherDifference
  358. , Buffer
  359. >& other,
  360. typename ::boost::enable_if<
  361. typename mpl::or_<
  362. typename mpl::and_<
  363. typename is_mutable_reference<OtherReference>::type,
  364. typename is_mutable_reference<Reference>::type
  365. >::type,
  366. typename mpl::and_<
  367. typename is_const_reference<OtherReference>::type,
  368. typename is_const_reference<Reference>::type
  369. >::type
  370. >::type,
  371. enabler
  372. >::type* = 0
  373. )
  374. : m_impl(other.m_impl
  375. ? other.m_impl->clone(m_buffer)
  376. : 0
  377. )
  378. {
  379. }
  380. // Implicit conversion to an any_iterator that uses a value for
  381. // the reference type.
  382. template<
  383. class OtherValue
  384. , class OtherTraversal
  385. , class OtherReference
  386. , class OtherDifference
  387. >
  388. any_iterator(const any_iterator<
  389. OtherValue
  390. , OtherTraversal
  391. , OtherReference
  392. , OtherDifference
  393. , Buffer
  394. >& other,
  395. typename ::boost::enable_if<
  396. typename is_convertible_to_value_as_reference<
  397. OtherReference
  398. , Reference
  399. >::type,
  400. enabler
  401. >::type* = 0
  402. )
  403. : m_impl(other.m_impl
  404. ? other.m_impl->clone_reference_as_value(m_buffer)
  405. : 0
  406. )
  407. {
  408. }
  409. any_iterator clone() const
  410. {
  411. any_iterator result;
  412. if (m_impl)
  413. result.m_impl = m_impl->clone(result.m_buffer);
  414. return result;
  415. }
  416. any_iterator<
  417. Value
  418. , Traversal
  419. , typename abstract_base_type::const_reference
  420. , Difference
  421. , Buffer
  422. >
  423. clone_const_ref() const
  424. {
  425. typedef any_iterator<
  426. Value
  427. , Traversal
  428. , typename abstract_base_type::const_reference
  429. , Difference
  430. , Buffer
  431. > result_type;
  432. result_type result;
  433. if (m_impl)
  434. result.m_impl = m_impl->clone_const_ref(result.m_buffer);
  435. return result;
  436. }
  437. // implicit conversion and construction from type-erasure-compatible
  438. // iterators
  439. template<class WrappedIterator>
  440. explicit any_iterator(
  441. const WrappedIterator& wrapped_iterator,
  442. typename disable_if<
  443. typename is_any_iterator<WrappedIterator>::type
  444. , disabler
  445. >::type* = 0
  446. )
  447. {
  448. typedef typename any_iterator_wrapper_type_generator<
  449. WrappedIterator
  450. , Traversal
  451. , Reference
  452. , Difference
  453. , Buffer
  454. >::type wrapper_type;
  455. void* ptr = m_buffer.allocate(sizeof(wrapper_type));
  456. m_impl = new(ptr) wrapper_type(wrapped_iterator);
  457. }
  458. ~any_iterator()
  459. {
  460. // manually run the destructor, the deallocation is automatically
  461. // handled by the any_iterator_small_buffer base class.
  462. if (m_impl)
  463. m_impl->~abstract_base_type();
  464. }
  465. private:
  466. friend class ::boost::iterator_core_access;
  467. Reference dereference() const
  468. {
  469. BOOST_ASSERT( m_impl );
  470. return m_impl->dereference();
  471. }
  472. bool equal(const any_iterator& other) const
  473. {
  474. return (m_impl == other.m_impl)
  475. || (m_impl && other.m_impl && m_impl->equal(*other.m_impl));
  476. }
  477. void increment()
  478. {
  479. BOOST_ASSERT( m_impl );
  480. m_impl->increment();
  481. }
  482. void decrement()
  483. {
  484. BOOST_ASSERT( m_impl );
  485. m_impl->decrement();
  486. }
  487. Difference distance_to(const any_iterator& other) const
  488. {
  489. return m_impl && other.m_impl
  490. ? m_impl->distance_to(*other.m_impl)
  491. : 0;
  492. }
  493. void advance(Difference offset)
  494. {
  495. BOOST_ASSERT( m_impl );
  496. m_impl->advance(offset);
  497. }
  498. any_iterator& swap(any_iterator& other)
  499. {
  500. BOOST_ASSERT( this != &other );
  501. // grab a temporary copy of the other iterator
  502. any_iterator tmp(other);
  503. // deallocate the other iterator, taking care to obey the
  504. // class-invariants in-case of exceptions later
  505. if (other.m_impl)
  506. {
  507. other.m_impl->~abstract_base_type();
  508. other.m_buffer.deallocate();
  509. other.m_impl = 0;
  510. }
  511. // If this is a non-null iterator then we need to put
  512. // a clone of this iterators implementation into the other
  513. // iterator.
  514. // We can't just swap because of the small buffer optimization.
  515. if (m_impl)
  516. {
  517. other.m_impl = m_impl->clone(other.m_buffer);
  518. m_impl->~abstract_base_type();
  519. m_buffer.deallocate();
  520. m_impl = 0;
  521. }
  522. // assign to this instance a clone of the temporarily held
  523. // tmp which represents the input other parameter at the
  524. // start of execution of this function.
  525. if (tmp.m_impl)
  526. m_impl = tmp.m_impl->clone(m_buffer);
  527. return *this;
  528. }
  529. buffer_type m_buffer;
  530. abstract_base_type* m_impl;
  531. };
  532. } // namespace range_detail
  533. } // namespace boost
  534. #endif // include guard