libstdc++
safe_unordered_base.h
Go to the documentation of this file.
1// Safe container/iterator base implementation -*- C++ -*-
2
3// Copyright (C) 2011-2025 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_unordered_base.h
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H
30#define _GLIBCXX_DEBUG_SAFE_UNORDERED_BASE_H 1
31
32#include <debug/safe_base.h>
33
34namespace __gnu_debug
35{
37
38 /** \brief Basic functionality for a @a safe iterator.
39 *
40 * The %_Safe_local_iterator_base base class implements the functionality
41 * of a safe local iterator that is not specific to a particular iterator
42 * type. It contains a pointer back to the container it references
43 * along with iterator version information and pointers to form a
44 * doubly-linked list of local iterators referenced by the container.
45 *
46 * This class must not perform any operations that can throw an
47 * exception, or the exception guarantees of derived iterators will
48 * be broken.
49 */
51 {
52 public:
54 _M_safe_container() const noexcept;
55
56 protected:
57 /** Initializes the iterator and makes it singular. */
60
61 /** Initialize the iterator to reference the container pointed to
62 * by @p __seq. @p __constant is true when we are initializing a
63 * constant local iterator, and false if it is a mutable local iterator.
64 * Note that @p __seq may be NULL, in which case the iterator will be
65 * singular. Otherwise, the iterator will reference @p __seq and
66 * be nonsingular.
67 */
69 bool __constant)
70 { _M_attach(__seq, __constant); }
71
72 /** Initializes the iterator to reference the same container that
73 @p __x does. @p __constant is true if this is a constant
74 iterator, and false if it is mutable. */
76 bool __constant)
77 { this->_M_attach(__x._M_safe_container(), __constant); }
78
80
81 /** Attaches this iterator to the given container, detaching it
82 * from whatever container it was attached to originally. If the
83 * new container is the NULL pointer, the iterator is left
84 * unattached.
85 */
86 void
88 bool __constant);
89
90 /** Likewise, but not thread-safe. */
91 void
93 bool __constant) noexcept;
94
95 /** Detach the iterator for whatever container it is attached to,
96 * if any.
97 */
98 void
100
101 /** Likewise, but not thread-safe. */
102 void
104
105#if !_GLIBCXX_INLINE_VERSION
106 private:
107 /***************************************************************/
108 /** Not-const method preserved for abi backward compatibility. */
109 void
110 _M_attach(_Safe_sequence_base* __seq, bool __constant);
111
112 void
113 _M_attach_single(_Safe_sequence_base* __seq, bool __constant) noexcept;
114 /***************************************************************/
115#endif
116 };
117
118 /**
119 * @brief Base class that supports tracking of local iterators that
120 * reference an unordered container.
121 *
122 * The %_Safe_unordered_container_base class provides basic support for
123 * tracking iterators into an unordered container. Containers that track
124 * iterators must derived from %_Safe_unordered_container_base publicly, so
125 * that safe iterators (which inherit _Safe_iterator_base) can
126 * attach to them. This class contains four linked lists of
127 * iterators, one for constant iterators, one for mutable
128 * iterators, one for constant local iterators, one for mutable local
129 * iterators and a version number that allows very fast
130 * invalidation of all iterators that reference the container.
131 *
132 * This class must ensure that no operation on it may throw an
133 * exception, otherwise @a safe containers may fail to provide the
134 * exception-safety guarantees required by the C++ standard.
135 */
136 class _Safe_unordered_container_base : public _Safe_sequence_base
137 {
138 friend class _Safe_local_iterator_base;
139 typedef _Safe_sequence_base _Base;
140
141 public:
142 /// The list of mutable local iterators that reference this container
143 mutable _Safe_iterator_base* _M_local_iterators;
144
145 /// The list of constant local iterators that reference this container
146 mutable _Safe_iterator_base* _M_const_local_iterators;
147
148 protected:
149 // Initialize with a version number of 1 and no iterators
150 _Safe_unordered_container_base() noexcept
152 { }
153
154 // Copy constructor does not copy iterators.
156 noexcept
158
159 // When moved unordered containers iterators are swapped.
160 _Safe_unordered_container_base(_Safe_unordered_container_base&& __x)
161 noexcept
162 : _Safe_unordered_container_base()
163 { this->_M_swap(__x); }
164
165 /** Notify all iterators that reference this container that the
166 container is being destroyed. */
169
170 /** Detach all iterators, leaving them singular. */
171 void
173
174 /** Swap this container with the given container. This operation
175 * also swaps ownership of the iterators, so that when the
176 * operation is complete all iterators that originally referenced
177 * one container now reference the other container.
178 */
179 void
180 _M_swap(const _Safe_unordered_container_base& __x) const noexcept;
181
182 private:
183#if !_GLIBCXX_INLINE_VERSION
184 /***************************************************************/
185 /** Not-const method preserved for abi backward compatibility. */
186 void
188
189 void
190 _M_swap(_Safe_unordered_container_base& __x) noexcept;
191 /***************************************************************/
192#endif
193
194 /** Attach an iterator to this container. */
195 void
196 _M_attach_local(_Safe_iterator_base* __it, bool __constant) const;
197
198 /** Likewise but not thread safe. */
199 void
200 _M_attach_local_single(_Safe_iterator_base* __it,
201 bool __constant) const noexcept;
202
203 /** Detach an iterator from this container */
204 void
205 _M_detach_local(_Safe_iterator_base* __it) const;
206
207 /** Likewise but not thread safe. */
208 void
209 _M_detach_local_single(_Safe_iterator_base* __it) const noexcept;
210 };
211
213 _Safe_local_iterator_base::
214 _M_safe_container() const noexcept
215 { return static_cast<const _Safe_unordered_container_base*>(_M_sequence); }
216} // namespace __gnu_debug
217
218#endif
GNU debug classes for public use.
const _Safe_sequence_base * _M_sequence
Definition safe_base.h:59
Basic functionality for a safe iterator.
_Safe_local_iterator_base(const _Safe_local_iterator_base &__x, bool __constant)
void _M_attach_single(const _Safe_unordered_container_base *__cont, bool __constant) noexcept
void _M_attach(const _Safe_unordered_container_base *__cont, bool __constant)
_Safe_local_iterator_base(const _Safe_unordered_container_base *__seq, bool __constant)
Base class that supports tracking of local iterators that reference an unordered container.
void _M_swap(const _Safe_unordered_container_base &__x) const noexcept
_Safe_iterator_base * _M_const_local_iterators
The list of constant local iterators that reference this container.
_Safe_iterator_base * _M_local_iterators
The list of mutable local iterators that reference this container.