libstdc++: safe_sequence.h Source File

libstdc++

safe_sequence.h
Go to the documentation of this file.
1// Safe sequence implementation -*- C++ -*-
2
3// Copyright (C) 2003-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file debug/safe_sequence.h
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H
30#define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1
31
32#include <debug/assertions.h>
33#include <debug/macros.h>
34#include <debug/functions.h>
35#include <debug/safe_base.h>
36
37namespace __gnu_debug
38{
39 /** A simple function object that returns true if the passed-in
40 * value is not equal to the stored value. It saves typing over
41 * using both bind1st and not_equal.
42 */
43 template<typename _Type>
44 class _Not_equal_to
45 {
46 _Type __value;
47
48 public:
49 _GLIBCXX20_CONSTEXPR
50 explicit _Not_equal_to(const _Type& __v) : __value(__v) { }
51
52 bool
53 operator()(const _Type& __x) const
54 { return __value != __x; }
55 };
56
57 /** A simple function object that returns true if the passed-in
58 * value is equal to the stored value. */
59 template <typename _Type>
60 class _Equal_to
61 {
62 _Type __value;
63
64 public:
65 _GLIBCXX20_CONSTEXPR
66 explicit _Equal_to(const _Type& __v) : __value(__v) { }
67
68 bool
69 operator()(const _Type& __x) const
70 { return __value == __x; }
71 };
72
73 /** A function object that returns true when the given random access
74 iterator is at least @c n steps away from the given iterator. */
75 template<typename _Iterator>
76 class _After_nth_from
77 {
78 typedef typename std::iterator_traits<_Iterator>::difference_type
79 difference_type;
80
81 _Iterator _M_base;
82 difference_type _M_n;
83
84 public:
85 _GLIBCXX20_CONSTEXPR
86 _After_nth_from(const difference_type& __n, const _Iterator& __base)
87 : _M_base(__base), _M_n(__n) { }
88
89 bool
90 operator()(const _Iterator& __x) const
91 { return __x - _M_base >= _M_n; }
92 };
93
94 /**
95 * @brief Base class for constructing a @a safe sequence type that
96 * tracks iterators that reference it.
97 *
98 * The class template %_Safe_sequence simplifies the construction of
99 * @a safe sequences that track the iterators that reference the
100 * sequence, so that the iterators are notified of changes in the
101 * sequence that may affect their operation, e.g., if the container
102 * invalidates its iterators or is destructed. This class template
103 * may only be used by deriving from it and passing the name of the
104 * derived class as its template parameter via the curiously
105 * recurring template pattern. The derived class must have @c
106 * iterator and @c const_iterator types that are instantiations of
107 * class template _Safe_iterator for this sequence. Iterators will
108 * then be tracked automatically.
109 */
110 template<typename _Sequence>
111 class _Safe_sequence : public _Safe_sequence_base
112 {
113 template<typename _Predicate>
114 void
115 _M_invalidate_if_impl(_Predicate __pred) const;
116
117 public:
118 /** Invalidates all iterators @c x that reference this sequence,
119 are not singular, and for which @c __pred(x) returns @c
120 true. @c __pred will be invoked with the normal iterators nested
121 in the safe ones. */
122 template<typename _Predicate>
123 _GLIBCXX20_CONSTEXPR void
124 _M_invalidate_if(_Predicate __pred) const
125 {
126 if (std::__is_constant_evaluated())
127 return;
128
129 _M_invalidate_if_impl(__pred);
130 }
131
132 /** Transfers all iterators @c x that reference @c from sequence,
133 are not singular, and for which @c __pred(x) returns @c
134 true. @c __pred will be invoked with the normal iterators nested
135 in the safe ones. */
136 template<typename _Predicate>
137 void
138 _M_transfer_from_if(const _Safe_sequence& __from,
139 _Predicate __pred) const;
140 };
141
142 /// Like _Safe_sequence but with a special _M_invalidate_all implementation
143 /// not invalidating past-the-end iterators. Used by node based sequence.
144 template<typename _Sequence>
145 class _Safe_node_sequence
146 : public _Safe_sequence<_Sequence>
147 {
148 public:
149#if __cplusplus >= 201103L
150 _Safe_node_sequence() = default;
151 _Safe_node_sequence(_Safe_node_sequence&&) = default;
152 _Safe_node_sequence(_Safe_node_sequence const&) = default;
153
154 _GLIBCXX20_CONSTEXPR _Safe_node_sequence&
155 operator=(_Safe_node_sequence&& __x) noexcept
156 {
157 _M_invalidate_all();
158 __x._M_invalidate_all();
159 return *this;
160 }
161#endif
162
163 _GLIBCXX20_CONSTEXPR _Safe_node_sequence&
164 operator=(const _Safe_node_sequence&) _GLIBCXX_NOEXCEPT
165 {
166 _M_invalidate_all();
167 return *this;
168 }
169
170 protected:
171 _GLIBCXX20_CONSTEXPR void
172 _M_invalidate_all() const
173 {
174 if (std::__is_constant_evaluated())
175 return;
176
177 typedef typename _Sequence::const_iterator _Const_iterator;
178 typedef typename _Const_iterator::iterator_type _Base_const_iterator;
179 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
180 const _Sequence& __seq = *static_cast<const _Sequence*>(this);
181 this->_M_invalidate_if(_Not_equal(__seq._M_base().end()));
182 }
183 };
184
185} // namespace __gnu_debug
186
187#include <debug/safe_sequence.tcc>
188
189#endif
assertions.h
functions.h
macros.h
safe_base.h
safe_sequence.tcc
__gnu_debug
GNU debug classes for public use.
Definition boost_concept_check.h:60
__gnu_debug::__base
constexpr _Iterator __base(_Iterator __it)
Definition helper_functions.h:339
std::iterator_traits
Traits class for iterators.
Definition stl_iterator_base_types.h:185
__gnu_debug::_Safe_sequence
Base class for constructing a safe sequence type that tracks iterators that reference it.
Definition safe_sequence.h:112
__gnu_debug::_Safe_sequence::_M_invalidate_if
constexpr void _M_invalidate_if(_Predicate __pred) const
Definition safe_sequence.h:124
__gnu_debug::_Safe_sequence::_M_transfer_from_if
void _M_transfer_from_if(const _Safe_sequence &__from, _Predicate __pred) const
Definition safe_sequence.tcc:69
__gnu_debug::_Safe_sequence_base::_M_invalidate_all
constexpr void _M_invalidate_all() const
Definition safe_base.h:314
__gnu_debug::_Not_equal_to
Definition safe_sequence.h:45