uses_allocator.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Uses-allocator Construction -*- C++ -*-
  2. // Copyright (C) 2010-2023 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 include/bits/uses_allocator_args.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory}
  23. */
  24. #ifndef _USES_ALLOCATOR_H
  25. #define _USES_ALLOCATOR_H 1
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <type_traits>
  30. #include <bits/move.h>
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /// @cond undocumented
  35. // This is used for std::experimental::erased_type from Library Fundamentals.
  36. struct __erased_type { };
  37. // This also supports the "type-erased allocator" protocol from the
  38. // Library Fundamentals TS, where allocator_type is erased_type.
  39. // The second condition will always be false for types not using the TS.
  40. template<typename _Alloc, typename _Tp>
  41. using __is_erased_or_convertible
  42. = __or_<is_convertible<_Alloc, _Tp>, is_same<_Tp, __erased_type>>;
  43. /// [allocator.tag]
  44. struct allocator_arg_t { explicit allocator_arg_t() = default; };
  45. _GLIBCXX17_INLINE constexpr allocator_arg_t allocator_arg =
  46. allocator_arg_t();
  47. template<typename _Tp, typename _Alloc, typename = __void_t<>>
  48. struct __uses_allocator_helper
  49. : false_type { };
  50. template<typename _Tp, typename _Alloc>
  51. struct __uses_allocator_helper<_Tp, _Alloc,
  52. __void_t<typename _Tp::allocator_type>>
  53. : __is_erased_or_convertible<_Alloc, typename _Tp::allocator_type>::type
  54. { };
  55. /// [allocator.uses.trait]
  56. template<typename _Tp, typename _Alloc>
  57. struct uses_allocator
  58. : __uses_allocator_helper<_Tp, _Alloc>::type
  59. { };
  60. struct __uses_alloc_base { };
  61. struct __uses_alloc0 : __uses_alloc_base
  62. {
  63. struct _Sink { void _GLIBCXX20_CONSTEXPR operator=(const void*) { } } _M_a;
  64. };
  65. template<typename _Alloc>
  66. struct __uses_alloc1 : __uses_alloc_base { const _Alloc* _M_a; };
  67. template<typename _Alloc>
  68. struct __uses_alloc2 : __uses_alloc_base { const _Alloc* _M_a; };
  69. template<bool, typename _Tp, typename _Alloc, typename... _Args>
  70. struct __uses_alloc;
  71. template<typename _Tp, typename _Alloc, typename... _Args>
  72. struct __uses_alloc<true, _Tp, _Alloc, _Args...>
  73. : __conditional_t<
  74. is_constructible<_Tp, allocator_arg_t, const _Alloc&, _Args...>::value,
  75. __uses_alloc1<_Alloc>,
  76. __uses_alloc2<_Alloc>>
  77. {
  78. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  79. // 2586. Wrong value category used in scoped_allocator_adaptor::construct
  80. static_assert(__or_<
  81. is_constructible<_Tp, allocator_arg_t, const _Alloc&, _Args...>,
  82. is_constructible<_Tp, _Args..., const _Alloc&>>::value,
  83. "construction with an allocator must be possible"
  84. " if uses_allocator is true");
  85. };
  86. template<typename _Tp, typename _Alloc, typename... _Args>
  87. struct __uses_alloc<false, _Tp, _Alloc, _Args...>
  88. : __uses_alloc0 { };
  89. template<typename _Tp, typename _Alloc, typename... _Args>
  90. using __uses_alloc_t =
  91. __uses_alloc<uses_allocator<_Tp, _Alloc>::value, _Tp, _Alloc, _Args...>;
  92. template<typename _Tp, typename _Alloc, typename... _Args>
  93. _GLIBCXX20_CONSTEXPR
  94. inline __uses_alloc_t<_Tp, _Alloc, _Args...>
  95. __use_alloc(const _Alloc& __a)
  96. {
  97. __uses_alloc_t<_Tp, _Alloc, _Args...> __ret;
  98. __ret._M_a = std::__addressof(__a);
  99. return __ret;
  100. }
  101. template<typename _Tp, typename _Alloc, typename... _Args>
  102. void
  103. __use_alloc(const _Alloc&&) = delete;
  104. #if __cplusplus > 201402L
  105. template <typename _Tp, typename _Alloc>
  106. inline constexpr bool uses_allocator_v =
  107. uses_allocator<_Tp, _Alloc>::value;
  108. #endif // C++17
  109. template<template<typename...> class _Predicate,
  110. typename _Tp, typename _Alloc, typename... _Args>
  111. struct __is_uses_allocator_predicate
  112. : __conditional_t<uses_allocator<_Tp, _Alloc>::value,
  113. __or_<_Predicate<_Tp, allocator_arg_t, _Alloc, _Args...>,
  114. _Predicate<_Tp, _Args..., _Alloc>>,
  115. _Predicate<_Tp, _Args...>> { };
  116. template<typename _Tp, typename _Alloc, typename... _Args>
  117. struct __is_uses_allocator_constructible
  118. : __is_uses_allocator_predicate<is_constructible, _Tp, _Alloc, _Args...>
  119. { };
  120. #if __cplusplus >= 201402L
  121. template<typename _Tp, typename _Alloc, typename... _Args>
  122. _GLIBCXX17_INLINE constexpr bool __is_uses_allocator_constructible_v =
  123. __is_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value;
  124. #endif // C++14
  125. template<typename _Tp, typename _Alloc, typename... _Args>
  126. struct __is_nothrow_uses_allocator_constructible
  127. : __is_uses_allocator_predicate<is_nothrow_constructible,
  128. _Tp, _Alloc, _Args...>
  129. { };
  130. #if __cplusplus >= 201402L
  131. template<typename _Tp, typename _Alloc, typename... _Args>
  132. _GLIBCXX17_INLINE constexpr bool
  133. __is_nothrow_uses_allocator_constructible_v =
  134. __is_nothrow_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value;
  135. #endif // C++14
  136. template<typename _Tp, typename... _Args>
  137. void __uses_allocator_construct_impl(__uses_alloc0, _Tp* __ptr,
  138. _Args&&... __args)
  139. { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)...); }
  140. template<typename _Tp, typename _Alloc, typename... _Args>
  141. void __uses_allocator_construct_impl(__uses_alloc1<_Alloc> __a, _Tp* __ptr,
  142. _Args&&... __args)
  143. {
  144. ::new ((void*)__ptr) _Tp(allocator_arg, *__a._M_a,
  145. std::forward<_Args>(__args)...);
  146. }
  147. template<typename _Tp, typename _Alloc, typename... _Args>
  148. void __uses_allocator_construct_impl(__uses_alloc2<_Alloc> __a, _Tp* __ptr,
  149. _Args&&... __args)
  150. { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)..., *__a._M_a); }
  151. template<typename _Tp, typename _Alloc, typename... _Args>
  152. void __uses_allocator_construct(const _Alloc& __a, _Tp* __ptr,
  153. _Args&&... __args)
  154. {
  155. std::__uses_allocator_construct_impl(
  156. std::__use_alloc<_Tp, _Alloc, _Args...>(__a), __ptr,
  157. std::forward<_Args>(__args)...);
  158. }
  159. /// @endcond
  160. _GLIBCXX_END_NAMESPACE_VERSION
  161. } // namespace std
  162. #endif
  163. #endif