32#ifndef WPIUTIL_WPI_FUNCTIONEXTRAS_H
33#define WPIUTIL_WPI_FUNCTIONEXTRAS_H
61#if defined(__GNUC__) && !defined(__clang__)
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Warray-bounds"
64#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
71 std::enable_if_t<std::is_trivially_move_constructible<T>::value &&
72 std::is_trivially_destructible<T>::value>;
73template <
typename CallableT,
typename ThisT>
75 std::enable_if_t<!std::is_same<remove_cvref_t<CallableT>, ThisT>::value>;
76template <
typename CallableT,
typename Ret,
typename... Params>
79 std::is_same<decltype(std::declval<CallableT>()(std::declval<Params>()...)),
81 std::is_same<
const decltype(std::declval<CallableT>()(
82 std::declval<Params>()...)),
84 std::is_convertible<
decltype(std::declval<CallableT>()(
85 std::declval<Params>()...)),
92 template <
typename T,
class =
void>
97 T,
std::
enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {};
108 template <
typename T>
struct AdjustedParamTBase {
109 static_assert(!std::is_reference<T>::value,
110 "references should be handled by template specialization");
112 std::conditional_t<std::is_trivially_copy_constructible<T>::value &&
113 std::is_trivially_move_constructible<T>::value &&
114 IsSizeLessThanThresholdT<T>::value,
121 template <
typename T>
struct AdjustedParamTBase<T &> {
using type = T &; };
122 template <
typename T>
struct AdjustedParamTBase<T &&> {
using type = T &; };
124 template <
typename T>
129 using CallPtrT = ReturnT (*)(
void *CallableAddr,
130 AdjustedParamT<ParamTs>... Params);
131 using MovePtrT = void (*)(
void *LHSCallableAddr,
void *RHSCallableAddr);
132 using DestroyPtrT = void (*)(
void *CallableAddr);
136 struct alignas(8) TrivialCallback {
142 struct alignas(8) NonTrivialCallbacks {
145 DestroyPtrT DestroyPtr;
151 using CallbackPointerUnionT =
152 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
156 union StorageUnionT {
159 struct OutOfLineStorageT {
166 "Should always use all of the out-of-line storage for inline storage!");
178 PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag;
180 bool isInlineStorage()
const {
return CallbackAndInlineFlag.getInt(); }
182 bool isTrivialCallback()
const {
183 return isa<TrivialCallback *>(CallbackAndInlineFlag.getPointer());
186 CallPtrT getTrivialCallback()
const {
187 return cast<TrivialCallback *>(CallbackAndInlineFlag.getPointer())->CallPtr;
191 return cast<NonTrivialCallbacks *>(CallbackAndInlineFlag.getPointer());
195 return isTrivialCallback() ? getTrivialCallback()
210 return StorageUnion.OutOfLineStorage.StoragePtr;
214 return StorageUnion.OutOfLineStorage.Size;
217 return StorageUnion.OutOfLineStorage.Alignment;
221 StorageUnion.OutOfLineStorage = {Ptr, Size, Alignment};
224 template <
typename CalledAsT>
226 AdjustedParamT<ParamTs>... Params) {
227 auto &Func = *
reinterpret_cast<CalledAsT *
>(CallableAddr);
228 return Func(std::forward<ParamTs>(Params)...);
231 template <
typename CallableT>
232 static void MoveImpl(
void *LHSCallableAddr,
void *RHSCallableAddr)
noexcept {
233 new (LHSCallableAddr)
234 CallableT(std::move(*
reinterpret_cast<CallableT *
>(RHSCallableAddr)));
237 template <
typename CallableT>
239 reinterpret_cast<CallableT *
>(CallableAddr)->~CallableT();
250 template <
typename CallableT,
typename CalledAs,
typename Enable =
void>
257 template <
typename CallableT,
typename CalledAs>
268 template <
typename CallableT,
typename CalledAsT>
270 bool IsInlineStorage =
true;
273 alignof(CallableT) >
alignof(
decltype(StorageUnion.InlineStorage))) {
274 IsInlineStorage =
false;
277 auto Size =
sizeof(CallableT);
278 auto Alignment =
alignof(CallableT);
284 new (CallableAddr) CallableT(std::move(Callable));
285 CallbackAndInlineFlag.setPointerAndInt(
290 if (!CallbackAndInlineFlag.getPointer())
294 bool IsInlineStorage = isInlineStorage();
296 if (!isTrivialCallback())
300 if (!IsInlineStorage)
307 CallbackAndInlineFlag = RHS.CallbackAndInlineFlag;
313 if (!isInlineStorage()) {
315 StorageUnion.OutOfLineStorage = RHS.StorageUnion.OutOfLineStorage;
316 }
else if (isTrivialCallback()) {
322 RHS.getInlineStorage());
326 RHS.CallbackAndInlineFlag = {};
328#if !defined(NDEBUG) && !LLVM_ADDRESS_SANITIZER_BUILD
351 explicit operator bool()
const {
352 return (
bool)CallbackAndInlineFlag.getPointer();
356template <
typename R,
typename... P>
357template <
typename CallableT,
typename CalledAsT,
typename Enable>
358typename UniqueFunctionBase<
R, P...>::NonTrivialCallbacks UniqueFunctionBase<
359 R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
360 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
362template <
typename R,
typename... P>
363template <
typename CallableT,
typename CalledAsT>
364typename UniqueFunctionBase<
R, P...>::TrivialCallback
365 UniqueFunctionBase<
R, P...>::CallbacksHolder<
366 CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
367 &CallImpl<CalledAsT>};
371template <
typename R,
typename... P>
383 template <
typename CallableT>
388 :
Base(
std::forward<CallableT>(Callable),
389 typename
Base::template CalledAs<CallableT>{}) {}
392 return this->getCallPtr()(this->getCalleePtr(), Params...);
396template <
typename R,
typename... P>
409 template <
typename CallableT>
414 :
Base(
std::forward<CallableT>(Callable),
415 typename
Base::template CalledAs<const CallableT>{}) {}
418 return this->getCallPtr()(this->getCalleePtr(), Params...);
422#if defined(__GNUC__) && !defined(__clang__)
423#pragma GCC diagnostic pop
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
This file defines the PointerIntPair class.
This file defines the PointerUnion class, which is a discriminated union of pointer types.
This file contains library features backported from future STL versions.
Definition: FunctionExtras.h:88
void * getInlineStorage() const
Definition: FunctionExtras.h:208
static constexpr size_t InlineStorageSize
Definition: FunctionExtras.h:90
UniqueFunctionBase & operator=(UniqueFunctionBase &&RHS) noexcept
Definition: FunctionExtras.h:336
size_t getOutOfLineStorageSize() const
Definition: FunctionExtras.h:213
struct IsSizeLessThanThresholdT< T, std::enable_if_t< sizeof(T)<=2 *sizeof(void *)> > :std::true_type {};template< typename T > struct AdjustedParamTBase { static_assert(!std::is_reference< T >::value, "references should be handled by template specialization");using type=std::conditional_t< std::is_trivially_copy_constructible< T >::value &&std::is_trivially_move_constructible< T >::value &&IsSizeLessThanThresholdT< T >::value, T, T & >;};template< typename T > struct AdjustedParamTBase< T & > { using type=T &;};template< typename T > struct AdjustedParamTBase< T && > { using type=T &;};template< typename T > using AdjustedParamT=typename AdjustedParamTBase< T >::type;using CallPtrT=ReturnT(*)(void *CallableAddr, AdjustedParamT< ParamTs >... Params);using MovePtrT=void(*)(void *LHSCallableAddr, void *RHSCallableAddr);using DestroyPtrT=void(*)(void *CallableAddr);struct alignas(8) TrivialCallback { CallPtrT CallPtr;};struct alignas(8) NonTrivialCallbacks { CallPtrT CallPtr;MovePtrT MovePtr;DestroyPtrT DestroyPtr;};using CallbackPointerUnionT=PointerUnion< TrivialCallback *, NonTrivialCallbacks * >;union StorageUnionT { struct OutOfLineStorageT { void *StoragePtr;size_t Size;size_t Alignment;} OutOfLineStorage;static_assert(sizeof(OutOfLineStorageT)<=InlineStorageSize, "Should always use all of the out-of-line storage for inline storage!");alignas(void *) mutable std::byte InlineStorage[InlineStorageSize];} StorageUnion;PointerIntPair< CallbackPointerUnionT, 1, bool > CallbackAndInlineFlag;bool isInlineStorage() const { return CallbackAndInlineFlag.getInt();} bool isTrivialCallback() const { return isa< TrivialCallback * >(CallbackAndInlineFlag.getPointer());} CallPtrT getTrivialCallback() const { return cast< TrivialCallback * >(CallbackAndInlineFlag.getPointer()) -> CallPtr
Definition: FunctionExtras.h:96
void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment)
Definition: FunctionExtras.h:220
NonTrivialCallbacks * getNonTrivialCallbacks() const
Definition: FunctionExtras.h:190
static ReturnT CallImpl(void *CallableAddr, AdjustedParamT< ParamTs >... Params)
Definition: FunctionExtras.h:225
UniqueFunctionBase()=default
UniqueFunctionBase(UniqueFunctionBase &&RHS) noexcept
Definition: FunctionExtras.h:305
size_t getOutOfLineStorageAlignment() const
Definition: FunctionExtras.h:216
UniqueFunctionBase(CallableT Callable, CalledAs< CalledAsT >)
Definition: FunctionExtras.h:269
static void DestroyImpl(void *CallableAddr) noexcept
Definition: FunctionExtras.h:238
void * getCalleePtr() const
Definition: FunctionExtras.h:205
static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept
Definition: FunctionExtras.h:232
CallPtrT getCallPtr() const
Definition: FunctionExtras.h:194
void * getOutOfLineStorage() const
Definition: FunctionExtras.h:209
~UniqueFunctionBase()
Definition: FunctionExtras.h:289
unique_function()=default
unique_function(const unique_function &)=delete
unique_function & operator=(unique_function &&)=default
R operator()(P... Params) const
Definition: FunctionExtras.h:417
unique_function & operator=(const unique_function &)=delete
unique_function(unique_function &&)=default
unique_function(std::nullptr_t)
Definition: FunctionExtras.h:403
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< const CallableT, R, P... > *=nullptr)
Definition: FunctionExtras.h:410
unique_function()=default
R operator()(P... Params)
Definition: FunctionExtras.h:391
unique_function(const unique_function &)=delete
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< CallableT, R, P... > *=nullptr)
Definition: FunctionExtras.h:384
unique_function(std::nullptr_t)
Definition: FunctionExtras.h:377
unique_function(unique_function &&)=default
unique_function & operator=(unique_function &&)=default
unique_function & operator=(const unique_function &)=delete
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:58
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:271
detail namespace with internal helper functions
Definition: printf.h:61
type
Definition: core.h:573
Implement std::hash so that hash_code can be used in STL containers.
Definition: array.h:89
static constexpr const unit_t< compound_unit< energy::joules, inverse< temperature::kelvin >, inverse< substance::moles > > > R(8.3144598)
Gas constant.
std::enable_if_t< std::disjunction< std::is_void< Ret >, std::is_same< decltype(std::declval< CallableT >()(std::declval< Params >()...)), Ret >, std::is_same< const decltype(std::declval< CallableT >()(std::declval< Params >()...)), Ret >, std::is_convertible< decltype(std::declval< CallableT >()(std::declval< Params >()...)), Ret > >::value > EnableIfCallable
Definition: FunctionExtras.h:86
typename std::enable_if< E, T >::type enable_if_t
Definition: expected:233
std::enable_if_t<!std::is_same< remove_cvref_t< CallableT >, ThisT >::value > EnableUnlessSameType
Definition: FunctionExtras.h:75
std::enable_if_t< std::is_trivially_move_constructible< T >::value &&std::is_trivially_destructible< T >::value > EnableIfTrivial
Definition: FunctionExtras.h:72
Definition: ntcore_cpp.h:26
void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * allocate_buffer(size_t Size, size_t Alignment)
Allocate a buffer of memory with the given size and alignment.
static TrivialCallback Callbacks
Definition: FunctionExtras.h:259
Definition: FunctionExtras.h:251
static NonTrivialCallbacks Callbacks
Definition: FunctionExtras.h:252
Definition: FunctionExtras.h:263
Definition: FunctionExtras.h:93