proxygen
traits.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <functional>
19 #include <type_traits>
20 
22 
23 #define PUSHMI_NOEXCEPT_AUTO(...) \
24  noexcept(noexcept(static_cast<decltype((__VA_ARGS__))>(__VA_ARGS__)))\
25 
26 #define PUSHMI_NOEXCEPT_RETURN(...) \
27  PUSHMI_NOEXCEPT_AUTO(__VA_ARGS__) {\
28  return (__VA_ARGS__);\
29  }\
30 
31 
32 namespace folly {
33 namespace pushmi {
34 #if __cpp_fold_expressions >= 201603
35 template <bool... Bs>
36 PUSHMI_INLINE_VAR constexpr bool and_v = (Bs && ...);
37 
38 template <bool... Bs>
39 PUSHMI_INLINE_VAR constexpr bool or_v = (Bs || ...);
40 
41 template <int... Is>
42 PUSHMI_INLINE_VAR constexpr int sum_v = (Is + ...);
43 #else
44 namespace detail {
45 
46 template <bool...>
47 struct bools;
48 
49 template <std::size_t N>
50 constexpr int sum_impl(int const (&rgi)[N], int i = 0, int state = 0) noexcept {
51  return i == N ? state : sum_impl(rgi, i + 1, state + rgi[i]);
52 }
53 template <int... Is>
54 constexpr int sum_impl() noexcept {
55  using RGI = int[sizeof...(Is)];
56  return sum_impl(RGI{Is...});
57 }
58 
59 } // namespace detail
60 
61 template <bool... Bs>
62 PUSHMI_INLINE_VAR constexpr bool and_v =
64 
65 template <bool... Bs>
66 PUSHMI_INLINE_VAR constexpr bool or_v = !PUSHMI_PP_IS_SAME(
69 
70 template <int... Is>
71 PUSHMI_INLINE_VAR constexpr int sum_v = detail::sum_impl<Is...>();
72 #endif
73 
74 template <class...>
75 struct typelist;
76 
77 template <class...>
78 using void_t = void;
79 
80 template <class T>
81 using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
82 
84  template(class... Args)
85  (concept True)(Args...),
86  true
87 );
88 
90  template(class T, template<class...> class C)
91  (concept Valid)(T, C),
92  True< C<T> >
93 );
94 
96  template (class T, template<class...> class Trait, class... Args)
97  (concept Satisfies)(T, Trait, Args...),
98  static_cast<bool>(Trait<T>::type::value)
99 );
100 
102  template (class T, class U)
103  concept Same,
105 );
106 
108  template (bool...Bs)
109  (concept And)(Bs...),
110  and_v<Bs...>
111 );
112 
114  template (bool...Bs)
115  (concept Or)(Bs...),
116  or_v<Bs...>
117 );
118 
120  template (class T)
121  concept Object,
122  requires (T* p) (
123  *p,
124  implicitly_convertible_to<const volatile void*>(p)
125  )
126 );
127 
129  template (class T, class... Args)
130  (concept Constructible)(T, Args...),
132 );
133 
135  template (class T)
136  concept MoveConstructible,
137  Constructible<T, T>
138 );
139 
141  template (class From, class To)
142  concept ConvertibleTo,
143  requires (From (&f)()) (
144  static_cast<To>(f())
146 );
147 
149  template (class A, class B)
150  concept DerivedFrom,
151  __is_base_of(B, A)
152 );
153 
155  template (class A)
156  concept Decayed,
157  Same<A, std::decay_t<A>>
158 );
159 
161  template (class T, class U)
162  concept Assignable,
163  requires(T t, U&& u) (
164  t = (U &&) u,
165  requires_<Same<decltype(t = (U &&) u), T>>
166  ) && Same<T, T&>
167 );
168 
170  template (class T)
171  concept EqualityComparable,
172  requires(remove_cvref_t<T> const & t) (
173  implicitly_convertible_to<bool>( t == t ),
174  implicitly_convertible_to<bool>( t != t )
175  )
176 );
177 
179  template (class T)
180  concept SemiMovable,
181  Object<T> && Constructible<T, T> && ConvertibleTo<T, T>
182 );
183 
185  template (class T)
187  SemiMovable<T> && Assignable<T&, T>
188 );
189 
191  template (class T)
192  concept Copyable,
193  Movable<T> &&
194  Assignable<T&, const T&> &&
195  ConvertibleTo<const T&, T>
196 );
197 
199  template (class T)
200  concept Semiregular,
201  Copyable<T> && Constructible<T>
202 );
203 
205  template (class T)
206  concept Regular,
207  Semiregular<T> && EqualityComparable<T>
208 );
209 
210 namespace detail {
211 // is_ taken from meta library
212 
213 template <typename, template <typename...> class>
214 struct is_ : std::false_type {};
215 
216 template <typename... Ts, template <typename...> class C>
217 struct is_<C<Ts...>, C> : std::true_type {};
218 
219 template <typename T, template <typename...> class C>
220 constexpr bool is_v = is_<T, C>::value;
221 
222 template <bool B, class T = void>
223 using requires_ = std::enable_if_t<B, T>;
224 
225 PUSHMI_INLINE_VAR constexpr struct as_const_fn {
226  template <class T>
227  constexpr const T& operator()(T& t) const noexcept {
228  return t;
229  }
230 } const as_const{};
231 
232 } // namespace detail
233 
234 } // namespace pushmi
235 } // namespace folly
PUSHMI_INLINE_VAR constexpr bool and_v
Definition: traits.h:62
#define PUSHMI_PP_IS_CONSTRUCTIBLE(...)
Definition: concept_def.h:76
std::true_type True
Definition: TypeList.h:82
auto f
std::unique_ptr< int > A
std::remove_cv_t< std::remove_reference_t< T >> remove_cvref_t
Definition: traits.h:81
folly::std T
constexpr bool is_v
Definition: traits.h:220
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
constexpr int sum_impl(int const (&rgi)[N], int i=0, int state=0) noexcept
Definition: traits.h:50
bool_constant< true > true_type
Definition: gtest-port.h:2210
#define PUSHMI_PP_IS_SAME(...)
Definition: concept_def.h:68
PUSHMI_INLINE_VAR constexpr int sum_v
Definition: traits.h:71
PUSHMI_CONCEPT_DEF(template(class PS) concept Cardinality, has_cardinality_v< PS >)
#define concept
requires requires(::folly::pushmi::invoke(std::declval< F >(), std::get< Is >(std::declval< Tuple >())...))) const expr decltype(auto) apply_impl(F &&f
constexpr const T & operator()(T &t) const noexcept
Definition: traits.h:227
void void_t
Definition: traits.h:78
#define C(name, bit)
Definition: CpuId.h:204
static const char *const value
Definition: Conv.cpp:50
#define PUSHMI_INLINE_VAR
Definition: concept_def.h:60
std::enable_if_t< B, T > requires_
Definition: traits.h:223
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::detail::as_const_fn as_const
PUSHMI_INLINE_VAR constexpr bool or_v
Definition: traits.h:66
bool_constant< false > false_type
Definition: gtest-port.h:2209
state
Definition: http_parser.c:272