safe_iterator.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. // Safe iterator implementation -*- C++ -*-
  2. // Copyright (C) 2003-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file debug/safe_iterator.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_H
  24. #define _GLIBCXX_DEBUG_SAFE_ITERATOR_H 1
  25. #include <debug/assertions.h>
  26. #include <debug/macros.h>
  27. #include <debug/functions.h>
  28. #include <debug/safe_base.h>
  29. #include <bits/stl_pair.h>
  30. #include <ext/type_traits.h>
  31. namespace __gnu_debug
  32. {
  33. /** Helper struct to deal with sequence offering a before_begin
  34. * iterator.
  35. **/
  36. template<typename _Sequence>
  37. struct _BeforeBeginHelper
  38. {
  39. template<typename _Iterator>
  40. static bool
  41. _S_Is(const _Safe_iterator<_Iterator, _Sequence>&)
  42. { return false; }
  43. template<typename _Iterator>
  44. static bool
  45. _S_Is_Beginnest(const _Safe_iterator<_Iterator, _Sequence>& __it)
  46. { return __it.base() == __it._M_get_sequence()->_M_base().begin(); }
  47. };
  48. /** Sequence traits giving the size of a container if possible. */
  49. template<typename _Sequence>
  50. struct _Sequence_traits
  51. {
  52. typedef _Distance_traits<typename _Sequence::iterator> _DistTraits;
  53. static typename _DistTraits::__type
  54. _S_size(const _Sequence& __seq)
  55. { return std::make_pair(__seq.size(), __dp_exact); }
  56. };
  57. /** \brief Safe iterator wrapper.
  58. *
  59. * The class template %_Safe_iterator is a wrapper around an
  60. * iterator that tracks the iterator's movement among sequences and
  61. * checks that operations performed on the "safe" iterator are
  62. * legal. In additional to the basic iterator operations (which are
  63. * validated, and then passed to the underlying iterator),
  64. * %_Safe_iterator has member functions for iterator invalidation,
  65. * attaching/detaching the iterator from sequences, and querying
  66. * the iterator's state.
  67. *
  68. * Note that _Iterator must be the first base class so that it gets
  69. * initialized before the iterator is being attached to the container's list
  70. * of iterators and it is being detached before _Iterator get
  71. * destroyed. Otherwise it would result in a data race.
  72. */
  73. template<typename _Iterator, typename _Sequence>
  74. class _Safe_iterator
  75. : private _Iterator,
  76. public _Safe_iterator_base
  77. {
  78. typedef _Iterator _Iter_base;
  79. typedef _Safe_iterator_base _Safe_base;
  80. typedef typename _Sequence::const_iterator _Const_iterator;
  81. /// Determine if this is a constant iterator.
  82. bool
  83. _M_constant() const
  84. { return std::__are_same<_Const_iterator, _Safe_iterator>::__value; }
  85. typedef std::iterator_traits<_Iterator> _Traits;
  86. struct _Attach_single
  87. { };
  88. _Safe_iterator(const _Iterator& __i, _Safe_sequence_base* __seq,
  89. _Attach_single)
  90. _GLIBCXX_NOEXCEPT
  91. : _Iter_base(__i)
  92. { _M_attach_single(__seq); }
  93. public:
  94. typedef _Iterator iterator_type;
  95. typedef typename _Traits::iterator_category iterator_category;
  96. typedef typename _Traits::value_type value_type;
  97. typedef typename _Traits::difference_type difference_type;
  98. typedef typename _Traits::reference reference;
  99. typedef typename _Traits::pointer pointer;
  100. /// @post the iterator is singular and unattached
  101. _Safe_iterator() _GLIBCXX_NOEXCEPT : _Iter_base() { }
  102. /**
  103. * @brief Safe iterator construction from an unsafe iterator and
  104. * its sequence.
  105. *
  106. * @pre @p seq is not NULL
  107. * @post this is not singular
  108. */
  109. _Safe_iterator(const _Iterator& __i, const _Safe_sequence_base* __seq)
  110. _GLIBCXX_NOEXCEPT
  111. : _Iter_base(__i), _Safe_base(__seq, _M_constant())
  112. {
  113. _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
  114. _M_message(__msg_init_singular)
  115. ._M_iterator(*this, "this"));
  116. }
  117. /**
  118. * @brief Copy construction.
  119. */
  120. _Safe_iterator(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
  121. : _Iter_base(__x.base())
  122. {
  123. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  124. // DR 408. Is vector<reverse_iterator<char*> > forbidden?
  125. _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
  126. || __x.base() == _Iterator(),
  127. _M_message(__msg_init_copy_singular)
  128. ._M_iterator(*this, "this")
  129. ._M_iterator(__x, "other"));
  130. _M_attach(__x._M_sequence);
  131. }
  132. #if __cplusplus >= 201103L
  133. /**
  134. * @brief Move construction.
  135. * @post __x is singular and unattached
  136. */
  137. _Safe_iterator(_Safe_iterator&& __x) noexcept
  138. : _Iter_base()
  139. {
  140. _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
  141. || __x.base() == _Iterator(),
  142. _M_message(__msg_init_copy_singular)
  143. ._M_iterator(*this, "this")
  144. ._M_iterator(__x, "other"));
  145. _Safe_sequence_base* __seq = __x._M_sequence;
  146. __x._M_detach();
  147. std::swap(base(), __x.base());
  148. _M_attach(__seq);
  149. }
  150. #endif
  151. /**
  152. * @brief Converting constructor from a mutable iterator to a
  153. * constant iterator.
  154. */
  155. template<typename _MutableIterator>
  156. _Safe_iterator(
  157. const _Safe_iterator<_MutableIterator,
  158. typename __gnu_cxx::__enable_if<(std::__are_same<_MutableIterator,
  159. typename _Sequence::iterator::iterator_type>::__value),
  160. _Sequence>::__type>& __x) _GLIBCXX_NOEXCEPT
  161. : _Iter_base(__x.base())
  162. {
  163. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  164. // DR 408. Is vector<reverse_iterator<char*> > forbidden?
  165. _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
  166. || __x.base() == _Iterator(),
  167. _M_message(__msg_init_const_singular)
  168. ._M_iterator(*this, "this")
  169. ._M_iterator(__x, "other"));
  170. _M_attach(__x._M_sequence);
  171. }
  172. /**
  173. * @brief Copy assignment.
  174. */
  175. _Safe_iterator&
  176. operator=(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
  177. {
  178. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  179. // DR 408. Is vector<reverse_iterator<char*> > forbidden?
  180. _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
  181. || __x.base() == _Iterator(),
  182. _M_message(__msg_copy_singular)
  183. ._M_iterator(*this, "this")
  184. ._M_iterator(__x, "other"));
  185. if (this->_M_sequence && this->_M_sequence == __x._M_sequence)
  186. {
  187. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  188. base() = __x.base();
  189. _M_version = __x._M_sequence->_M_version;
  190. }
  191. else
  192. {
  193. _M_detach();
  194. base() = __x.base();
  195. _M_attach(__x._M_sequence);
  196. }
  197. return *this;
  198. }
  199. #if __cplusplus >= 201103L
  200. /**
  201. * @brief Move assignment.
  202. * @post __x is singular and unattached
  203. */
  204. _Safe_iterator&
  205. operator=(_Safe_iterator&& __x) noexcept
  206. {
  207. _GLIBCXX_DEBUG_VERIFY(this != &__x,
  208. _M_message(__msg_self_move_assign)
  209. ._M_iterator(*this, "this"));
  210. _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
  211. || __x.base() == _Iterator(),
  212. _M_message(__msg_copy_singular)
  213. ._M_iterator(*this, "this")
  214. ._M_iterator(__x, "other"));
  215. if (this->_M_sequence && this->_M_sequence == __x._M_sequence)
  216. {
  217. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  218. base() = __x.base();
  219. _M_version = __x._M_sequence->_M_version;
  220. }
  221. else
  222. {
  223. _M_detach();
  224. base() = __x.base();
  225. _M_attach(__x._M_sequence);
  226. }
  227. __x._M_detach();
  228. __x.base() = _Iterator();
  229. return *this;
  230. }
  231. #endif
  232. /**
  233. * @brief Iterator dereference.
  234. * @pre iterator is dereferenceable
  235. */
  236. reference
  237. operator*() const _GLIBCXX_NOEXCEPT
  238. {
  239. _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
  240. _M_message(__msg_bad_deref)
  241. ._M_iterator(*this, "this"));
  242. return *base();
  243. }
  244. /**
  245. * @brief Iterator dereference.
  246. * @pre iterator is dereferenceable
  247. */
  248. pointer
  249. operator->() const _GLIBCXX_NOEXCEPT
  250. {
  251. _GLIBCXX_DEBUG_VERIFY(this->_M_dereferenceable(),
  252. _M_message(__msg_bad_deref)
  253. ._M_iterator(*this, "this"));
  254. return base().operator->();
  255. }
  256. // ------ Input iterator requirements ------
  257. /**
  258. * @brief Iterator preincrement
  259. * @pre iterator is incrementable
  260. */
  261. _Safe_iterator&
  262. operator++() _GLIBCXX_NOEXCEPT
  263. {
  264. _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
  265. _M_message(__msg_bad_inc)
  266. ._M_iterator(*this, "this"));
  267. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  268. ++base();
  269. return *this;
  270. }
  271. /**
  272. * @brief Iterator postincrement
  273. * @pre iterator is incrementable
  274. */
  275. _Safe_iterator
  276. operator++(int) _GLIBCXX_NOEXCEPT
  277. {
  278. _GLIBCXX_DEBUG_VERIFY(this->_M_incrementable(),
  279. _M_message(__msg_bad_inc)
  280. ._M_iterator(*this, "this"));
  281. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  282. return _Safe_iterator(base()++, this->_M_sequence, _Attach_single());
  283. }
  284. // ------ Bidirectional iterator requirements ------
  285. /**
  286. * @brief Iterator predecrement
  287. * @pre iterator is decrementable
  288. */
  289. _Safe_iterator&
  290. operator--() _GLIBCXX_NOEXCEPT
  291. {
  292. _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
  293. _M_message(__msg_bad_dec)
  294. ._M_iterator(*this, "this"));
  295. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  296. --base();
  297. return *this;
  298. }
  299. /**
  300. * @brief Iterator postdecrement
  301. * @pre iterator is decrementable
  302. */
  303. _Safe_iterator
  304. operator--(int) _GLIBCXX_NOEXCEPT
  305. {
  306. _GLIBCXX_DEBUG_VERIFY(this->_M_decrementable(),
  307. _M_message(__msg_bad_dec)
  308. ._M_iterator(*this, "this"));
  309. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  310. return _Safe_iterator(base()--, this->_M_sequence, _Attach_single());
  311. }
  312. // ------ Random access iterator requirements ------
  313. reference
  314. operator[](const difference_type& __n) const _GLIBCXX_NOEXCEPT
  315. {
  316. _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n)
  317. && this->_M_can_advance(__n+1),
  318. _M_message(__msg_iter_subscript_oob)
  319. ._M_iterator(*this)._M_integer(__n));
  320. return base()[__n];
  321. }
  322. _Safe_iterator&
  323. operator+=(const difference_type& __n) _GLIBCXX_NOEXCEPT
  324. {
  325. _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
  326. _M_message(__msg_advance_oob)
  327. ._M_iterator(*this)._M_integer(__n));
  328. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  329. base() += __n;
  330. return *this;
  331. }
  332. _Safe_iterator
  333. operator+(const difference_type& __n) const _GLIBCXX_NOEXCEPT
  334. {
  335. _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(__n),
  336. _M_message(__msg_advance_oob)
  337. ._M_iterator(*this)._M_integer(__n));
  338. return _Safe_iterator(base() + __n, this->_M_sequence);
  339. }
  340. _Safe_iterator&
  341. operator-=(const difference_type& __n) _GLIBCXX_NOEXCEPT
  342. {
  343. _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
  344. _M_message(__msg_retreat_oob)
  345. ._M_iterator(*this)._M_integer(__n));
  346. __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
  347. base() -= __n;
  348. return *this;
  349. }
  350. _Safe_iterator
  351. operator-(const difference_type& __n) const _GLIBCXX_NOEXCEPT
  352. {
  353. _GLIBCXX_DEBUG_VERIFY(this->_M_can_advance(-__n),
  354. _M_message(__msg_retreat_oob)
  355. ._M_iterator(*this)._M_integer(__n));
  356. return _Safe_iterator(base() - __n, this->_M_sequence);
  357. }
  358. // ------ Utilities ------
  359. /**
  360. * @brief Return the underlying iterator
  361. */
  362. _Iterator&
  363. base() _GLIBCXX_NOEXCEPT { return *this; }
  364. const _Iterator&
  365. base() const _GLIBCXX_NOEXCEPT { return *this; }
  366. /**
  367. * @brief Conversion to underlying non-debug iterator to allow
  368. * better interaction with non-debug containers.
  369. */
  370. operator _Iterator() const _GLIBCXX_NOEXCEPT { return *this; }
  371. /** Attach iterator to the given sequence. */
  372. void
  373. _M_attach(_Safe_sequence_base* __seq)
  374. { _Safe_base::_M_attach(__seq, _M_constant()); }
  375. /** Likewise, but not thread-safe. */
  376. void
  377. _M_attach_single(_Safe_sequence_base* __seq)
  378. { _Safe_base::_M_attach_single(__seq, _M_constant()); }
  379. /// Is the iterator dereferenceable?
  380. bool
  381. _M_dereferenceable() const
  382. { return !this->_M_singular() && !_M_is_end() && !_M_is_before_begin(); }
  383. /// Is the iterator before a dereferenceable one?
  384. bool
  385. _M_before_dereferenceable() const
  386. {
  387. if (this->_M_incrementable())
  388. {
  389. _Iterator __base = base();
  390. return ++__base != _M_get_sequence()->_M_base().end();
  391. }
  392. return false;
  393. }
  394. /// Is the iterator incrementable?
  395. bool
  396. _M_incrementable() const
  397. { return !this->_M_singular() && !_M_is_end(); }
  398. // Is the iterator decrementable?
  399. bool
  400. _M_decrementable() const { return !_M_singular() && !_M_is_begin(); }
  401. // Can we advance the iterator @p __n steps (@p __n may be negative)
  402. bool
  403. _M_can_advance(const difference_type& __n) const;
  404. // Is the iterator range [*this, __rhs) valid?
  405. bool
  406. _M_valid_range(const _Safe_iterator& __rhs,
  407. std::pair<difference_type, _Distance_precision>& __dist,
  408. bool __check_dereferenceable = true) const;
  409. // The sequence this iterator references.
  410. typename
  411. __gnu_cxx::__conditional_type<std::__are_same<_Const_iterator,
  412. _Safe_iterator>::__value,
  413. const _Sequence*,
  414. _Sequence*>::__type
  415. _M_get_sequence() const
  416. { return static_cast<_Sequence*>(_M_sequence); }
  417. /// Is this iterator equal to the sequence's begin() iterator?
  418. bool
  419. _M_is_begin() const
  420. { return base() == _M_get_sequence()->_M_base().begin(); }
  421. /// Is this iterator equal to the sequence's end() iterator?
  422. bool
  423. _M_is_end() const
  424. { return base() == _M_get_sequence()->_M_base().end(); }
  425. /// Is this iterator equal to the sequence's before_begin() iterator if
  426. /// any?
  427. bool
  428. _M_is_before_begin() const
  429. { return _BeforeBeginHelper<_Sequence>::_S_Is(*this); }
  430. /// Is this iterator equal to the sequence's before_begin() iterator if
  431. /// any or begin() otherwise?
  432. bool
  433. _M_is_beginnest() const
  434. { return _BeforeBeginHelper<_Sequence>::_S_Is_Beginnest(*this); }
  435. };
  436. template<typename _IteratorL, typename _IteratorR, typename _Sequence>
  437. inline bool
  438. operator==(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
  439. const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
  440. _GLIBCXX_NOEXCEPT
  441. {
  442. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  443. _M_message(__msg_iter_compare_bad)
  444. ._M_iterator(__lhs, "lhs")
  445. ._M_iterator(__rhs, "rhs"));
  446. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  447. _M_message(__msg_compare_different)
  448. ._M_iterator(__lhs, "lhs")
  449. ._M_iterator(__rhs, "rhs"));
  450. return __lhs.base() == __rhs.base();
  451. }
  452. template<typename _Iterator, typename _Sequence>
  453. inline bool
  454. operator==(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
  455. const _Safe_iterator<_Iterator, _Sequence>& __rhs)
  456. _GLIBCXX_NOEXCEPT
  457. {
  458. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  459. _M_message(__msg_iter_compare_bad)
  460. ._M_iterator(__lhs, "lhs")
  461. ._M_iterator(__rhs, "rhs"));
  462. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  463. _M_message(__msg_compare_different)
  464. ._M_iterator(__lhs, "lhs")
  465. ._M_iterator(__rhs, "rhs"));
  466. return __lhs.base() == __rhs.base();
  467. }
  468. template<typename _IteratorL, typename _IteratorR, typename _Sequence>
  469. inline bool
  470. operator!=(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
  471. const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
  472. _GLIBCXX_NOEXCEPT
  473. {
  474. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  475. _M_message(__msg_iter_compare_bad)
  476. ._M_iterator(__lhs, "lhs")
  477. ._M_iterator(__rhs, "rhs"));
  478. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  479. _M_message(__msg_compare_different)
  480. ._M_iterator(__lhs, "lhs")
  481. ._M_iterator(__rhs, "rhs"));
  482. return __lhs.base() != __rhs.base();
  483. }
  484. template<typename _Iterator, typename _Sequence>
  485. inline bool
  486. operator!=(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
  487. const _Safe_iterator<_Iterator, _Sequence>& __rhs)
  488. _GLIBCXX_NOEXCEPT
  489. {
  490. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  491. _M_message(__msg_iter_compare_bad)
  492. ._M_iterator(__lhs, "lhs")
  493. ._M_iterator(__rhs, "rhs"));
  494. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  495. _M_message(__msg_compare_different)
  496. ._M_iterator(__lhs, "lhs")
  497. ._M_iterator(__rhs, "rhs"));
  498. return __lhs.base() != __rhs.base();
  499. }
  500. template<typename _IteratorL, typename _IteratorR, typename _Sequence>
  501. inline bool
  502. operator<(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
  503. const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
  504. _GLIBCXX_NOEXCEPT
  505. {
  506. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  507. _M_message(__msg_iter_order_bad)
  508. ._M_iterator(__lhs, "lhs")
  509. ._M_iterator(__rhs, "rhs"));
  510. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  511. _M_message(__msg_order_different)
  512. ._M_iterator(__lhs, "lhs")
  513. ._M_iterator(__rhs, "rhs"));
  514. return __lhs.base() < __rhs.base();
  515. }
  516. template<typename _Iterator, typename _Sequence>
  517. inline bool
  518. operator<(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
  519. const _Safe_iterator<_Iterator, _Sequence>& __rhs)
  520. _GLIBCXX_NOEXCEPT
  521. {
  522. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  523. _M_message(__msg_iter_order_bad)
  524. ._M_iterator(__lhs, "lhs")
  525. ._M_iterator(__rhs, "rhs"));
  526. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  527. _M_message(__msg_order_different)
  528. ._M_iterator(__lhs, "lhs")
  529. ._M_iterator(__rhs, "rhs"));
  530. return __lhs.base() < __rhs.base();
  531. }
  532. template<typename _IteratorL, typename _IteratorR, typename _Sequence>
  533. inline bool
  534. operator<=(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
  535. const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
  536. _GLIBCXX_NOEXCEPT
  537. {
  538. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  539. _M_message(__msg_iter_order_bad)
  540. ._M_iterator(__lhs, "lhs")
  541. ._M_iterator(__rhs, "rhs"));
  542. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  543. _M_message(__msg_order_different)
  544. ._M_iterator(__lhs, "lhs")
  545. ._M_iterator(__rhs, "rhs"));
  546. return __lhs.base() <= __rhs.base();
  547. }
  548. template<typename _Iterator, typename _Sequence>
  549. inline bool
  550. operator<=(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
  551. const _Safe_iterator<_Iterator, _Sequence>& __rhs)
  552. _GLIBCXX_NOEXCEPT
  553. {
  554. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  555. _M_message(__msg_iter_order_bad)
  556. ._M_iterator(__lhs, "lhs")
  557. ._M_iterator(__rhs, "rhs"));
  558. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  559. _M_message(__msg_order_different)
  560. ._M_iterator(__lhs, "lhs")
  561. ._M_iterator(__rhs, "rhs"));
  562. return __lhs.base() <= __rhs.base();
  563. }
  564. template<typename _IteratorL, typename _IteratorR, typename _Sequence>
  565. inline bool
  566. operator>(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
  567. const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
  568. _GLIBCXX_NOEXCEPT
  569. {
  570. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  571. _M_message(__msg_iter_order_bad)
  572. ._M_iterator(__lhs, "lhs")
  573. ._M_iterator(__rhs, "rhs"));
  574. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  575. _M_message(__msg_order_different)
  576. ._M_iterator(__lhs, "lhs")
  577. ._M_iterator(__rhs, "rhs"));
  578. return __lhs.base() > __rhs.base();
  579. }
  580. template<typename _Iterator, typename _Sequence>
  581. inline bool
  582. operator>(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
  583. const _Safe_iterator<_Iterator, _Sequence>& __rhs)
  584. _GLIBCXX_NOEXCEPT
  585. {
  586. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  587. _M_message(__msg_iter_order_bad)
  588. ._M_iterator(__lhs, "lhs")
  589. ._M_iterator(__rhs, "rhs"));
  590. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  591. _M_message(__msg_order_different)
  592. ._M_iterator(__lhs, "lhs")
  593. ._M_iterator(__rhs, "rhs"));
  594. return __lhs.base() > __rhs.base();
  595. }
  596. template<typename _IteratorL, typename _IteratorR, typename _Sequence>
  597. inline bool
  598. operator>=(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
  599. const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
  600. _GLIBCXX_NOEXCEPT
  601. {
  602. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  603. _M_message(__msg_iter_order_bad)
  604. ._M_iterator(__lhs, "lhs")
  605. ._M_iterator(__rhs, "rhs"));
  606. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  607. _M_message(__msg_order_different)
  608. ._M_iterator(__lhs, "lhs")
  609. ._M_iterator(__rhs, "rhs"));
  610. return __lhs.base() >= __rhs.base();
  611. }
  612. template<typename _Iterator, typename _Sequence>
  613. inline bool
  614. operator>=(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
  615. const _Safe_iterator<_Iterator, _Sequence>& __rhs)
  616. _GLIBCXX_NOEXCEPT
  617. {
  618. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  619. _M_message(__msg_iter_order_bad)
  620. ._M_iterator(__lhs, "lhs")
  621. ._M_iterator(__rhs, "rhs"));
  622. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  623. _M_message(__msg_order_different)
  624. ._M_iterator(__lhs, "lhs")
  625. ._M_iterator(__rhs, "rhs"));
  626. return __lhs.base() >= __rhs.base();
  627. }
  628. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  629. // According to the resolution of DR179 not only the various comparison
  630. // operators but also operator- must accept mixed iterator/const_iterator
  631. // parameters.
  632. template<typename _IteratorL, typename _IteratorR, typename _Sequence>
  633. inline typename _Safe_iterator<_IteratorL, _Sequence>::difference_type
  634. operator-(const _Safe_iterator<_IteratorL, _Sequence>& __lhs,
  635. const _Safe_iterator<_IteratorR, _Sequence>& __rhs)
  636. _GLIBCXX_NOEXCEPT
  637. {
  638. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  639. _M_message(__msg_distance_bad)
  640. ._M_iterator(__lhs, "lhs")
  641. ._M_iterator(__rhs, "rhs"));
  642. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  643. _M_message(__msg_distance_different)
  644. ._M_iterator(__lhs, "lhs")
  645. ._M_iterator(__rhs, "rhs"));
  646. return __lhs.base() - __rhs.base();
  647. }
  648. template<typename _Iterator, typename _Sequence>
  649. inline typename _Safe_iterator<_Iterator, _Sequence>::difference_type
  650. operator-(const _Safe_iterator<_Iterator, _Sequence>& __lhs,
  651. const _Safe_iterator<_Iterator, _Sequence>& __rhs)
  652. _GLIBCXX_NOEXCEPT
  653. {
  654. _GLIBCXX_DEBUG_VERIFY(! __lhs._M_singular() && ! __rhs._M_singular(),
  655. _M_message(__msg_distance_bad)
  656. ._M_iterator(__lhs, "lhs")
  657. ._M_iterator(__rhs, "rhs"));
  658. _GLIBCXX_DEBUG_VERIFY(__lhs._M_can_compare(__rhs),
  659. _M_message(__msg_distance_different)
  660. ._M_iterator(__lhs, "lhs")
  661. ._M_iterator(__rhs, "rhs"));
  662. return __lhs.base() - __rhs.base();
  663. }
  664. template<typename _Iterator, typename _Sequence>
  665. inline _Safe_iterator<_Iterator, _Sequence>
  666. operator+(typename _Safe_iterator<_Iterator,_Sequence>::difference_type __n,
  667. const _Safe_iterator<_Iterator, _Sequence>& __i) _GLIBCXX_NOEXCEPT
  668. { return __i + __n; }
  669. /** Safe iterators know if they are dereferenceable. */
  670. template<typename _Iterator, typename _Sequence>
  671. inline bool
  672. __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
  673. { return __x._M_dereferenceable(); }
  674. /** Safe iterators know how to check if they form a valid range. */
  675. template<typename _Iterator, typename _Sequence>
  676. inline bool
  677. __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
  678. const _Safe_iterator<_Iterator, _Sequence>& __last,
  679. typename _Distance_traits<_Iterator>::__type& __dist)
  680. { return __first._M_valid_range(__last, __dist); }
  681. /** Safe iterators can help to get better distance knowledge. */
  682. template<typename _Iterator, typename _Sequence>
  683. inline typename _Distance_traits<_Iterator>::__type
  684. __get_distance(const _Safe_iterator<_Iterator, _Sequence>& __first,
  685. const _Safe_iterator<_Iterator, _Sequence>& __last,
  686. std::random_access_iterator_tag)
  687. { return std::make_pair(__last.base() - __first.base(), __dp_exact); }
  688. template<typename _Iterator, typename _Sequence>
  689. inline typename _Distance_traits<_Iterator>::__type
  690. __get_distance(const _Safe_iterator<_Iterator, _Sequence>& __first,
  691. const _Safe_iterator<_Iterator, _Sequence>& __last,
  692. std::input_iterator_tag)
  693. {
  694. typedef typename _Distance_traits<_Iterator>::__type _Diff;
  695. typedef _Sequence_traits<_Sequence> _SeqTraits;
  696. if (__first.base() == __last.base())
  697. return std::make_pair(0, __dp_exact);
  698. if (__first._M_is_before_begin())
  699. {
  700. if (__last._M_is_begin())
  701. return std::make_pair(1, __dp_exact);
  702. return std::make_pair(1, __dp_sign);
  703. }
  704. if (__first._M_is_begin())
  705. {
  706. if (__last._M_is_before_begin())
  707. return std::make_pair(-1, __dp_exact);
  708. if (__last._M_is_end())
  709. return _SeqTraits::_S_size(*__first._M_get_sequence());
  710. return std::make_pair(1, __dp_sign);
  711. }
  712. if (__first._M_is_end())
  713. {
  714. if (__last._M_is_before_begin())
  715. return std::make_pair(-1, __dp_exact);
  716. if (__last._M_is_begin())
  717. {
  718. _Diff __diff = _SeqTraits::_S_size(*__first._M_get_sequence());
  719. return std::make_pair(-__diff.first, __diff.second);
  720. }
  721. return std::make_pair(-1, __dp_sign);
  722. }
  723. if (__last._M_is_before_begin() || __last._M_is_begin())
  724. return std::make_pair(-1, __dp_sign);
  725. if (__last._M_is_end())
  726. return std::make_pair(1, __dp_sign);
  727. return std::make_pair(1, __dp_equality);
  728. }
  729. // Get distance from sequence begin to specified iterator.
  730. template<typename _Iterator, typename _Sequence>
  731. inline typename _Distance_traits<_Iterator>::__type
  732. __get_distance_from_begin(const _Safe_iterator<_Iterator, _Sequence>& __it)
  733. {
  734. typedef _Sequence_traits<_Sequence> _SeqTraits;
  735. // No need to consider before_begin as this function is only used in
  736. // _M_can_advance which won't be used for forward_list iterators.
  737. if (__it._M_is_begin())
  738. return std::make_pair(0, __dp_exact);
  739. if (__it._M_is_end())
  740. return _SeqTraits::_S_size(*__it._M_get_sequence());
  741. typename _Distance_traits<_Iterator>::__type __res
  742. = __get_distance(__it._M_get_sequence()->_M_base().begin(), __it.base());
  743. if (__res.second == __dp_equality)
  744. return std::make_pair(1, __dp_sign);
  745. return __res;
  746. }
  747. // Get distance from specified iterator to sequence end.
  748. template<typename _Iterator, typename _Sequence>
  749. inline typename _Distance_traits<_Iterator>::__type
  750. __get_distance_to_end(const _Safe_iterator<_Iterator, _Sequence>& __it)
  751. {
  752. typedef _Sequence_traits<_Sequence> _SeqTraits;
  753. // No need to consider before_begin as this function is only used in
  754. // _M_can_advance which won't be used for forward_list iterators.
  755. if (__it._M_is_begin())
  756. return _SeqTraits::_S_size(*__it._M_get_sequence());
  757. if (__it._M_is_end())
  758. return std::make_pair(0, __dp_exact);
  759. typename _Distance_traits<_Iterator>::__type __res
  760. = __get_distance(__it.base(), __it._M_get_sequence()->_M_base().end());
  761. if (__res.second == __dp_equality)
  762. return std::make_pair(1, __dp_sign);
  763. return __res;
  764. }
  765. #if __cplusplus < 201103L
  766. template<typename _Iterator, typename _Sequence>
  767. struct __is_safe_random_iterator<_Safe_iterator<_Iterator, _Sequence> >
  768. : std::__are_same<std::random_access_iterator_tag,
  769. typename std::iterator_traits<_Iterator>::
  770. iterator_category>
  771. { };
  772. #else
  773. template<typename _Iterator, typename _Sequence>
  774. _Iterator
  775. __base(const _Safe_iterator<_Iterator, _Sequence>& __it,
  776. std::random_access_iterator_tag)
  777. { return __it.base(); }
  778. template<typename _Iterator, typename _Sequence>
  779. const _Safe_iterator<_Iterator, _Sequence>&
  780. __base(const _Safe_iterator<_Iterator, _Sequence>& __it,
  781. std::input_iterator_tag)
  782. { return __it; }
  783. template<typename _Iterator, typename _Sequence>
  784. auto
  785. __base(const _Safe_iterator<_Iterator, _Sequence>& __it)
  786. -> decltype(__base(__it, std::__iterator_category(__it)))
  787. { return __base(__it, std::__iterator_category(__it)); }
  788. #endif
  789. #if __cplusplus < 201103L
  790. template<typename _Iterator, typename _Sequence>
  791. struct _Unsafe_type<_Safe_iterator<_Iterator, _Sequence> >
  792. { typedef _Iterator _Type; };
  793. #endif
  794. template<typename _Iterator, typename _Sequence>
  795. inline _Iterator
  796. __unsafe(const _Safe_iterator<_Iterator, _Sequence>& __it)
  797. { return __it.base(); }
  798. } // namespace __gnu_debug
  799. #include <debug/safe_iterator.tcc>
  800. #endif