alloc_traits.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Allocator traits -*- C++ -*-
  2. // Copyright (C) 2011-2019 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 ext/alloc_traits.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _EXT_ALLOC_TRAITS_H
  24. #define _EXT_ALLOC_TRAITS_H 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201103L
  27. # include <bits/move.h>
  28. # include <bits/alloc_traits.h>
  29. #else
  30. # include <bits/allocator.h> // for __alloc_swap
  31. #endif
  32. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @brief Uniform interface to C++98 and C++11 allocators.
  37. * @ingroup allocators
  38. */
  39. template<typename _Alloc, typename = typename _Alloc::value_type>
  40. struct __alloc_traits
  41. #if __cplusplus >= 201103L
  42. : std::allocator_traits<_Alloc>
  43. #endif
  44. {
  45. typedef _Alloc allocator_type;
  46. #if __cplusplus >= 201103L
  47. typedef std::allocator_traits<_Alloc> _Base_type;
  48. typedef typename _Base_type::value_type value_type;
  49. typedef typename _Base_type::pointer pointer;
  50. typedef typename _Base_type::const_pointer const_pointer;
  51. typedef typename _Base_type::size_type size_type;
  52. typedef typename _Base_type::difference_type difference_type;
  53. // C++11 allocators do not define reference or const_reference
  54. typedef value_type& reference;
  55. typedef const value_type& const_reference;
  56. using _Base_type::allocate;
  57. using _Base_type::deallocate;
  58. using _Base_type::construct;
  59. using _Base_type::destroy;
  60. using _Base_type::max_size;
  61. private:
  62. template<typename _Ptr>
  63. using __is_custom_pointer
  64. = std::__and_<std::is_same<pointer, _Ptr>,
  65. std::__not_<std::is_pointer<_Ptr>>>;
  66. public:
  67. // overload construct for non-standard pointer types
  68. template<typename _Ptr, typename... _Args>
  69. static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
  70. construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
  71. noexcept(noexcept(_Base_type::construct(__a, std::__to_address(__p),
  72. std::forward<_Args>(__args)...)))
  73. {
  74. _Base_type::construct(__a, std::__to_address(__p),
  75. std::forward<_Args>(__args)...);
  76. }
  77. // overload destroy for non-standard pointer types
  78. template<typename _Ptr>
  79. static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
  80. destroy(_Alloc& __a, _Ptr __p)
  81. noexcept(noexcept(_Base_type::destroy(__a, std::__to_address(__p))))
  82. { _Base_type::destroy(__a, std::__to_address(__p)); }
  83. static _Alloc _S_select_on_copy(const _Alloc& __a)
  84. { return _Base_type::select_on_container_copy_construction(__a); }
  85. static void _S_on_swap(_Alloc& __a, _Alloc& __b)
  86. { std::__alloc_on_swap(__a, __b); }
  87. static constexpr bool _S_propagate_on_copy_assign()
  88. { return _Base_type::propagate_on_container_copy_assignment::value; }
  89. static constexpr bool _S_propagate_on_move_assign()
  90. { return _Base_type::propagate_on_container_move_assignment::value; }
  91. static constexpr bool _S_propagate_on_swap()
  92. { return _Base_type::propagate_on_container_swap::value; }
  93. static constexpr bool _S_always_equal()
  94. { return _Base_type::is_always_equal::value; }
  95. static constexpr bool _S_nothrow_move()
  96. { return _S_propagate_on_move_assign() || _S_always_equal(); }
  97. template<typename _Tp>
  98. struct rebind
  99. { typedef typename _Base_type::template rebind_alloc<_Tp> other; };
  100. #else
  101. typedef typename _Alloc::pointer pointer;
  102. typedef typename _Alloc::const_pointer const_pointer;
  103. typedef typename _Alloc::value_type value_type;
  104. typedef typename _Alloc::reference reference;
  105. typedef typename _Alloc::const_reference const_reference;
  106. typedef typename _Alloc::size_type size_type;
  107. typedef typename _Alloc::difference_type difference_type;
  108. _GLIBCXX_NODISCARD static pointer
  109. allocate(_Alloc& __a, size_type __n)
  110. { return __a.allocate(__n); }
  111. static void deallocate(_Alloc& __a, pointer __p, size_type __n)
  112. { __a.deallocate(__p, __n); }
  113. template<typename _Tp>
  114. static void construct(_Alloc& __a, pointer __p, const _Tp& __arg)
  115. { __a.construct(__p, __arg); }
  116. static void destroy(_Alloc& __a, pointer __p)
  117. { __a.destroy(__p); }
  118. static size_type max_size(const _Alloc& __a)
  119. { return __a.max_size(); }
  120. static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; }
  121. static void _S_on_swap(_Alloc& __a, _Alloc& __b)
  122. {
  123. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  124. // 431. Swapping containers with unequal allocators.
  125. std::__alloc_swap<_Alloc>::_S_do_it(__a, __b);
  126. }
  127. template<typename _Tp>
  128. struct rebind
  129. { typedef typename _Alloc::template rebind<_Tp>::other other; };
  130. #endif
  131. };
  132. _GLIBCXX_END_NAMESPACE_VERSION
  133. } // namespace __gnu_cxx
  134. #endif