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 
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  _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;
55  _Safe_container(_Safe_container&&) = default;
56 
57  private:
58  _GLIBCXX20_CONSTEXPR
59  _Safe_container(_Safe_container&& __x, const _Alloc&, std::true_type)
61  { }
62 
63  _GLIBCXX20_CONSTEXPR
64  _Safe_container(_Safe_container&& __x, const _Alloc& __a, std::false_type)
65  : _Safe_container()
66  {
67  if (__x._M_cont().get_allocator() == __a)
68  _Base::_M_swap(__x);
69  else if (!std::__is_constant_evaluated())
70  __x._M_invalidate_all();
71  }
72 
73  protected:
74  _GLIBCXX20_CONSTEXPR
75  _Safe_container(_Safe_container&& __x, const _Alloc& __a)
76  : _Safe_container(std::move(__x), __a,
78  { }
79 #endif
80 
81  // Copy assignment invalidate all iterators.
82  _GLIBCXX20_CONSTEXPR
84  operator=(const _Safe_container&) _GLIBCXX_NOEXCEPT
85  {
86  if (!std::__is_constant_evaluated())
87  this->_M_invalidate_all();
88  return *this;
89  }
90 
91 #if __cplusplus >= 201103L
92  _GLIBCXX20_CONSTEXPR
94  operator=(_Safe_container&& __x) noexcept
95  {
96  if (std::__is_constant_evaluated())
97  return *this;
98 
99  if (std::__addressof(__x) == this)
100  {
101  // Standard containers have a valid but unspecified value after
102  // self-move, so we invalidate all debug iterators even if the
103  // underlying container happens to preserve its contents.
104  this->_M_invalidate_all();
105  return *this;
106  }
107 
108  if (_IsCxx11AllocatorAware)
109  {
110  typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
111 
112  bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
113  || _M_cont().get_allocator() == __x._M_cont().get_allocator();
114  if (__xfer_memory)
115  _Base::_M_swap(__x);
116  else
117  this->_M_invalidate_all();
118  }
119  else
120  _Base::_M_swap(__x);
121 
122  __x._M_invalidate_all();
123  return *this;
124  }
125 
126  _GLIBCXX20_CONSTEXPR
127  void
128  _M_swap(_Safe_container& __x) noexcept
129  {
130  if (_IsCxx11AllocatorAware)
131  {
132  typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
133 
134  if (!_Alloc_traits::_S_propagate_on_swap())
135  __glibcxx_check_equal_allocs(this->_M_cont()._M_base(),
136  __x._M_cont()._M_base());
137  }
138 
139  _Base::_M_swap(__x);
140  }
141 #endif
142  };
143 
144 } // namespace __gnu_debug
145 
146 #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 _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:51
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:126
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.