libstdc++
safe_container.h
Go to the documentation of this file.
1// Safe container implementation -*- C++ -*-
2
3// Copyright (C) 2014-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_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>
41 class _Safe_container
42 : public _SafeBase<_SafeContainer>
43 {
44 typedef _SafeBase<_SafeContainer> _Base;
45
46 _GLIBCXX20_CONSTEXPR
47 const _SafeContainer&
48 _M_cont() const _GLIBCXX_NOEXCEPT
49 { return *static_cast<const _SafeContainer*>(this); }
50
51 protected:
52#if __cplusplus >= 201103L
53 _Safe_container() = default;
54 _Safe_container(const _Safe_container&) = default;
55 _Safe_container(_Safe_container&&) = default;
56
57 private:
58 _GLIBCXX20_CONSTEXPR
59 void
60 _M_swap_base(const _Safe_container& __x) const noexcept
61 { _Base::_M_swap(__x); }
62
63 _GLIBCXX20_CONSTEXPR
64 _Safe_container(_Safe_container&& __x, const _Alloc&, std::true_type)
65 : _Safe_container(std::move(__x))
66 { }
67
68 _GLIBCXX20_CONSTEXPR
69 _Safe_container(_Safe_container&& __x, const _Alloc& __a, std::false_type)
70 : _Safe_container()
71 {
72 if (!std::__is_constant_evaluated())
73 {
74 if (__x._M_cont().get_allocator() == __a)
75 _M_swap_base(__x);
76 else
77 __x._M_invalidate_all();
78 }
79 }
80
81 protected:
82 _GLIBCXX20_CONSTEXPR
83 _Safe_container(_Safe_container&& __x, const _Alloc& __a)
84 : _Safe_container(std::move(__x), __a,
86 { }
87#endif
88
89 // Copy assignment invalidate all iterators.
90 _GLIBCXX20_CONSTEXPR
91 _Safe_container&
92 operator=(const _Safe_container&) _GLIBCXX_NOEXCEPT
93 {
94 if (!std::__is_constant_evaluated())
95 this->_M_invalidate_all();
96 return *this;
97 }
98
99#if __cplusplus >= 201103L
100 _GLIBCXX20_CONSTEXPR
101 _Safe_container&
102 operator=(_Safe_container&& __x) noexcept
103 {
104 if (std::__is_constant_evaluated())
105 return *this;
106
107 if (std::__addressof(__x) == this)
108 {
109 // Standard containers have a valid but unspecified value after
110 // self-move, so we invalidate all debug iterators even if the
111 // underlying container happens to preserve its contents.
112 this->_M_invalidate_all();
113 return *this;
114 }
115
116 if (_IsCxx11AllocatorAware)
117 {
118 typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
119
120 bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
121 || _M_cont().get_allocator() == __x._M_cont().get_allocator();
122 if (__xfer_memory)
123 _M_swap_base(__x);
124 else
125 this->_M_invalidate_all();
126 }
127 else
128 _M_swap_base(__x);
129
130 __x._M_invalidate_all();
131 return *this;
132 }
133
134 _GLIBCXX20_CONSTEXPR
135 void
136 _M_swap(const _Safe_container& __x) const noexcept
137 {
138 if (_IsCxx11AllocatorAware)
139 {
140 typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
141
142 if (!_Alloc_traits::_S_propagate_on_swap())
143 __glibcxx_check_equal_allocs(this->_M_cont()._M_base(),
144 __x._M_cont()._M_base());
145 }
146
147 _M_swap_base(__x);
148 }
149#else
150 void
151 _M_swap(const _Safe_container& __x) const throw()
152 { _Base::_M_swap(__x); }
153#endif
154 };
155
156} // namespace __gnu_debug
157
158#endif
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:116
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:119
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
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.
Uniform interface to C++98 and C++11 allocators.