1*38fd1498Szrj // Safe container implementation -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2014-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the 7*38fd1498Szrj // terms of the GNU General Public License as published by the 8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj // any later version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, 12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj // GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /** @file debug/safe_container.h 26*38fd1498Szrj * This file is a GNU debug extension to the Standard C++ Library. 27*38fd1498Szrj */ 28*38fd1498Szrj 29*38fd1498Szrj #ifndef _GLIBCXX_DEBUG_SAFE_CONTAINER_H 30*38fd1498Szrj #define _GLIBCXX_DEBUG_SAFE_CONTAINER_H 1 31*38fd1498Szrj 32*38fd1498Szrj #include <ext/alloc_traits.h> 33*38fd1498Szrj 34*38fd1498Szrj namespace __gnu_debug 35*38fd1498Szrj { 36*38fd1498Szrj /// Safe class dealing with some allocator dependent operations. 37*38fd1498Szrj template<typename _SafeContainer, 38*38fd1498Szrj typename _Alloc, 39*38fd1498Szrj template<typename> class _SafeBase, 40*38fd1498Szrj bool _IsCxx11AllocatorAware = true> 41*38fd1498Szrj class _Safe_container 42*38fd1498Szrj : public _SafeBase<_SafeContainer> 43*38fd1498Szrj { 44*38fd1498Szrj typedef _SafeBase<_SafeContainer> _Base; 45*38fd1498Szrj 46*38fd1498Szrj _SafeContainer& _M_cont()47*38fd1498Szrj _M_cont() _GLIBCXX_NOEXCEPT 48*38fd1498Szrj { return *static_cast<_SafeContainer*>(this); } 49*38fd1498Szrj 50*38fd1498Szrj protected: 51*38fd1498Szrj _Safe_container& _M_safe()52*38fd1498Szrj _M_safe() _GLIBCXX_NOEXCEPT 53*38fd1498Szrj { return *this; } 54*38fd1498Szrj 55*38fd1498Szrj #if __cplusplus >= 201103L 56*38fd1498Szrj _Safe_container() = default; 57*38fd1498Szrj _Safe_container(const _Safe_container&) = default; 58*38fd1498Szrj _Safe_container(_Safe_container&&) = default; 59*38fd1498Szrj _Safe_container(_Safe_container && __x,const _Alloc & __a)60*38fd1498Szrj _Safe_container(_Safe_container&& __x, const _Alloc& __a) 61*38fd1498Szrj : _Safe_container() 62*38fd1498Szrj { 63*38fd1498Szrj if (__x._M_cont().get_allocator() == __a) 64*38fd1498Szrj _Base::_M_swap(__x); 65*38fd1498Szrj else 66*38fd1498Szrj __x._M_invalidate_all(); 67*38fd1498Szrj } 68*38fd1498Szrj #endif 69*38fd1498Szrj 70*38fd1498Szrj public: 71*38fd1498Szrj // Copy assignment invalidate all iterators. 72*38fd1498Szrj _Safe_container& 73*38fd1498Szrj operator=(const _Safe_container&) _GLIBCXX_NOEXCEPT 74*38fd1498Szrj { 75*38fd1498Szrj this->_M_invalidate_all(); 76*38fd1498Szrj return *this; 77*38fd1498Szrj } 78*38fd1498Szrj 79*38fd1498Szrj #if __cplusplus >= 201103L 80*38fd1498Szrj _Safe_container& 81*38fd1498Szrj operator=(_Safe_container&& __x) noexcept 82*38fd1498Szrj { 83*38fd1498Szrj __glibcxx_check_self_move_assign(__x); 84*38fd1498Szrj 85*38fd1498Szrj if (_IsCxx11AllocatorAware) 86*38fd1498Szrj { 87*38fd1498Szrj typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits; 88*38fd1498Szrj 89*38fd1498Szrj bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign() 90*38fd1498Szrj || _M_cont().get_allocator() == __x._M_cont().get_allocator(); 91*38fd1498Szrj if (__xfer_memory) 92*38fd1498Szrj _Base::_M_swap(__x); 93*38fd1498Szrj else 94*38fd1498Szrj this->_M_invalidate_all(); 95*38fd1498Szrj } 96*38fd1498Szrj else 97*38fd1498Szrj _Base::_M_swap(__x); 98*38fd1498Szrj 99*38fd1498Szrj __x._M_invalidate_all(); 100*38fd1498Szrj return *this; 101*38fd1498Szrj } 102*38fd1498Szrj 103*38fd1498Szrj void _M_swap(_Safe_container & __x)104*38fd1498Szrj _M_swap(_Safe_container& __x) noexcept 105*38fd1498Szrj { 106*38fd1498Szrj if (_IsCxx11AllocatorAware) 107*38fd1498Szrj { 108*38fd1498Szrj typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits; 109*38fd1498Szrj 110*38fd1498Szrj if (!_Alloc_traits::_S_propagate_on_swap()) 111*38fd1498Szrj __glibcxx_check_equal_allocs(this->_M_cont()._M_base(), 112*38fd1498Szrj __x._M_cont()._M_base()); 113*38fd1498Szrj } 114*38fd1498Szrj 115*38fd1498Szrj _Base::_M_swap(__x); 116*38fd1498Szrj } 117*38fd1498Szrj #endif 118*38fd1498Szrj }; 119*38fd1498Szrj 120*38fd1498Szrj } // namespace __gnu_debug 121*38fd1498Szrj 122*38fd1498Szrj #endif 123