libstdc++: safe_sequence.tcc Source File

libstdc++

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