libstdc++
safe_sequence.tcc
Go to the documentation of this file.
1// Safe sequence implementation -*- C++ -*-
2
3// Copyright (C) 2010-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_sequence.tcc
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC
30#define _GLIBCXX_DEBUG_SAFE_SEQUENCE_TCC 1
31
32namespace __gnu_debug
33{
34 template<typename _Sequence>
35 template<typename _Predicate>
36 _GLIBCXX20_CONSTEXPR void
38 _M_invalidate_if(_Predicate __pred) const
39 {
40 if (std::__is_constant_evaluated())
41 return;
42
43 typedef typename _Sequence::iterator iterator;
44 typedef typename _Sequence::const_iterator const_iterator;
45
47 for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
48 {
49 iterator* __victim = static_cast<iterator*>(__iter);
50 __iter = __iter->_M_next;
51 if (!__victim->_M_singular() && __pred(__victim->base()))
52 {
53 __victim->_M_invalidate();
54 }
55 }
56
57 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
58 {
59 const_iterator* __victim = static_cast<const_iterator*>(__iter2);
60 __iter2 = __iter2->_M_next;
61 if (!__victim->_M_singular() && __pred(__victim->base()))
62 {
63 __victim->_M_invalidate();
64 }
65 }
66 }
67
68 template<typename _Sequence>
69 template<typename _Predicate>
70 void
72 _M_transfer_from_if(const _Safe_sequence& __from, _Predicate __pred) const
73 {
74 if (this == std::__addressof(__from))
75 return;
76
77 typedef typename _Sequence::iterator iterator;
78 typedef typename _Sequence::const_iterator const_iterator;
79
80 _Safe_iterator_base* __transfered_iterators = 0;
81 _Safe_iterator_base* __transfered_const_iterators = 0;
82 _Safe_iterator_base* __last_iterator = 0;
83 _Safe_iterator_base* __last_const_iterator = 0;
84 {
85 // We lock __from first and detach iterator(s) to transfer
87
88 for (_Safe_iterator_base* __iter = __from._M_iterators; __iter;)
89 {
90 _Safe_iterator_base* __victim_base = __iter;
91 iterator* __victim = static_cast<iterator*>(__victim_base);
92 __iter = __iter->_M_next;
93 if (!__victim->_M_singular() && __pred(__victim->base()))
94 {
95 __victim->_M_detach_single();
96 if (__transfered_iterators)
97 {
98 __victim_base->_M_next = __transfered_iterators;
99 __transfered_iterators->_M_prior = __victim_base;
100 }
101 else
102 __last_iterator = __victim_base;
103 __victim_base->_M_sequence = this;
104 __victim_base->_M_version = this->_M_version;
105 __transfered_iterators = __victim_base;
106 }
107 }
108
109 for (_Safe_iterator_base* __iter2 = __from._M_const_iterators;
110 __iter2;)
111 {
112 _Safe_iterator_base* __victim_base = __iter2;
113 const_iterator* __victim =
114 static_cast<const_iterator*>(__victim_base);
115 __iter2 = __iter2->_M_next;
116 if (!__victim->_M_singular() && __pred(__victim->base()))
117 {
118 __victim->_M_detach_single();
119 if (__transfered_const_iterators)
121 __victim_base->_M_next = __transfered_const_iterators;
122 __transfered_const_iterators->_M_prior = __victim_base;
123 }
124 else
125 __last_const_iterator = __victim;
126 __victim_base->_M_sequence = this;
127 __victim_base->_M_version = this->_M_version;
128 __transfered_const_iterators = __victim_base;
129 }
130 }
131 }
132
133 // Now we can lock *this and add the transfered iterators if any
134 if (__last_iterator || __last_const_iterator)
135 {
136 __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
137 if (__last_iterator)
138 {
139 if (this->_M_iterators)
140 {
141 this->_M_iterators->_M_prior = __last_iterator;
142 __last_iterator->_M_next = this->_M_iterators;
143 }
144 this->_M_iterators = __transfered_iterators;
145 }
146 if (__last_const_iterator)
147 {
148 if (this->_M_const_iterators)
149 {
150 this->_M_const_iterators->_M_prior = __last_const_iterator;
151 __last_const_iterator->_M_next = this->_M_const_iterators;
152 }
153 this->_M_const_iterators = __transfered_const_iterators;
154 }
155 }
156 }
157} // namespace __gnu_debug
158
159#endif
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
GNU debug classes for public use.
Base class for constructing a safe sequence type that tracks iterators that reference it.
constexpr void _M_invalidate_if(_Predicate __pred) const
void _M_transfer_from_if(const _Safe_sequence &__from, _Predicate __pred) const
const _Safe_sequence_base * _M_sequence
Definition safe_base.h:59
_Safe_iterator_base * _M_prior
Definition safe_base.h:72
_Safe_iterator_base * _M_next
Definition safe_base.h:76
_Safe_iterator_base * _M_const_iterators
The list of constant iterators that reference this container.
Definition safe_base.h:227
_Safe_iterator_base * _M_iterators
The list of mutable iterators that reference this container.
Definition safe_base.h:224
__gnu_cxx::__mutex & _M_get_mutex() const noexcept
unsigned int _M_version
The container version number. This number may never be 0.
Definition safe_base.h:230
Scoped lock idiom.