safe_iterator.tcc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Debugging iterator implementation (out of line) -*- 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.tcc
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC
  24. #define _GLIBCXX_DEBUG_SAFE_ITERATOR_TCC 1
  25. namespace __gnu_debug
  26. {
  27. template<typename _Iterator, typename _Sequence>
  28. bool
  29. _Safe_iterator<_Iterator, _Sequence>::
  30. _M_can_advance(const difference_type& __n) const
  31. {
  32. if (this->_M_singular())
  33. return false;
  34. if (__n == 0)
  35. return true;
  36. if (__n < 0)
  37. {
  38. std::pair<difference_type, _Distance_precision> __dist =
  39. __get_distance_from_begin(*this);
  40. bool __ok = ((__dist.second == __dp_exact && __dist.first >= -__n)
  41. || (__dist.second != __dp_exact && __dist.first > 0));
  42. return __ok;
  43. }
  44. else
  45. {
  46. std::pair<difference_type, _Distance_precision> __dist =
  47. __get_distance_to_end(*this);
  48. bool __ok = ((__dist.second == __dp_exact && __dist.first >= __n)
  49. || (__dist.second != __dp_exact && __dist.first > 0));
  50. return __ok;
  51. }
  52. }
  53. template<typename _Iterator, typename _Sequence>
  54. bool
  55. _Safe_iterator<_Iterator, _Sequence>::
  56. _M_valid_range(const _Safe_iterator& __rhs,
  57. std::pair<difference_type, _Distance_precision>& __dist,
  58. bool __check_dereferenceable) const
  59. {
  60. if (!_M_can_compare(__rhs))
  61. return false;
  62. /* Determine iterators order */
  63. __dist = __get_distance(*this, __rhs);
  64. switch (__dist.second)
  65. {
  66. case __dp_equality:
  67. if (__dist.first == 0)
  68. return true;
  69. break;
  70. case __dp_sign:
  71. case __dp_exact:
  72. // If range is not empty first iterator must be dereferenceable.
  73. if (__dist.first > 0)
  74. return !__check_dereferenceable || _M_dereferenceable();
  75. return __dist.first == 0;
  76. }
  77. // Assume that this is a valid range; we can't check anything else.
  78. return true;
  79. }
  80. } // namespace __gnu_debug
  81. #endif