]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/debug/forward_list
libstdc++: Make debug containers prefer copy ctor to base ctor (PR 90102)
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / forward_list
CommitLineData
b8b4301e
PC
1// <forward_list> -*- C++ -*-
2
8d9254fc 3// Copyright (C) 2010-2020 Free Software Foundation, Inc.
b8b4301e
PC
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/forward_list
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_FORWARD_LIST
30#define _GLIBCXX_DEBUG_FORWARD_LIST 1
31
32#pragma GCC system_header
33
9ca2ac69
JW
34#include <bits/c++config.h>
35namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
36 template<typename _Tp, typename _Allocator> class forward_list;
37} } // namespace std::__debug
38
b8b4301e
PC
39#include <forward_list>
40#include <debug/safe_sequence.h>
15ee1a77 41#include <debug/safe_container.h>
b8b4301e
PC
42#include <debug/safe_iterator.h>
43
24167c42
FD
44// Special validity check for forward_list ranges.
45#define __glibcxx_check_valid_fl_range(_First,_Last,_Dist) \
46_GLIBCXX_DEBUG_VERIFY(_First._M_valid_range(_Last, _Dist, false), \
47 _M_message(__gnu_debug::__msg_valid_range) \
48 ._M_iterator(_First, #_First) \
49 ._M_iterator(_Last, #_Last))
50
15ee1a77
FD
51namespace __gnu_debug
52{
53 /// Special iterators swap and invalidation for forward_list because of the
54 /// before_begin iterator.
55 template<typename _SafeSequence>
56 class _Safe_forward_list
57 : public _Safe_sequence<_SafeSequence>
58 {
59 _SafeSequence&
60 _M_this() noexcept
61 { return *static_cast<_SafeSequence*>(this); }
62
63 static void
64 _M_swap_aux(_Safe_sequence_base& __lhs,
65 _Safe_iterator_base*& __lhs_iterators,
66 _Safe_sequence_base& __rhs,
67 _Safe_iterator_base*& __rhs_iterators);
68
69 void _M_swap_single(_Safe_sequence_base&) noexcept;
70
71 protected:
72 void
73 _M_invalidate_all()
74 {
75 using _Base_const_iterator = __decltype(_M_this()._M_base().cend());
76 this->_M_invalidate_if([this](_Base_const_iterator __it)
77 {
78 return __it != _M_this()._M_base().cbefore_begin()
79 && __it != _M_this()._M_base().cend(); });
80 }
81
82 void _M_swap(_Safe_sequence_base&) noexcept;
83 };
84
85 template<typename _SafeSequence>
86 void
87 _Safe_forward_list<_SafeSequence>::
88 _M_swap_aux(_Safe_sequence_base& __lhs,
89 _Safe_iterator_base*& __lhs_iterators,
90 _Safe_sequence_base& __rhs,
91 _Safe_iterator_base*& __rhs_iterators)
92 {
93 using const_iterator = typename _SafeSequence::const_iterator;
94 _Safe_iterator_base* __bbegin_its = 0;
95 _Safe_iterator_base* __last_bbegin = 0;
96 _SafeSequence& __rseq = static_cast<_SafeSequence&>(__rhs);
97
98 for (_Safe_iterator_base* __iter = __lhs_iterators; __iter;)
99 {
100 // Even iterator is cast to const_iterator, not a problem.
ebd4c354
FD
101 _Safe_iterator_base* __victim_base = __iter;
102 const_iterator* __victim =
103 static_cast<const_iterator*>(__victim_base);
15ee1a77
FD
104 __iter = __iter->_M_next;
105 if (__victim->base() == __rseq._M_base().cbefore_begin())
106 {
107 __victim->_M_unlink();
ebd4c354
FD
108 if (__lhs_iterators == __victim_base)
109 __lhs_iterators = __victim_base->_M_next;
15ee1a77
FD
110 if (__bbegin_its)
111 {
ebd4c354
FD
112 __victim_base->_M_next = __bbegin_its;
113 __bbegin_its->_M_prior = __victim_base;
15ee1a77
FD
114 }
115 else
ebd4c354
FD
116 __last_bbegin = __victim_base;
117 __bbegin_its = __victim_base;
15ee1a77
FD
118 }
119 else
9649e5b6 120 __victim_base->_M_sequence = std::__addressof(__lhs);
15ee1a77
FD
121 }
122
123 if (__bbegin_its)
124 {
125 if (__rhs_iterators)
126 {
127 __rhs_iterators->_M_prior = __last_bbegin;
128 __last_bbegin->_M_next = __rhs_iterators;
129 }
130 __rhs_iterators = __bbegin_its;
131 }
132 }
133
134 template<typename _SafeSequence>
135 void
136 _Safe_forward_list<_SafeSequence>::
137 _M_swap_single(_Safe_sequence_base& __other) noexcept
138 {
139 std::swap(_M_this()._M_iterators, __other._M_iterators);
140 std::swap(_M_this()._M_const_iterators, __other._M_const_iterators);
141 // Useless, always 1 on forward_list
142 //std::swap(_M_this()_M_version, __other._M_version);
143 _Safe_iterator_base* __this_its = _M_this()._M_iterators;
144 _M_swap_aux(__other, __other._M_iterators,
145 _M_this(), _M_this()._M_iterators);
146 _Safe_iterator_base* __this_const_its = _M_this()._M_const_iterators;
147 _M_swap_aux(__other, __other._M_const_iterators,
148 _M_this(), _M_this()._M_const_iterators);
149 _M_swap_aux(_M_this(), __this_its,
150 __other, __other._M_iterators);
151 _M_swap_aux(_M_this(), __this_const_its,
152 __other, __other._M_const_iterators);
153 }
154
155 /* Special forward_list _M_swap version that does not swap the
156 * before-begin ownership.*/
157 template<typename _SafeSequence>
158 void
159 _Safe_forward_list<_SafeSequence>::
160 _M_swap(_Safe_sequence_base& __other) noexcept
161 {
162 // We need to lock both sequences to swap
163 using namespace __gnu_cxx;
164 __mutex *__this_mutex = &_M_this()._M_get_mutex();
165 __mutex *__other_mutex =
166 &static_cast<_SafeSequence&>(__other)._M_get_mutex();
167 if (__this_mutex == __other_mutex)
168 {
169 __scoped_lock __lock(*__this_mutex);
170 _M_swap_single(__other);
171 }
172 else
173 {
174 __scoped_lock __l1(__this_mutex < __other_mutex
175 ? *__this_mutex : *__other_mutex);
176 __scoped_lock __l2(__this_mutex < __other_mutex
177 ? *__other_mutex : *__this_mutex);
178 _M_swap_single(__other);
179 }
180 }
181}
182
12ffa228 183namespace std _GLIBCXX_VISIBILITY(default)
b8b4301e
PC
184{
185namespace __debug
186{
187 /// Class std::forward_list with safety/checking/debug instrumentation.
188 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
189 class forward_list
15ee1a77
FD
190 : public __gnu_debug::_Safe_container<
191 forward_list<_Tp, _Alloc>, _Alloc, __gnu_debug::_Safe_forward_list>,
192 public _GLIBCXX_STD_C::forward_list<_Tp, _Alloc>
b8b4301e 193 {
15ee1a77
FD
194 typedef _GLIBCXX_STD_C::forward_list<_Tp, _Alloc> _Base;
195 typedef __gnu_debug::_Safe_container<
196 forward_list, _Alloc, __gnu_debug::_Safe_forward_list> _Safe;
b8b4301e 197
15ee1a77
FD
198 typedef typename _Base::iterator _Base_iterator;
199 typedef typename _Base::const_iterator _Base_const_iterator;
e9afbed0
FD
200
201 template<typename _ItT, typename _SeqT, typename _CatT>
202 friend class ::__gnu_debug::_Safe_iterator;
e77c9aed 203
eca833b8
JW
204 // Reference wrapper for base class. See PR libstdc++/90102.
205 struct _Base_ref
206 {
207 _Base_ref(const _Base& __r) : _M_ref(__r) { }
208
209 const _Base& _M_ref;
210 };
211
b8b4301e 212 public:
15ee1a77
FD
213 typedef typename _Base::reference reference;
214 typedef typename _Base::const_reference const_reference;
b8b4301e 215
15ee1a77
FD
216 typedef __gnu_debug::_Safe_iterator<
217 _Base_iterator, forward_list> iterator;
218 typedef __gnu_debug::_Safe_iterator<
219 _Base_const_iterator, forward_list> const_iterator;
b8b4301e 220
15ee1a77
FD
221 typedef typename _Base::size_type size_type;
222 typedef typename _Base::difference_type difference_type;
b8b4301e 223
15ee1a77
FD
224 typedef _Tp value_type;
225 typedef typename _Base::allocator_type allocator_type;
226 typedef typename _Base::pointer pointer;
227 typedef typename _Base::const_pointer const_pointer;
b8b4301e
PC
228
229 // 23.2.3.1 construct/copy/destroy:
21bdef94
JW
230
231 forward_list() = default;
232
b8b4301e 233 explicit
21bdef94 234 forward_list(const allocator_type& __al) noexcept
b8b4301e
PC
235 : _Base(__al) { }
236
15ee1a77 237 forward_list(const forward_list& __list, const allocator_type& __al)
b8b4301e
PC
238 : _Base(__list, __al)
239 { }
240
15ee1a77
FD
241 forward_list(forward_list&& __list, const allocator_type& __al)
242 : _Safe(std::move(__list._M_safe()), __al),
243 _Base(std::move(__list._M_base()), __al)
244 { }
b8b4301e
PC
245
246 explicit
15ee1a77 247 forward_list(size_type __n, const allocator_type& __al = allocator_type())
e77c9aed 248 : _Base(__n, __al)
b8b4301e
PC
249 { }
250
251 forward_list(size_type __n, const _Tp& __value,
15ee1a77 252 const allocator_type& __al = allocator_type())
b8b4301e
PC
253 : _Base(__n, __value, __al)
254 { }
255
2203cb90
PC
256 template<typename _InputIterator,
257 typename = std::_RequireInputIter<_InputIterator>>
258 forward_list(_InputIterator __first, _InputIterator __last,
15ee1a77 259 const allocator_type& __al = allocator_type())
90aabc7e
FD
260 : _Base(__gnu_debug::__base(
261 __glibcxx_check_valid_constructor_range(__first, __last)),
b8b4301e 262 __gnu_debug::__base(__last), __al)
15ee1a77 263 { }
b8b4301e 264
15ee1a77 265 forward_list(const forward_list&) = default;
b8b4301e 266
15ee1a77 267 forward_list(forward_list&&) = default;
b8b4301e
PC
268
269 forward_list(std::initializer_list<_Tp> __il,
15ee1a77 270 const allocator_type& __al = allocator_type())
b8b4301e
PC
271 : _Base(__il, __al)
272 { }
273
15ee1a77 274 ~forward_list() = default;
b8b4301e 275
eca833b8
JW
276 forward_list(_Base_ref __x) : _Base(__x._M_ref) { }
277
b8b4301e 278 forward_list&
15ee1a77 279 operator=(const forward_list&) = default;
b8b4301e
PC
280
281 forward_list&
15ee1a77 282 operator=(forward_list&&) = default;
b8b4301e
PC
283
284 forward_list&
285 operator=(std::initializer_list<_Tp> __il)
286 {
15ee1a77 287 _M_base() = __il;
b8b4301e 288 this->_M_invalidate_all();
15ee1a77 289 return *this;
b8b4301e
PC
290 }
291
2203cb90
PC
292 template<typename _InputIterator,
293 typename = std::_RequireInputIter<_InputIterator>>
294 void
15ee1a77
FD
295 assign(_InputIterator __first, _InputIterator __last)
296 {
24167c42
FD
297 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
298 __glibcxx_check_valid_range2(__first, __last, __dist);
299
300 if (__dist.second >= __gnu_debug::__dp_sign)
301 _Base::assign(__gnu_debug::__unsafe(__first),
302 __gnu_debug::__unsafe(__last));
303 else
304 _Base::assign(__first, __last);
305
b8b4301e 306 this->_M_invalidate_all();
15ee1a77 307 }
b8b4301e
PC
308
309 void
310 assign(size_type __n, const _Tp& __val)
311 {
312 _Base::assign(__n, __val);
313 this->_M_invalidate_all();
314 }
315
316 void
317 assign(std::initializer_list<_Tp> __il)
318 {
319 _Base::assign(__il);
320 this->_M_invalidate_all();
321 }
322
323 using _Base::get_allocator;
324
325 // iterators:
326
327 iterator
d3677132 328 before_begin() noexcept
3e670ecf 329 { return { _Base::before_begin(), this }; }
b8b4301e
PC
330
331 const_iterator
d3677132 332 before_begin() const noexcept
3e670ecf 333 { return { _Base::before_begin(), this }; }
b8b4301e
PC
334
335 iterator
d3677132 336 begin() noexcept
3e670ecf 337 { return { _Base::begin(), this }; }
b8b4301e
PC
338
339 const_iterator
d3677132 340 begin() const noexcept
3e670ecf 341 { return { _Base::begin(), this }; }
b8b4301e
PC
342
343 iterator
d3677132 344 end() noexcept
3e670ecf 345 { return { _Base::end(), this }; }
b8b4301e
PC
346
347 const_iterator
d3677132 348 end() const noexcept
3e670ecf 349 { return { _Base::end(), this }; }
b8b4301e
PC
350
351 const_iterator
d3677132 352 cbegin() const noexcept
3e670ecf 353 { return { _Base::cbegin(), this }; }
b8b4301e
PC
354
355 const_iterator
d3677132 356 cbefore_begin() const noexcept
3e670ecf 357 { return { _Base::cbefore_begin(), this }; }
b8b4301e
PC
358
359 const_iterator
d3677132 360 cend() const noexcept
3e670ecf 361 { return { _Base::cend(), this }; }
b8b4301e
PC
362
363 using _Base::empty;
364 using _Base::max_size;
365
366 // element access:
367
368 reference
369 front()
370 {
371 __glibcxx_check_nonempty();
372 return _Base::front();
373 }
374
375 const_reference
376 front() const
377 {
378 __glibcxx_check_nonempty();
379 return _Base::front();
380 }
381
3c732e6f 382 // modifiers:
b8b4301e
PC
383
384 using _Base::emplace_front;
385 using _Base::push_front;
386
387 void
388 pop_front()
389 {
390 __glibcxx_check_nonempty();
afe96d41
FD
391 this->_M_invalidate_if([this](_Base_const_iterator __it)
392 { return __it == this->_M_base().cbegin(); });
b8b4301e
PC
393 _Base::pop_front();
394 }
395
396 template<typename... _Args>
397 iterator
398 emplace_after(const_iterator __pos, _Args&&... __args)
399 {
400 __glibcxx_check_insert_after(__pos);
3e670ecf 401 return { _Base::emplace_after(__pos.base(),
b8b4301e 402 std::forward<_Args>(__args)...),
3e670ecf 403 this };
b8b4301e
PC
404 }
405
406 iterator
407 insert_after(const_iterator __pos, const _Tp& __val)
408 {
409 __glibcxx_check_insert_after(__pos);
3e670ecf 410 return { _Base::insert_after(__pos.base(), __val), this };
b8b4301e
PC
411 }
412
413 iterator
414 insert_after(const_iterator __pos, _Tp&& __val)
415 {
416 __glibcxx_check_insert_after(__pos);
3e670ecf 417 return { _Base::insert_after(__pos.base(), std::move(__val)), this };
b8b4301e
PC
418 }
419
420 iterator
421 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
422 {
423 __glibcxx_check_insert_after(__pos);
3e670ecf 424 return { _Base::insert_after(__pos.base(), __n, __val), this };
b8b4301e
PC
425 }
426
2203cb90
PC
427 template<typename _InputIterator,
428 typename = std::_RequireInputIter<_InputIterator>>
15ee1a77
FD
429 iterator
430 insert_after(const_iterator __pos,
431 _InputIterator __first, _InputIterator __last)
432 {
24167c42
FD
433 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
434 __glibcxx_check_insert_range_after(__pos, __first, __last, __dist);
435
436 if (__dist.second >= __gnu_debug::__dp_sign)
437 return
438 {
439 _Base::insert_after(__pos.base(),
440 __gnu_debug::__unsafe(__first),
441 __gnu_debug::__unsafe(__last)),
442 this
443 };
444 else
445 return { _Base::insert_after(__pos.base(), __first, __last), this };
15ee1a77 446 }
b8b4301e
PC
447
448 iterator
449 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
450 {
451 __glibcxx_check_insert_after(__pos);
3e670ecf 452 return { _Base::insert_after(__pos.base(), __il), this };
b8b4301e
PC
453 }
454
afe96d41
FD
455 private:
456 _Base_iterator
457 _M_erase_after(_Base_const_iterator __pos)
458 {
459 _Base_const_iterator __next = std::next(__pos);
460 this->_M_invalidate_if([__next](_Base_const_iterator __it)
461 { return __it == __next; });
462 return _Base::erase_after(__pos);
463 }
464 public:
efb7b456 465 iterator
b8b4301e
PC
466 erase_after(const_iterator __pos)
467 {
468 __glibcxx_check_erase_after(__pos);
3e670ecf 469 return { _M_erase_after(__pos.base()), this };
b8b4301e
PC
470 }
471
efb7b456 472 iterator
b8b4301e
PC
473 erase_after(const_iterator __pos, const_iterator __last)
474 {
475 __glibcxx_check_erase_range_after(__pos, __last);
afe96d41
FD
476 for (_Base_const_iterator __victim = std::next(__pos.base());
477 __victim != __last.base(); ++__victim)
b8b4301e 478 {
3e670ecf 479 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::cend(),
afe96d41
FD
480 _M_message(__gnu_debug::__msg_valid_range2)
481 ._M_sequence(*this, "this")
482 ._M_iterator(__pos, "pos")
483 ._M_iterator(__last, "last"));
484 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
485 { return __it == __victim; });
b8b4301e 486 }
3e670ecf
FD
487
488 return { _Base::erase_after(__pos.base(), __last.base()), this };
b8b4301e
PC
489 }
490
491 void
492 swap(forward_list& __list)
c5d9ec56 493 noexcept( noexcept(declval<_Base&>().swap(__list)) )
b8b4301e 494 {
15ee1a77 495 _Safe::_M_swap(__list);
b8b4301e 496 _Base::swap(__list);
b8b4301e
PC
497 }
498
499 void
500 resize(size_type __sz)
501 {
502 this->_M_detach_singular();
503
504 // if __sz < size(), invalidate all iterators in [begin+__sz, end()
afe96d41
FD
505 _Base_iterator __victim = _Base::begin();
506 _Base_iterator __end = _Base::end();
b8b4301e
PC
507 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
508 ++__victim;
509
afe96d41 510 for (; __victim != __end; ++__victim)
b8b4301e 511 {
afe96d41
FD
512 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
513 { return __it == __victim; });
b8b4301e
PC
514 }
515
516 __try
517 {
518 _Base::resize(__sz);
519 }
520 __catch(...)
521 {
522 this->_M_revalidate_singular();
523 __throw_exception_again;
15ee1a77 524 }
b8b4301e
PC
525 }
526
527 void
528 resize(size_type __sz, const value_type& __val)
529 {
530 this->_M_detach_singular();
531
532 // if __sz < size(), invalidate all iterators in [begin+__sz, end())
afe96d41
FD
533 _Base_iterator __victim = _Base::begin();
534 _Base_iterator __end = _Base::end();
b8b4301e
PC
535 for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
536 ++__victim;
537
afe96d41 538 for (; __victim != __end; ++__victim)
b8b4301e 539 {
afe96d41
FD
540 this->_M_invalidate_if([__victim](_Base_const_iterator __it)
541 { return __it == __victim; });
b8b4301e
PC
542 }
543
544 __try
545 {
546 _Base::resize(__sz, __val);
547 }
548 __catch(...)
549 {
550 this->_M_revalidate_singular();
551 __throw_exception_again;
15ee1a77 552 }
b8b4301e
PC
553 }
554
555 void
d3677132 556 clear() noexcept
b8b4301e
PC
557 {
558 _Base::clear();
559 this->_M_invalidate_all();
560 }
561
562 // 23.2.3.5 forward_list operations:
b8b4301e
PC
563 void
564 splice_after(const_iterator __pos, forward_list&& __list)
565 {
15ee1a77 566 __glibcxx_check_insert_after(__pos);
9649e5b6 567 _GLIBCXX_DEBUG_VERIFY(std::__addressof(__list) != this,
b8b4301e
PC
568 _M_message(__gnu_debug::__msg_self_splice)
569 ._M_sequence(*this, "this"));
7c688ade
JW
570 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
571 _M_message(__gnu_debug::__msg_splice_alloc)
572 ._M_sequence(*this)
573 ._M_sequence(__list, "__list"));
afe96d41
FD
574 this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it)
575 {
576 return __it != __list._M_base().cbefore_begin()
577 && __it != __list._M_base().end();
578 });
579 _Base::splice_after(__pos.base(), std::move(__list._M_base()));
b8b4301e
PC
580 }
581
78263296
PC
582 void
583 splice_after(const_iterator __pos, forward_list& __list)
584 { splice_after(__pos, std::move(__list)); }
585
b8b4301e
PC
586 void
587 splice_after(const_iterator __pos, forward_list&& __list,
588 const_iterator __i)
589 {
590 __glibcxx_check_insert_after(__pos);
591 _GLIBCXX_DEBUG_VERIFY(__i._M_before_dereferenceable(),
592 _M_message(__gnu_debug::__msg_splice_bad)
593 ._M_iterator(__i, "__i"));
9649e5b6 594 _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(std::__addressof(__list)),
b8b4301e
PC
595 _M_message(__gnu_debug::__msg_splice_other)
596 ._M_iterator(__i, "__i")
597 ._M_sequence(__list, "__list"));
7c688ade
JW
598 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
599 _M_message(__gnu_debug::__msg_splice_alloc)
600 ._M_sequence(*this)
601 ._M_sequence(__list, "__list"));
b8b4301e
PC
602
603 // _GLIBCXX_RESOLVE_LIB_DEFECTS
604 // 250. splicing invalidates iterators
afe96d41
FD
605 _Base_const_iterator __next = std::next(__i.base());
606 this->_M_transfer_from_if(__list, [__next](_Base_const_iterator __it)
607 { return __it == __next; });
b8b4301e
PC
608 _Base::splice_after(__pos.base(), std::move(__list._M_base()),
609 __i.base());
610 }
611
78263296
PC
612 void
613 splice_after(const_iterator __pos, forward_list& __list,
614 const_iterator __i)
615 { splice_after(__pos, std::move(__list), __i); }
616
b8b4301e
PC
617 void
618 splice_after(const_iterator __pos, forward_list&& __list,
619 const_iterator __before, const_iterator __last)
620 {
24167c42 621 typename __gnu_debug::_Distance_traits<const_iterator>::__type __dist;
9649e5b6 622 auto __listptr = std::__addressof(__list);
15ee1a77 623 __glibcxx_check_insert_after(__pos);
24167c42 624 __glibcxx_check_valid_fl_range(__before, __last, __dist);
9649e5b6 625 _GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(__listptr),
b8b4301e
PC
626 _M_message(__gnu_debug::__msg_splice_other)
627 ._M_sequence(__list, "list")
628 ._M_iterator(__before, "before"));
629 _GLIBCXX_DEBUG_VERIFY(__before._M_dereferenceable()
630 || __before._M_is_before_begin(),
631 _M_message(__gnu_debug::__msg_valid_range2)
632 ._M_sequence(__list, "list")
633 ._M_iterator(__before, "before")
634 ._M_iterator(__last, "last"));
635 _GLIBCXX_DEBUG_VERIFY(__before != __last,
636 _M_message(__gnu_debug::__msg_valid_range2)
637 ._M_sequence(__list, "list")
638 ._M_iterator(__before, "before")
639 ._M_iterator(__last, "last"));
7c688ade
JW
640 _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
641 _M_message(__gnu_debug::__msg_splice_alloc)
642 ._M_sequence(*this)
643 ._M_sequence(__list, "__list"));
afe96d41
FD
644
645 for (_Base_const_iterator __tmp = std::next(__before.base());
646 __tmp != __last.base(); ++__tmp)
647 {
648 _GLIBCXX_DEBUG_VERIFY(__tmp != __list._M_base().end(),
649 _M_message(__gnu_debug::__msg_valid_range2)
650 ._M_sequence(__list, "list")
651 ._M_iterator(__before, "before")
652 ._M_iterator(__last, "last"));
9649e5b6 653 _GLIBCXX_DEBUG_VERIFY(__listptr != this || __tmp != __pos.base(),
15ee1a77
FD
654 _M_message(__gnu_debug::__msg_splice_overlap)
655 ._M_iterator(__tmp, "position")
afe96d41
FD
656 ._M_iterator(__before, "before")
657 ._M_iterator(__last, "last"));
658 // _GLIBCXX_RESOLVE_LIB_DEFECTS
659 // 250. splicing invalidates iterators
660 this->_M_transfer_from_if(__list, [__tmp](_Base_const_iterator __it)
661 { return __it == __tmp; });
662 }
663
664 _Base::splice_after(__pos.base(), std::move(__list._M_base()),
665 __before.base(), __last.base());
b8b4301e
PC
666 }
667
78263296
PC
668 void
669 splice_after(const_iterator __pos, forward_list& __list,
670 const_iterator __before, const_iterator __last)
671 { splice_after(__pos, std::move(__list), __before, __last); }
672
f7191a37
FD
673 private:
674#if __cplusplus > 201703L
675 using __remove_return_type = size_type;
676# define _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG \
677 __attribute__((__abi_tag__("__cxx20")))
678# define _GLIBCXX20_ONLY(__expr) __expr
679#else
680 using __remove_return_type = void;
681# define _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
682# define _GLIBCXX20_ONLY(__expr)
683#endif
684
685 public:
686 _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
687 __remove_return_type
b8b4301e
PC
688 remove(const _Tp& __val)
689 {
f7191a37
FD
690 if (!this->_M_iterators && !this->_M_const_iterators)
691 return _Base::remove(__val);
692
693 size_type __removed __attribute__((__unused__)) = 0;
afe96d41
FD
694 _Base_iterator __x = _Base::before_begin();
695 _Base_iterator __old = __x++;
f7191a37 696 _Base_iterator __extra = _Base::end();
afe96d41 697 while (__x != _Base::end())
b8b4301e
PC
698 {
699 if (*__x == __val)
f7191a37
FD
700 {
701 if (std::__addressof(*__x) != std::__addressof(__val))
702 {
703 __x = _M_erase_after(__old);
704 _GLIBCXX20_ONLY( __removed++ );
705 continue;
706 }
707 else
708 __extra = __old;
709 }
710 __old = __x++;
b8b4301e 711 }
f7191a37
FD
712
713 if (__extra != _Base::end())
714 {
715 this->_M_erase_after(__extra);
716 _GLIBCXX20_ONLY( __removed++ );
717 }
718
719 return _GLIBCXX20_ONLY( __removed );
b8b4301e
PC
720 }
721
722 template<typename _Pred>
f7191a37 723 __remove_return_type
15ee1a77 724 remove_if(_Pred __pred)
b8b4301e 725 {
f7191a37
FD
726 if (!this->_M_iterators && !this->_M_const_iterators)
727 return _Base::remove_if(__pred);
728
729 size_type __removed __attribute__((__unused__)) = 0;
afe96d41
FD
730 _Base_iterator __x = _Base::before_begin();
731 _Base_iterator __old = __x++;
732 while (__x != _Base::end())
f7191a37
FD
733 if (__pred(*__x))
734 {
afe96d41 735 __x = _M_erase_after(__old);
f7191a37
FD
736 _GLIBCXX20_ONLY( __removed++ );
737 }
738 else
739 __old = __x++;
740
741 return _GLIBCXX20_ONLY( __removed );
b8b4301e
PC
742 }
743
f7191a37
FD
744 _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
745 __remove_return_type
b8b4301e 746 unique()
f7191a37 747 { return unique(std::equal_to<_Tp>()); }
b8b4301e
PC
748
749 template<typename _BinPred>
f7191a37 750 __remove_return_type
15ee1a77 751 unique(_BinPred __binary_pred)
b8b4301e 752 {
f7191a37
FD
753 if (!this->_M_iterators && !this->_M_const_iterators)
754 return _Base::unique(__binary_pred);
755
afe96d41
FD
756 _Base_iterator __first = _Base::begin();
757 _Base_iterator __last = _Base::end();
b8b4301e 758 if (__first == __last)
f7191a37
FD
759 return _GLIBCXX20_ONLY(0);
760
761 size_type __removed __attribute__((__unused__)) = 0;
afe96d41
FD
762 _Base_iterator __next = std::next(__first);
763 while (__next != __last)
b8b4301e
PC
764 {
765 if (__binary_pred(*__first, *__next))
f7191a37
FD
766 {
767 __next = _M_erase_after(__first);
768 _GLIBCXX20_ONLY( __removed++ );
769 }
b8b4301e 770 else
afe96d41 771 __first = __next++;
b8b4301e 772 }
f7191a37
FD
773
774 return _GLIBCXX20_ONLY( __removed );
b8b4301e
PC
775 }
776
f7191a37
FD
777#undef _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
778#undef _GLIBCXX20_ONLY
779
b8b4301e
PC
780 void
781 merge(forward_list&& __list)
782 {
9649e5b6 783 if (this != std::__addressof(__list))
b8b4301e
PC
784 {
785 __glibcxx_check_sorted(_Base::begin(), _Base::end());
786 __glibcxx_check_sorted(__list._M_base().begin(),
787 __list._M_base().end());
afe96d41 788 this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it)
b8b4301e 789 {
afe96d41
FD
790 return __it != __list._M_base().cbefore_begin()
791 && __it != __list._M_base().cend();
792 });
b8b4301e
PC
793 _Base::merge(std::move(__list._M_base()));
794 }
795 }
796
78263296
PC
797 void
798 merge(forward_list& __list)
799 { merge(std::move(__list)); }
800
b8b4301e 801 template<typename _Comp>
15ee1a77
FD
802 void
803 merge(forward_list&& __list, _Comp __comp)
b8b4301e 804 {
9649e5b6 805 if (this != std::__addressof(__list))
b8b4301e
PC
806 {
807 __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(), __comp);
808 __glibcxx_check_sorted_pred(__list._M_base().begin(),
809 __list._M_base().end(), __comp);
afe96d41
FD
810 this->_M_transfer_from_if(__list,
811 [&__list](_Base_const_iterator __it)
b8b4301e 812 {
15ee1a77 813 return __it != __list._M_base().cbefore_begin()
afe96d41
FD
814 && __it != __list._M_base().cend();
815 });
b8b4301e
PC
816 _Base::merge(std::move(__list._M_base()), __comp);
817 }
818 }
819
78263296 820 template<typename _Comp>
15ee1a77
FD
821 void
822 merge(forward_list& __list, _Comp __comp)
823 { merge(std::move(__list), __comp); }
78263296 824
b8b4301e
PC
825 using _Base::sort;
826 using _Base::reverse;
827
828 _Base&
15ee1a77 829 _M_base() noexcept { return *this; }
b8b4301e
PC
830
831 const _Base&
d3677132 832 _M_base() const noexcept { return *this; }
b8b4301e
PC
833 };
834
957f5fea
VV
835#if __cpp_deduction_guides >= 201606
836 template<typename _InputIterator, typename _ValT
837 = typename iterator_traits<_InputIterator>::value_type,
838 typename _Allocator = allocator<_ValT>,
839 typename = _RequireInputIter<_InputIterator>,
840 typename = _RequireAllocator<_Allocator>>
841 forward_list(_InputIterator, _InputIterator, _Allocator = _Allocator())
842 -> forward_list<_ValT, _Allocator>;
843#endif
844
b8b4301e
PC
845 template<typename _Tp, typename _Alloc>
846 bool
847 operator==(const forward_list<_Tp, _Alloc>& __lx,
15ee1a77 848 const forward_list<_Tp, _Alloc>& __ly)
b8b4301e
PC
849 { return __lx._M_base() == __ly._M_base(); }
850
bd2420f8
JW
851#if __cpp_lib_three_way_comparison
852 template<typename _Tp, typename _Alloc>
853 constexpr __detail::__synth3way_t<_Tp>
854 operator<=>(const forward_list<_Tp, _Alloc>& __x,
855 const forward_list<_Tp, _Alloc>& __y)
856 { return __x._M_base() <=> __y._M_base(); }
857#else
b8b4301e
PC
858 template<typename _Tp, typename _Alloc>
859 inline bool
860 operator<(const forward_list<_Tp, _Alloc>& __lx,
15ee1a77 861 const forward_list<_Tp, _Alloc>& __ly)
b8b4301e
PC
862 { return __lx._M_base() < __ly._M_base(); }
863
864 template<typename _Tp, typename _Alloc>
865 inline bool
866 operator!=(const forward_list<_Tp, _Alloc>& __lx,
15ee1a77 867 const forward_list<_Tp, _Alloc>& __ly)
b8b4301e
PC
868 { return !(__lx == __ly); }
869
870 /// Based on operator<
871 template<typename _Tp, typename _Alloc>
872 inline bool
873 operator>(const forward_list<_Tp, _Alloc>& __lx,
15ee1a77 874 const forward_list<_Tp, _Alloc>& __ly)
b8b4301e
PC
875 { return (__ly < __lx); }
876
877 /// Based on operator<
878 template<typename _Tp, typename _Alloc>
879 inline bool
880 operator>=(const forward_list<_Tp, _Alloc>& __lx,
15ee1a77 881 const forward_list<_Tp, _Alloc>& __ly)
b8b4301e
PC
882 { return !(__lx < __ly); }
883
884 /// Based on operator<
885 template<typename _Tp, typename _Alloc>
886 inline bool
887 operator<=(const forward_list<_Tp, _Alloc>& __lx,
15ee1a77 888 const forward_list<_Tp, _Alloc>& __ly)
b8b4301e 889 { return !(__ly < __lx); }
bd2420f8 890#endif // three-way comparison
b8b4301e
PC
891
892 /// See std::forward_list::swap().
893 template<typename _Tp, typename _Alloc>
894 inline void
c5d9ec56
JW
895 swap(forward_list<_Tp, _Alloc>& __lx, forward_list<_Tp, _Alloc>& __ly)
896 noexcept(noexcept(__lx.swap(__ly)))
b8b4301e
PC
897 { __lx.swap(__ly); }
898
899} // namespace __debug
900} // namespace std
901
902namespace __gnu_debug
903{
24167c42 904 template<typename _Tp, typename _Alloc>
b8b4301e
PC
905 struct _BeforeBeginHelper<std::__debug::forward_list<_Tp, _Alloc> >
906 {
afe96d41 907 typedef std::__debug::forward_list<_Tp, _Alloc> _Sequence;
b8b4301e 908
9fc0e24b
FD
909 template<typename _Iterator>
910 static bool
911 _S_Is(const _Safe_iterator<_Iterator, _Sequence>& __it)
912 {
913 return
914 __it.base() == __it._M_get_sequence()->_M_base().before_begin();
915 }
079c74f3 916
9fc0e24b
FD
917 template<typename _Iterator>
918 static bool
919 _S_Is_Beginnest(const _Safe_iterator<_Iterator, _Sequence>& __it)
920 { return _S_Is(__it); }
b8b4301e 921 };
5720787a 922
24167c42
FD
923 template<typename _Tp, typename _Alloc>
924 struct _Sequence_traits<std::__debug::forward_list<_Tp, _Alloc> >
925 {
926 typedef typename std::__debug::forward_list<_Tp, _Alloc>::iterator _It;
927
928 static typename _Distance_traits<_It>::__type
929 _S_size(const std::__debug::forward_list<_Tp, _Alloc>& __seq)
930 {
931 return __seq.empty()
36edf9ca 932 ? std::make_pair(0, __dp_exact) : std::make_pair(1, __dp_sign);
24167c42
FD
933 }
934 };
935
5720787a
FD
936#ifndef _GLIBCXX_DEBUG_PEDANTIC
937 template<class _Tp, class _Alloc>
938 struct _Insert_range_from_self_is_safe<
939 std::__debug::forward_list<_Tp, _Alloc> >
9fc0e24b 940 { enum { __value = 1 }; };
5720787a 941#endif
b8b4301e
PC
942}
943
944#endif