safe_container.h 3.8 KB

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