uses_allocator.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Uses-allocator Construction -*- C++ -*-
  2. // Copyright (C) 2010-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. #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 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. inline __uses_alloc_t<_Tp, _Alloc, _Args...>
  89. __use_alloc(const _Alloc& __a)
  90. {
  91. __uses_alloc_t<_Tp, _Alloc, _Args...> __ret;
  92. __ret._M_a = std::__addressof(__a);
  93. return __ret;
  94. }
  95. template<typename _Tp, typename _Alloc, typename... _Args>
  96. void
  97. __use_alloc(const _Alloc&&) = delete;
  98. #if __cplusplus > 201402L
  99. template <typename _Tp, typename _Alloc>
  100. inline constexpr bool uses_allocator_v =
  101. uses_allocator<_Tp, _Alloc>::value;
  102. #endif // C++17
  103. template<template<typename...> class _Predicate,
  104. typename _Tp, typename _Alloc, typename... _Args>
  105. struct __is_uses_allocator_predicate
  106. : conditional<uses_allocator<_Tp, _Alloc>::value,
  107. __or_<_Predicate<_Tp, allocator_arg_t, _Alloc, _Args...>,
  108. _Predicate<_Tp, _Args..., _Alloc>>,
  109. _Predicate<_Tp, _Args...>>::type { };
  110. template<typename _Tp, typename _Alloc, typename... _Args>
  111. struct __is_uses_allocator_constructible
  112. : __is_uses_allocator_predicate<is_constructible, _Tp, _Alloc, _Args...>
  113. { };
  114. #if __cplusplus >= 201402L
  115. template<typename _Tp, typename _Alloc, typename... _Args>
  116. _GLIBCXX17_INLINE constexpr bool __is_uses_allocator_constructible_v =
  117. __is_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value;
  118. #endif // C++14
  119. template<typename _Tp, typename _Alloc, typename... _Args>
  120. struct __is_nothrow_uses_allocator_constructible
  121. : __is_uses_allocator_predicate<is_nothrow_constructible,
  122. _Tp, _Alloc, _Args...>
  123. { };
  124. #if __cplusplus >= 201402L
  125. template<typename _Tp, typename _Alloc, typename... _Args>
  126. _GLIBCXX17_INLINE constexpr bool
  127. __is_nothrow_uses_allocator_constructible_v =
  128. __is_nothrow_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value;
  129. #endif // C++14
  130. template<typename _Tp, typename... _Args>
  131. void __uses_allocator_construct_impl(__uses_alloc0 __a, _Tp* __ptr,
  132. _Args&&... __args)
  133. { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)...); }
  134. template<typename _Tp, typename _Alloc, typename... _Args>
  135. void __uses_allocator_construct_impl(__uses_alloc1<_Alloc> __a, _Tp* __ptr,
  136. _Args&&... __args)
  137. {
  138. ::new ((void*)__ptr) _Tp(allocator_arg, *__a._M_a,
  139. std::forward<_Args>(__args)...);
  140. }
  141. template<typename _Tp, typename _Alloc, typename... _Args>
  142. void __uses_allocator_construct_impl(__uses_alloc2<_Alloc> __a, _Tp* __ptr,
  143. _Args&&... __args)
  144. { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)..., *__a._M_a); }
  145. template<typename _Tp, typename _Alloc, typename... _Args>
  146. void __uses_allocator_construct(const _Alloc& __a, _Tp* __ptr,
  147. _Args&&... __args)
  148. {
  149. std::__uses_allocator_construct_impl(
  150. std::__use_alloc<_Tp, _Alloc, _Args...>(__a), __ptr,
  151. std::forward<_Args>(__args)...);
  152. }
  153. _GLIBCXX_END_NAMESPACE_VERSION
  154. } // namespace std
  155. #endif
  156. #endif