uses_allocator.h 6.7 KB

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