libstdc++
safe_container.h
Go to the documentation of this file.
1// Safe container implementation -*- C++ -*-
2
3// Copyright (C) 2014-2024 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_container.h
26 * This file is a GNU debug extension to the Standard C++ Library.
27 */
28
29#ifndef _GLIBCXX_DEBUG_SAFE_CONTAINER_H
30#define _GLIBCXX_DEBUG_SAFE_CONTAINER_H 1
31
32#include <ext/alloc_traits.h>
33
34namespace __gnu_debug
35{
36 /// Safe class dealing with some allocator dependent operations.
37 template<typename _SafeContainer,
38 typename _Alloc,
39 template<typename> class _SafeBase,
40 bool _IsCxx11AllocatorAware = true>
42 : public _SafeBase<_SafeContainer>
43 {
44 typedef _SafeBase<_SafeContainer> _Base;
45
46 _GLIBCXX20_CONSTEXPR
47 _SafeContainer&
48 _M_cont() _GLIBCXX_NOEXCEPT
49 { return *static_cast<_SafeContainer*>(this); }
50
51 protected:
52#if __cplusplus >= 201103L
53 _Safe_container() = default;
54 _Safe_container(const _Safe_container&) = default;
56
57 private:
58 _GLIBCXX20_CONSTEXPR
61 { }
62
63 _GLIBCXX20_CONSTEXPR
64 _Safe_container(_Safe_container&& __x, const _Alloc& __a, std::false_type)
66 {
67 if (!std::__is_constant_evaluated())
68 {
69 if (__x._M_cont().get_allocator() == __a)
70 _Base::_M_swap(__x);
71 else
72 __x._M_invalidate_all();
73 }
74 }
75
76 protected:
77 _GLIBCXX20_CONSTEXPR
78 _Safe_container(_Safe_container&& __x, const _Alloc& __a)
79 : _Safe_container(std::move(__x), __a,
81 { }
82#endif
83
84 // Copy assignment invalidate all iterators.
85 _GLIBCXX20_CONSTEXPR
87 operator=(const _Safe_container&) _GLIBCXX_NOEXCEPT
88 {
89 if (!std::__is_constant_evaluated())
90 this->_M_invalidate_all();
91 return *this;
92 }
93
94#if __cplusplus >= 201103L
95 _GLIBCXX20_CONSTEXPR
97 operator=(_Safe_container&& __x) noexcept
98 {
99 if (std::__is_constant_evaluated())
100 return *this;
101
102 if (std::__addressof(__x) == this)
103 {
104 // Standard containers have a valid but unspecified value after
105 // self-move, so we invalidate all debug iterators even if the
106 // underlying container happens to preserve its contents.
107 this->_M_invalidate_all();
108 return *this;
109 }
110
111 if (_IsCxx11AllocatorAware)
112 {
113 typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
114
115 bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
116 || _M_cont().get_allocator() == __x._M_cont().get_allocator();
117 if (__xfer_memory)
118 _Base::_M_swap(__x);
119 else
120 this->_M_invalidate_all();
121 }
122 else
123 _Base::_M_swap(__x);
124
125 __x._M_invalidate_all();
126 return *this;
127 }
128
129 _GLIBCXX20_CONSTEXPR
130 void
131 _M_swap(_Safe_container& __x) noexcept
132 {
133 if (_IsCxx11AllocatorAware)
134 {
135 typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
136
137 if (!_Alloc_traits::_S_propagate_on_swap())
138 __glibcxx_check_equal_allocs(this->_M_cont()._M_base(),
139 __x._M_cont()._M_base());
140 }
141
142 _Base::_M_swap(__x);
143 }
144#endif
145 };
146
147} // namespace __gnu_debug
148
149#endif
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:111
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:114
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:137
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
GNU debug classes for public use.
typename __detected_or_t< is_empty< _Alloc >, __equal, _Alloc >::type is_always_equal
Whether all instances of the allocator type compare equal.
Safe class dealing with some allocator dependent operations.
Uniform interface to C++98 and C++11 allocators.