You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.0KB

  1. // Safe sequence 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_sequence.h
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H
  24. #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1
  25. #include <debug/assertions.h>
  26. #include <debug/macros.h>
  27. #include <debug/functions.h>
  28. #include <debug/safe_base.h>
  29. namespace __gnu_debug
  30. {
  31. /** A simple function object that returns true if the passed-in
  32. * value is not equal to the stored value. It saves typing over
  33. * using both bind1st and not_equal.
  34. */
  35. template<typename _Type>
  36. class _Not_equal_to
  37. {
  38. _Type __value;
  39. public:
  40. explicit _Not_equal_to(const _Type& __v) : __value(__v) { }
  41. bool
  42. operator()(const _Type& __x) const
  43. { return __value != __x; }
  44. };
  45. /** A simple function object that returns true if the passed-in
  46. * value is equal to the stored value. */
  47. template <typename _Type>
  48. class _Equal_to
  49. {
  50. _Type __value;
  51. public:
  52. explicit _Equal_to(const _Type& __v) : __value(__v) { }
  53. bool
  54. operator()(const _Type& __x) const
  55. { return __value == __x; }
  56. };
  57. /** A function object that returns true when the given random access
  58. iterator is at least @c n steps away from the given iterator. */
  59. template<typename _Iterator>
  60. class _After_nth_from
  61. {
  62. typedef typename std::iterator_traits<_Iterator>::difference_type
  63. difference_type;
  64. _Iterator _M_base;
  65. difference_type _M_n;
  66. public:
  67. _After_nth_from(const difference_type& __n, const _Iterator& __base)
  68. : _M_base(__base), _M_n(__n) { }
  69. bool
  70. operator()(const _Iterator& __x) const
  71. { return __x - _M_base >= _M_n; }
  72. };
  73. /**
  74. * @brief Base class for constructing a @a safe sequence type that
  75. * tracks iterators that reference it.
  76. *
  77. * The class template %_Safe_sequence simplifies the construction of
  78. * @a safe sequences that track the iterators that reference the
  79. * sequence, so that the iterators are notified of changes in the
  80. * sequence that may affect their operation, e.g., if the container
  81. * invalidates its iterators or is destructed. This class template
  82. * may only be used by deriving from it and passing the name of the
  83. * derived class as its template parameter via the curiously
  84. * recurring template pattern. The derived class must have @c
  85. * iterator and @c const_iterator types that are instantiations of
  86. * class template _Safe_iterator for this sequence. Iterators will
  87. * then be tracked automatically.
  88. */
  89. template<typename _Sequence>
  90. class _Safe_sequence : public _Safe_sequence_base
  91. {
  92. public:
  93. /** Invalidates all iterators @c x that reference this sequence,
  94. are not singular, and for which @c __pred(x) returns @c
  95. true. @c __pred will be invoked with the normal iterators nested
  96. in the safe ones. */
  97. template<typename _Predicate>
  98. void
  99. _M_invalidate_if(_Predicate __pred);
  100. /** Transfers all iterators @c x that reference @c from sequence,
  101. are not singular, and for which @c __pred(x) returns @c
  102. true. @c __pred will be invoked with the normal iterators nested
  103. in the safe ones. */
  104. template<typename _Predicate>
  105. void
  106. _M_transfer_from_if(_Safe_sequence& __from, _Predicate __pred);
  107. };
  108. /// Like _Safe_sequence but with a special _M_invalidate_all implementation
  109. /// not invalidating past-the-end iterators. Used by node based sequence.
  110. template<typename _Sequence>
  111. class _Safe_node_sequence
  112. : public _Safe_sequence<_Sequence>
  113. {
  114. protected:
  115. void
  116. _M_invalidate_all()
  117. {
  118. typedef typename _Sequence::const_iterator _Const_iterator;
  119. typedef typename _Const_iterator::iterator_type _Base_const_iterator;
  120. typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
  121. const _Sequence& __seq = *static_cast<_Sequence*>(this);
  122. this->_M_invalidate_if(_Not_equal(__seq._M_base().end()));
  123. }
  124. };
  125. } // namespace __gnu_debug
  126. #include <debug/safe_sequence.tcc>
  127. #endif