safe_iterator.h 29 KB

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