uses_allocator.h 6.7 KB

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