proxygen
Utility.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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 
17 #pragma once
18 
19 #include <cstdint>
20 #include <limits>
21 #include <type_traits>
22 #include <utility>
23 
24 #include <folly/CPortability.h>
25 #include <folly/Traits.h>
26 
27 namespace folly {
28 
71 template <typename T>
72 constexpr typename std::decay<T>::type copy(T&& value) noexcept(
73  noexcept(typename std::decay<T>::type(std::forward<T>(value)))) {
74  return std::forward<T>(value);
75 }
76 
89 #if __cpp_lib_as_const || _LIBCPP_STD_VER > 14 || _MSC_VER
90 
91 /* using override */ using std::as_const;
92 
93 #else
94 
95 template <class T>
96 constexpr T const& as_const(T& t) noexcept {
97  return t;
98 }
99 
100 template <class T>
101 void as_const(T const&&) = delete;
102 
103 #endif
104 
105 // mimic: forward_like, p0847r0
106 template <typename Src, typename Dst>
107 constexpr like_t<Src, Dst>&& forward_like(Dst&& dst) noexcept {
108  return static_cast<like_t<Src, Dst>&&>(std::forward<Dst>(dst));
109 }
110 
111 #if __cpp_lib_exchange_function || _LIBCPP_STD_VER > 11 || _MSC_VER
112 
113 /* using override */ using std::exchange;
114 
115 #else
116 
117 // mimic: std::exchange, C++14
118 // from: http://en.cppreference.com/w/cpp/utility/exchange, CC-BY-SA
119 template <class T, class U = T>
120 T exchange(T& obj, U&& new_value) {
121  T old_value = std::move(obj);
122  obj = std::forward<U>(new_value);
123  return old_value;
124 }
125 
126 #endif
127 
128 namespace utility_detail {
129 template <typename...>
131 template <
132  template <typename T, T...> class S,
133  typename T,
134  T... Ta,
135  T... Tb,
136  T... Tc>
137 struct make_seq_cat<S<T, Ta...>, S<T, Tb...>, S<T, Tc...>> {
138  using type =
139  S<T,
140  Ta...,
141  (sizeof...(Ta) + Tb)...,
142  (sizeof...(Ta) + sizeof...(Tb) + Tc)...>;
143 };
144 
145 // Not parameterizing by `template <typename T, T...> class, typename` because
146 // clang precisely v4.0 fails to compile that. Note that clang v3.9 and v5.0
147 // handle that code correctly.
148 //
149 // For this to work, `S0` is required to be `Sequence<T>` and `S1` is required
150 // to be `Sequence<T, 0>`.
151 
152 template <std::size_t Size>
153 struct make_seq {
154  template <typename S0, typename S1>
155  using apply = typename make_seq_cat<
156  typename make_seq<Size / 2>::template apply<S0, S1>,
157  typename make_seq<Size / 2>::template apply<S0, S1>,
158  typename make_seq<Size % 2>::template apply<S0, S1>>::type;
159 };
160 template <>
161 struct make_seq<1> {
162  template <typename S0, typename S1>
163  using apply = S1;
164 };
165 template <>
166 struct make_seq<0> {
167  template <typename S0, typename S1>
168  using apply = S0;
169 };
170 } // namespace utility_detail
171 
172 #if __cpp_lib_integer_sequence || _MSC_VER
173 
174 /* using override */ using std::index_sequence;
175 /* using override */ using std::integer_sequence;
176 
177 #else
178 
179 // TODO: Remove after upgrading to C++14 baseline
180 
181 template <class T, T... Ints>
183  using value_type = T;
184 
185  static constexpr std::size_t size() noexcept {
186  return sizeof...(Ints);
187  }
188 };
189 
190 template <std::size_t... Ints>
191 using index_sequence = integer_sequence<std::size_t, Ints...>;
192 
193 #endif
194 
195 #if FOLLY_HAS_BUILTIN(__make_integer_seq) || _MSC_FULL_VER >= 190023918
196 
197 template <typename T, std::size_t Size>
198 using make_integer_sequence = __make_integer_seq<integer_sequence, T, Size>;
199 
200 #else
201 
202 template <typename T, std::size_t Size>
203 using make_integer_sequence = typename utility_detail::make_seq<
204  Size>::template apply<integer_sequence<T>, integer_sequence<T, 0>>;
205 
206 #endif
207 
208 template <std::size_t Size>
210 template <class... T>
212 
223 struct in_place_tag {};
224 template <class>
226 template <std::size_t>
228 
230 template <class T>
232 template <std::size_t I>
234 
236  return {};
237 }
238 template <class T>
240  return {};
241 }
242 template <std::size_t I>
244  return {};
245 }
246 
291 
310 struct presorted_t {};
311 constexpr presorted_t presorted{};
312 
331 struct unsorted_t {};
332 constexpr unsorted_t unsorted{};
333 
334 template <typename T>
335 struct transparent : T {
336  using is_transparent = void;
337  using T::T;
338 };
339 
359 struct Identity {
360  template <class T>
361  constexpr T&& operator()(T&& x) const noexcept {
362  return static_cast<T&&>(x);
363  }
364 };
365 
366 namespace moveonly_ { // Protection from unintended ADL.
367 
373 class MoveOnly {
374  protected:
375  constexpr MoveOnly() = default;
376  ~MoveOnly() = default;
377 
378  MoveOnly(MoveOnly&&) = default;
379  MoveOnly& operator=(MoveOnly&&) = default;
380  MoveOnly(const MoveOnly&) = delete;
381  MoveOnly& operator=(const MoveOnly&) = delete;
382 };
383 
384 } // namespace moveonly_
385 
387 
388 template <typename T>
389 constexpr auto to_signed(T const& t) -> typename std::make_signed<T>::type {
390  using S = typename std::make_signed<T>::type;
391  // note: static_cast<S>(t) would be more straightforward, but it would also be
392  // implementation-defined behavior and that is typically to be avoided; the
393  // following code optimized into the same thing, though
394  return std::numeric_limits<S>::max() < t ? -static_cast<S>(~t) + S{-1}
395  : static_cast<S>(t);
396 }
397 
398 template <typename T>
399 constexpr auto to_unsigned(T const& t) -> typename std::make_unsigned<T>::type {
400  using U = typename std::make_unsigned<T>::type;
401  return static_cast<U>(t);
402 }
403 
404 } // namespace folly
Definition: InvokeTest.cpp:58
make_index_sequence< sizeof...(T)> index_sequence_for
Definition: Utility.h:211
#define T(v)
Definition: http_parser.c:233
typename make_seq_cat< typename make_seq< Size/2 >::template apply< S0, S1 >, typename make_seq< Size/2 >::template apply< S0, S1 >, typename make_seq< Size%2 >::template apply< S0, S1 >>::type apply
Definition: Utility.h:158
static constexpr std::size_t size() noexcept
Definition: Utility.h:185
LogLevel max
Definition: LogLevel.cpp:31
PskType type
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const int x
STL namespace.
folly::std T
in_place_type_tag< T > in_place_type(in_place_type_tag< T >={})
Definition: Utility.h:239
void as_const(T const &&)=delete
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
in_place_tag in_place(in_place_tag={})
Definition: Utility.h:235
requires E e noexcept(noexcept(s.error(std::move(e))))
in_place_tag(&)(in_place_tag) in_place_t
Definition: Utility.h:229
constexpr T const & as_const(T &t) noexcept
Definition: Utility.h:96
constexpr auto to_unsigned(T const &t) -> typename std::make_unsigned< T >::type
Definition: Utility.h:399
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
typename detail::like_< Src >::template apply< remove_cvref_t< Dst >> like_t
Definition: Traits.h:220
constexpr like_t< Src, Dst > && forward_like(Dst &&dst) noexcept
Definition: Utility.h:107
integer_sequence< std::size_t, Ints... > index_sequence
Definition: Utility.h:191
make_integer_sequence< std::size_t, Size > make_index_sequence
Definition: Utility.h:209
S< T, Ta...,(sizeof...(Ta)+Tb)...,(sizeof...(Ta)+sizeof...(Tb)+Tc)... > type
Definition: Utility.h:142
typename utility_detail::make_seq< Size >::template apply< integer_sequence< T >, integer_sequence< T, 0 >> make_integer_sequence
Definition: Utility.h:204
constexpr auto to_signed(T const &t) -> typename std::make_signed< T >::type
Definition: Utility.h:389
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
constexpr unsorted_t unsorted
Definition: Utility.h:332
moveonly_::MoveOnly MoveOnly
Definition: Utility.h:386
in_place_index_tag< I > in_place_index(in_place_index_tag< I >={})
Definition: Utility.h:243
constexpr presorted_t presorted
Definition: Utility.h:311
constexpr T && operator()(T &&x) const noexcept
Definition: Utility.h:361
const
Definition: upload.py:398
constexpr initlist_construct_t initlist_construct
Definition: Utility.h:290
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)