Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123 lines
3.3KB

  1. // Safe container implementation -*- C++ -*-
  2. // Copyright (C) 2014-2020 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. _Safe_container&
  42. _M_safe() _GLIBCXX_NOEXCEPT
  43. { return *this; }
  44. #if __cplusplus >= 201103L
  45. _Safe_container() = default;
  46. _Safe_container(const _Safe_container&) = default;
  47. _Safe_container(_Safe_container&&) = default;
  48. _Safe_container(_Safe_container&& __x, const _Alloc& __a)
  49. : _Safe_container()
  50. {
  51. if (__x._M_cont().get_allocator() == __a)
  52. _Base::_M_swap(__x);
  53. else
  54. __x._M_invalidate_all();
  55. }
  56. #endif
  57. public:
  58. // Copy assignment invalidate all iterators.
  59. _Safe_container&
  60. operator=(const _Safe_container&) _GLIBCXX_NOEXCEPT
  61. {
  62. this->_M_invalidate_all();
  63. return *this;
  64. }
  65. #if __cplusplus >= 201103L
  66. _Safe_container&
  67. operator=(_Safe_container&& __x) noexcept
  68. {
  69. __glibcxx_check_self_move_assign(__x);
  70. if (_IsCxx11AllocatorAware)
  71. {
  72. typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
  73. bool __xfer_memory = _Alloc_traits::_S_propagate_on_move_assign()
  74. || _M_cont().get_allocator() == __x._M_cont().get_allocator();
  75. if (__xfer_memory)
  76. _Base::_M_swap(__x);
  77. else
  78. this->_M_invalidate_all();
  79. }
  80. else
  81. _Base::_M_swap(__x);
  82. __x._M_invalidate_all();
  83. return *this;
  84. }
  85. void
  86. _M_swap(_Safe_container& __x) noexcept
  87. {
  88. if (_IsCxx11AllocatorAware)
  89. {
  90. typedef __gnu_cxx::__alloc_traits<_Alloc> _Alloc_traits;
  91. if (!_Alloc_traits::_S_propagate_on_swap())
  92. __glibcxx_check_equal_allocs(this->_M_cont()._M_base(),
  93. __x._M_cont()._M_base());
  94. }
  95. _Base::_M_swap(__x);
  96. }
  97. #endif
  98. };
  99. } // namespace __gnu_debug
  100. #endif