safe_iterator.h 29 KB

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