2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_EXPERIMENTAL___MEMORY
11 #define _LIBCPP_EXPERIMENTAL___MEMORY
13 #include <__memory/allocator_arg_t.h>
14 #include <__memory/uses_allocator.h>
15 #include <__type_traits/conditional.h>
16 #include <__type_traits/is_constructible.h>
17 #include <__type_traits/is_convertible.h>
18 #include <__type_traits/is_same.h>
19 #include <experimental/__config>
20 #include <experimental/utility> // for erased_type
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
26 _LIBCPP_BEGIN_NAMESPACE_LFTS
29 class _Tp, class _Alloc
30 , bool = uses_allocator<_Tp, _Alloc>::value
31 , bool = __has_allocator_type<_Tp>::value
33 struct __lfts_uses_allocator : public false_type {};
35 template <class _Tp, class _Alloc>
36 struct __lfts_uses_allocator<_Tp, _Alloc, false, false> : public false_type {};
38 template <class _Tp, class _Alloc, bool HasAlloc>
39 struct __lfts_uses_allocator<_Tp, _Alloc, true, HasAlloc> : public true_type {};
41 template <class _Tp, class _Alloc>
42 struct __lfts_uses_allocator<_Tp, _Alloc, false, true>
43 : public integral_constant<bool
44 , is_convertible<_Alloc, typename _Tp::allocator_type>::value
45 || is_same<erased_type, typename _Tp::allocator_type>::value
49 template <bool _UsesAlloc, class _Tp, class _Alloc, class ..._Args>
50 struct __lfts_uses_alloc_ctor_imp
52 static const int value = 0;
55 template <class _Tp, class _Alloc, class ..._Args>
56 struct __lfts_uses_alloc_ctor_imp<true, _Tp, _Alloc, _Args...>
58 static const bool __ic_first
59 = is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
61 static const bool __ic_second =
65 is_constructible<_Tp, _Args..., _Alloc>
68 static_assert(__ic_first || __ic_second,
69 "Request for uses allocator construction is ill-formed");
71 static const int value = __ic_first ? 1 : 2;
74 template <class _Tp, class _Alloc, class ..._Args>
75 struct __lfts_uses_alloc_ctor
76 : integral_constant<int,
77 __lfts_uses_alloc_ctor_imp<
78 __lfts_uses_allocator<_Tp, _Alloc>::value
79 , _Tp, _Alloc, _Args...
84 template <class _Tp, class _Allocator, class... _Args>
85 inline _LIBCPP_INLINE_VISIBILITY
86 void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
88 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
91 // FIXME: This should have a version which takes a non-const alloc.
92 template <class _Tp, class _Allocator, class... _Args>
93 inline _LIBCPP_INLINE_VISIBILITY
94 void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
96 new (__storage) _Tp (allocator_arg_t(), __a, _VSTD::forward<_Args>(__args)...);
99 // FIXME: This should have a version which takes a non-const alloc.
100 template <class _Tp, class _Allocator, class... _Args>
101 inline _LIBCPP_INLINE_VISIBILITY
102 void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
104 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
107 template <class _Tp, class _Alloc, class ..._Args>
108 inline _LIBCPP_INLINE_VISIBILITY
109 void __lfts_user_alloc_construct(
110 _Tp * __store, const _Alloc & __a, _Args &&... __args)
112 ::std::experimental::fundamentals_v1::__user_alloc_construct_impl(
113 typename __lfts_uses_alloc_ctor<_Tp, _Alloc, _Args...>::type()
114 , __store, __a, _VSTD::forward<_Args>(__args)...
118 _LIBCPP_END_NAMESPACE_LFTS
120 #endif /* _LIBCPP_EXPERIMENTAL___MEMORY */