[1166] | 1 | // Safe container implementation -*- C++ -*-
|
---|
| 2 |
|
---|
| 3 | // Copyright (C) 2014-2021 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>
|
---|
| 41 | class _Safe_container
|
---|
| 42 | : public _SafeBase<_SafeContainer>
|
---|
| 43 | {
|
---|
| 44 | typedef _SafeBase<_SafeContainer> _Base;
|
---|
| 45 |
|
---|
| 46 | _SafeContainer&
|
---|
| 47 | _M_cont() _GLIBCXX_NOEXCEPT
|
---|
| 48 | { return *static_cast<_SafeContainer*>(this); }
|
---|
| 49 |
|
---|
| 50 | protected:
|
---|
| 51 | _Safe_container&
|
---|
| 52 | _M_safe() _GLIBCXX_NOEXCEPT
|
---|
| 53 | { return *this; }
|
---|
| 54 |
|
---|
| 55 | #if __cplusplus >= 201103L
|
---|
| 56 | _Safe_container() = default;
|
---|
| 57 | _Safe_container(const _Safe_container&) = default;
|
---|
| 58 | _Safe_container(_Safe_container&&) = default;
|
---|
| 59 |
|
---|
| 60 | _Safe_container(_Safe_container&& __x, const _Alloc& __a)
|
---|
| 61 | : _Safe_container()
|
---|
| 62 | {
|
---|
| 63 | if (__x._M_cont().get_allocator() == __a)
|
---|
| 64 | _Base::_M_swap(__x);
|
---|
| 65 | else
|
---|
| 66 | __x._M_invalidate_all();
|
---|
| 67 | }
|
---|
| 68 | #endif
|
---|
| 69 |
|
---|
| 70 | public:
|
---|
| 71 | // Copy assignment invalidate all iterators.
|
---|
| 72 | _Safe_container&
|
---|
| 73 | operator=(const _Safe_container&) _GLIBCXX_NOEXCEPT
|
---|
| 74 | {
|
---|
| 75 | this->_M_invalidate_all();
|
---|
| 76 | return *this;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | #if __cplusplus >= 201103L
|
---|
| 80 | _Safe_container&
|
---|
| 81 | operator=(_Safe_container&& __x) noexcept
|
---|
| 82 | {
|
---|
| 83 | if (std::__addressof(__x) == this)
|
---|
| 84 | {
|
---|
| 85 | // Standard containers have a valid but unspecified value after
|
---|
| 86 | // self-move, so we invalidate all debug iterators even if the
|
---|
| 87 | // underlying container happens to preserve its contents.
|
---|
| 88 | this->_M_invalidate_all();
|
---|
| 89 | return *this;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | if (_IsCxx11AllocatorAware)
|
---|
| 93 | {
|
---|
| 94 | typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
|
---|
| 95 |
|
---|
| 96 | bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
|
---|
| 97 | || _M_cont().get_allocator() == __x._M_cont().get_allocator();
|
---|
| 98 | if (__xfer_memory)
|
---|
| 99 | _Base::_M_swap(__x);
|
---|
| 100 | else
|
---|
| 101 | this->_M_invalidate_all();
|
---|
| 102 | }
|
---|
| 103 | else
|
---|
| 104 | _Base::_M_swap(__x);
|
---|
| 105 |
|
---|
| 106 | __x._M_invalidate_all();
|
---|
| 107 | return *this;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void
|
---|
| 111 | _M_swap(_Safe_container& __x) noexcept
|
---|
| 112 | {
|
---|
| 113 | if (_IsCxx11AllocatorAware)
|
---|
| 114 | {
|
---|
| 115 | typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
|
---|
| 116 |
|
---|
| 117 | if (!_Alloc_traits::_S_propagate_on_swap())
|
---|
| 118 | __glibcxx_check_equal_allocs(this->_M_cont()._M_base(),
|
---|
| 119 | __x._M_cont()._M_base());
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | _Base::_M_swap(__x);
|
---|
| 123 | }
|
---|
| 124 | #endif
|
---|
| 125 | };
|
---|
| 126 |
|
---|
| 127 | } // namespace __gnu_debug
|
---|
| 128 |
|
---|
| 129 | #endif
|
---|