libstdc++
safe_container.h
Go to the documentation of this file.
1 // Safe container implementation -*- C++ -*-
2 
3 // Copyright (C) 2014-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_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 
34 namespace __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  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)
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 #if __cplusplus < 201103L
91  operator=(const _Safe_container& __x)
92  {
93  _Base::operator=(__x);
94  return *this;
95  }
96 
97  void
98  _M_swap(const _Safe_container& __x) const throw()
99  { _Base::_M_swap(__x); }
100 #else
101  _GLIBCXX20_CONSTEXPR
103  operator=(const _Safe_container&) noexcept = default;
104 
105  _GLIBCXX20_CONSTEXPR
107  operator=(_Safe_container&& __x) noexcept
108  {
109  if (std::__is_constant_evaluated())
110  return *this;
111 
112  if (std::__addressof(__x) == this)
113  {
114  // Standard containers have a valid but unspecified value after
115  // self-move, so we invalidate all debug iterators even if the
116  // underlying container happens to preserve its contents.
117  this->_M_invalidate_all();
118  return *this;
119  }
120 
121  if (_IsCxx11AllocatorAware)
122  {
123  typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
124 
125  bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
126  || _M_cont().get_allocator() == __x._M_cont().get_allocator();
127  if (__xfer_memory)
128  _M_swap_base(__x);
129  else
130  this->_M_invalidate_all();
131  }
132  else
133  _M_swap_base(__x);
134 
135  __x._M_invalidate_all();
136  return *this;
137  }
138 
139  _GLIBCXX20_CONSTEXPR
140  void
141  _M_swap(const _Safe_container& __x) const noexcept
142  {
143  if (_IsCxx11AllocatorAware)
144  {
145  typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
146 
147  if (!_Alloc_traits::_S_propagate_on_swap())
148  __glibcxx_check_equal_allocs(this->_M_cont()._M_base(),
149  __x._M_cont()._M_base());
150  }
151 
152  _M_swap_base(__x);
153  }
154 #endif
155  };
156 
157 } // namespace __gnu_debug
158 
159 #endif
__gnu_debug::_Safe_container
Safe class dealing with some allocator dependent operations.
Definition: safe_container.h:41
std::allocator_traits::is_always_equal
typename __detected_or_t< is_empty< _Alloc >, __equal, _Alloc >::type is_always_equal
Whether all instances of the allocator type compare equal.
Definition: bits/alloc_traits.h:369
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
__gnu_debug
GNU debug classes for public use.
Definition: boost_concept_check.h:59
std::true_type
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:119
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:52
std::false_type
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:122
__gnu_cxx::__alloc_traits
Uniform interface to C++98 and C++11 allocators.
Definition: ext/alloc_traits.h:47
alloc_traits.h