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