1*38fd1498Szrj // Uses-allocator Construction -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2010-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj #ifndef _USES_ALLOCATOR_H
26*38fd1498Szrj #define _USES_ALLOCATOR_H 1
27*38fd1498Szrj
28*38fd1498Szrj #if __cplusplus < 201103L
29*38fd1498Szrj # include <bits/c++0x_warning.h>
30*38fd1498Szrj #else
31*38fd1498Szrj
32*38fd1498Szrj #include <type_traits>
33*38fd1498Szrj #include <bits/move.h>
34*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)35*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
36*38fd1498Szrj {
37*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
38*38fd1498Szrj
39*38fd1498Szrj struct __erased_type { };
40*38fd1498Szrj
41*38fd1498Szrj template<typename _Alloc, typename _Tp>
42*38fd1498Szrj using __is_erased_or_convertible
43*38fd1498Szrj = __or_<is_same<_Tp, __erased_type>, is_convertible<_Alloc, _Tp>>;
44*38fd1498Szrj
45*38fd1498Szrj /// [allocator.tag]
46*38fd1498Szrj struct allocator_arg_t { explicit allocator_arg_t() = default; };
47*38fd1498Szrj
48*38fd1498Szrj _GLIBCXX17_INLINE constexpr allocator_arg_t allocator_arg =
49*38fd1498Szrj allocator_arg_t();
50*38fd1498Szrj
51*38fd1498Szrj template<typename _Tp, typename _Alloc, typename = __void_t<>>
52*38fd1498Szrj struct __uses_allocator_helper
53*38fd1498Szrj : false_type { };
54*38fd1498Szrj
55*38fd1498Szrj template<typename _Tp, typename _Alloc>
56*38fd1498Szrj struct __uses_allocator_helper<_Tp, _Alloc,
57*38fd1498Szrj __void_t<typename _Tp::allocator_type>>
58*38fd1498Szrj : __is_erased_or_convertible<_Alloc, typename _Tp::allocator_type>::type
59*38fd1498Szrj { };
60*38fd1498Szrj
61*38fd1498Szrj /// [allocator.uses.trait]
62*38fd1498Szrj template<typename _Tp, typename _Alloc>
63*38fd1498Szrj struct uses_allocator
64*38fd1498Szrj : __uses_allocator_helper<_Tp, _Alloc>::type
65*38fd1498Szrj { };
66*38fd1498Szrj
67*38fd1498Szrj struct __uses_alloc_base { };
68*38fd1498Szrj
69*38fd1498Szrj struct __uses_alloc0 : __uses_alloc_base
70*38fd1498Szrj {
71*38fd1498Szrj struct _Sink { void operator=(const void*) { } } _M_a;
72*38fd1498Szrj };
73*38fd1498Szrj
74*38fd1498Szrj template<typename _Alloc>
75*38fd1498Szrj struct __uses_alloc1 : __uses_alloc_base { const _Alloc* _M_a; };
76*38fd1498Szrj
77*38fd1498Szrj template<typename _Alloc>
78*38fd1498Szrj struct __uses_alloc2 : __uses_alloc_base { const _Alloc* _M_a; };
79*38fd1498Szrj
80*38fd1498Szrj template<bool, typename _Tp, typename _Alloc, typename... _Args>
81*38fd1498Szrj struct __uses_alloc;
82*38fd1498Szrj
83*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
84*38fd1498Szrj struct __uses_alloc<true, _Tp, _Alloc, _Args...>
85*38fd1498Szrj : conditional<
86*38fd1498Szrj is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value,
87*38fd1498Szrj __uses_alloc1<_Alloc>,
88*38fd1498Szrj __uses_alloc2<_Alloc>>::type
89*38fd1498Szrj {
90*38fd1498Szrj static_assert(__or_<
91*38fd1498Szrj is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>,
92*38fd1498Szrj is_constructible<_Tp, _Args..., _Alloc>>::value, "construction with"
93*38fd1498Szrj " an allocator must be possible if uses_allocator is true");
94*38fd1498Szrj };
95*38fd1498Szrj
96*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
97*38fd1498Szrj struct __uses_alloc<false, _Tp, _Alloc, _Args...>
98*38fd1498Szrj : __uses_alloc0 { };
99*38fd1498Szrj
100*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
101*38fd1498Szrj using __uses_alloc_t =
102*38fd1498Szrj __uses_alloc<uses_allocator<_Tp, _Alloc>::value, _Tp, _Alloc, _Args...>;
103*38fd1498Szrj
104*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
105*38fd1498Szrj inline __uses_alloc_t<_Tp, _Alloc, _Args...>
106*38fd1498Szrj __use_alloc(const _Alloc& __a)
107*38fd1498Szrj {
108*38fd1498Szrj __uses_alloc_t<_Tp, _Alloc, _Args...> __ret;
109*38fd1498Szrj __ret._M_a = std::__addressof(__a);
110*38fd1498Szrj return __ret;
111*38fd1498Szrj }
112*38fd1498Szrj
113*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
114*38fd1498Szrj void
115*38fd1498Szrj __use_alloc(const _Alloc&&) = delete;
116*38fd1498Szrj
117*38fd1498Szrj #if __cplusplus > 201402L
118*38fd1498Szrj template <typename _Tp, typename _Alloc>
119*38fd1498Szrj inline constexpr bool uses_allocator_v =
120*38fd1498Szrj uses_allocator<_Tp, _Alloc>::value;
121*38fd1498Szrj #endif // C++17
122*38fd1498Szrj
123*38fd1498Szrj template<template<typename...> class _Predicate,
124*38fd1498Szrj typename _Tp, typename _Alloc, typename... _Args>
125*38fd1498Szrj struct __is_uses_allocator_predicate
126*38fd1498Szrj : conditional<uses_allocator<_Tp, _Alloc>::value,
127*38fd1498Szrj __or_<_Predicate<_Tp, allocator_arg_t, _Alloc, _Args...>,
128*38fd1498Szrj _Predicate<_Tp, _Args..., _Alloc>>,
129*38fd1498Szrj _Predicate<_Tp, _Args...>>::type { };
130*38fd1498Szrj
131*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
132*38fd1498Szrj struct __is_uses_allocator_constructible
133*38fd1498Szrj : __is_uses_allocator_predicate<is_constructible, _Tp, _Alloc, _Args...>
134*38fd1498Szrj { };
135*38fd1498Szrj
136*38fd1498Szrj #if __cplusplus >= 201402L
137*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
138*38fd1498Szrj _GLIBCXX17_INLINE constexpr bool __is_uses_allocator_constructible_v =
139*38fd1498Szrj __is_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value;
140*38fd1498Szrj #endif // C++14
141*38fd1498Szrj
142*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
143*38fd1498Szrj struct __is_nothrow_uses_allocator_constructible
144*38fd1498Szrj : __is_uses_allocator_predicate<is_nothrow_constructible,
145*38fd1498Szrj _Tp, _Alloc, _Args...>
146*38fd1498Szrj { };
147*38fd1498Szrj
148*38fd1498Szrj
149*38fd1498Szrj #if __cplusplus >= 201402L
150*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
151*38fd1498Szrj _GLIBCXX17_INLINE constexpr bool
152*38fd1498Szrj __is_nothrow_uses_allocator_constructible_v =
153*38fd1498Szrj __is_nothrow_uses_allocator_constructible<_Tp, _Alloc, _Args...>::value;
154*38fd1498Szrj #endif // C++14
155*38fd1498Szrj
156*38fd1498Szrj template<typename _Tp, typename... _Args>
157*38fd1498Szrj void __uses_allocator_construct_impl(__uses_alloc0 __a, _Tp* __ptr,
158*38fd1498Szrj _Args&&... __args)
159*38fd1498Szrj { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)...); }
160*38fd1498Szrj
161*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
162*38fd1498Szrj void __uses_allocator_construct_impl(__uses_alloc1<_Alloc> __a, _Tp* __ptr,
163*38fd1498Szrj _Args&&... __args)
164*38fd1498Szrj {
165*38fd1498Szrj ::new ((void*)__ptr) _Tp(allocator_arg, *__a._M_a,
166*38fd1498Szrj std::forward<_Args>(__args)...);
167*38fd1498Szrj }
168*38fd1498Szrj
169*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
170*38fd1498Szrj void __uses_allocator_construct_impl(__uses_alloc2<_Alloc> __a, _Tp* __ptr,
171*38fd1498Szrj _Args&&... __args)
172*38fd1498Szrj { ::new ((void*)__ptr) _Tp(std::forward<_Args>(__args)..., *__a._M_a); }
173*38fd1498Szrj
174*38fd1498Szrj template<typename _Tp, typename _Alloc, typename... _Args>
175*38fd1498Szrj void __uses_allocator_construct(const _Alloc& __a, _Tp* __ptr,
176*38fd1498Szrj _Args&&... __args)
177*38fd1498Szrj {
178*38fd1498Szrj __uses_allocator_construct_impl(__use_alloc<_Tp, _Alloc, _Args...>(__a),
179*38fd1498Szrj __ptr, std::forward<_Args>(__args)...);
180*38fd1498Szrj }
181*38fd1498Szrj
182*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
183*38fd1498Szrj } // namespace std
184*38fd1498Szrj
185*38fd1498Szrj #endif
186*38fd1498Szrj #endif
187