uses_allocator.h 6.2 KB

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