32#ifndef WPIUTIL_WPI_FUNCTIONEXTRAS_H
33#define WPIUTIL_WPI_FUNCTIONEXTRAS_H
60#if defined(__GNUC__) && !defined(__clang__)
61#pragma GCC diagnostic push
62#pragma GCC diagnostic ignored "-Warray-bounds"
63#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
70 std::enable_if_t<std::is_trivially_move_constructible<T>::value &&
71 std::is_trivially_destructible<T>::value>;
72template <
typename CallableT,
typename ThisT>
74 std::enable_if_t<!std::is_same<remove_cvref_t<CallableT>, ThisT>::value>;
75template <
typename CallableT,
typename Ret,
typename... Params>
78 std::is_same<decltype(std::declval<CallableT>()(std::declval<Params>()...)),
80 std::is_same<
const decltype(std::declval<CallableT>()(
81 std::declval<Params>()...)),
83 std::is_convertible<
decltype(std::declval<CallableT>()(
84 std::declval<Params>()...)),
91 template <
typename T,
class =
void>
96 T,
std::
enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {};
107 template <
typename T>
struct AdjustedParamTBase {
108 static_assert(!std::is_reference<T>::value,
109 "references should be handled by template specialization");
111 std::conditional_t<std::is_trivially_copy_constructible<T>::value &&
112 std::is_trivially_move_constructible<T>::value &&
113 IsSizeLessThanThresholdT<T>::value,
120 template <
typename T>
struct AdjustedParamTBase<T &> {
using type = T &; };
121 template <
typename T>
struct AdjustedParamTBase<T &&> {
using type = T &; };
123 template <
typename T>
128 using CallPtrT = ReturnT (*)(
void *CallableAddr,
129 AdjustedParamT<ParamTs>... Params);
130 using MovePtrT = void (*)(
void *LHSCallableAddr,
void *RHSCallableAddr);
131 using DestroyPtrT = void (*)(
void *CallableAddr);
135 struct alignas(8) TrivialCallback {
141 struct alignas(8) NonTrivialCallbacks {
144 DestroyPtrT DestroyPtr;
150 using CallbackPointerUnionT =
151 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
155 union StorageUnionT {
158 struct OutOfLineStorageT {
165 "Should always use all of the out-of-line storage for inline storage!");
177 PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag;
179 bool isInlineStorage()
const {
return CallbackAndInlineFlag.getInt(); }
181 bool isTrivialCallback()
const {
182 return isa<TrivialCallback *>(CallbackAndInlineFlag.getPointer());
185 CallPtrT getTrivialCallback()
const {
186 return cast<TrivialCallback *>(CallbackAndInlineFlag.getPointer())->CallPtr;
190 return cast<NonTrivialCallbacks *>(CallbackAndInlineFlag.getPointer());
194 return isTrivialCallback() ? getTrivialCallback()
209 return StorageUnion.OutOfLineStorage.StoragePtr;
213 return StorageUnion.OutOfLineStorage.Size;
216 return StorageUnion.OutOfLineStorage.Alignment;
220 StorageUnion.OutOfLineStorage = {Ptr, Size, Alignment};
223 template <
typename CalledAsT>
225 AdjustedParamT<ParamTs>... Params) {
226 auto &Func = *
reinterpret_cast<CalledAsT *
>(CallableAddr);
227 return Func(std::forward<ParamTs>(Params)...);
230 template <
typename CallableT>
231 static void MoveImpl(
void *LHSCallableAddr,
void *RHSCallableAddr)
noexcept {
232 new (LHSCallableAddr)
233 CallableT(std::move(*
reinterpret_cast<CallableT *
>(RHSCallableAddr)));
236 template <
typename CallableT>
238 reinterpret_cast<CallableT *
>(CallableAddr)->~CallableT();
249 template <
typename CallableT,
typename CalledAs,
typename Enable =
void>
256 template <
typename CallableT,
typename CalledAs>
267 template <
typename CallableT,
typename CalledAsT>
269 bool IsInlineStorage =
true;
272 alignof(CallableT) >
alignof(
decltype(StorageUnion.InlineStorage))) {
273 IsInlineStorage =
false;
276 auto Size =
sizeof(CallableT);
277 auto Alignment =
alignof(CallableT);
283 new (CallableAddr) CallableT(std::move(Callable));
284 CallbackAndInlineFlag.setPointerAndInt(
289 if (!CallbackAndInlineFlag.getPointer())
293 bool IsInlineStorage = isInlineStorage();
295 if (!isTrivialCallback())
299 if (!IsInlineStorage)
306 CallbackAndInlineFlag = RHS.CallbackAndInlineFlag;
312 if (!isInlineStorage()) {
314 StorageUnion.OutOfLineStorage = RHS.StorageUnion.OutOfLineStorage;
315 }
else if (isTrivialCallback()) {
321 RHS.getInlineStorage());
325 RHS.CallbackAndInlineFlag = {};
348 explicit operator bool()
const {
349 return (
bool)CallbackAndInlineFlag.getPointer();
353template <
typename R,
typename... P>
354template <
typename CallableT,
typename CalledAsT,
typename Enable>
355typename UniqueFunctionBase<
R, P...>::NonTrivialCallbacks UniqueFunctionBase<
356 R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
357 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
359template <
typename R,
typename... P>
360template <
typename CallableT,
typename CalledAsT>
361typename UniqueFunctionBase<
R, P...>::TrivialCallback
362 UniqueFunctionBase<
R, P...>::CallbacksHolder<
363 CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
364 &CallImpl<CalledAsT>};
368template <
typename R,
typename... P>
380 template <
typename CallableT>
385 :
Base(
std::forward<CallableT>(Callable),
386 typename
Base::template CalledAs<CallableT>{}) {}
389 return this->getCallPtr()(this->getCalleePtr(), Params...);
393template <
typename R,
typename... P>
406 template <
typename CallableT>
411 :
Base(
std::forward<CallableT>(Callable),
412 typename
Base::template CalledAs<const CallableT>{}) {}
415 return this->getCallPtr()(this->getCalleePtr(), Params...);
419#if defined(__GNUC__) && !defined(__clang__)
420#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:87
void * getInlineStorage() const
Definition: FunctionExtras.h:207
static constexpr size_t InlineStorageSize
Definition: FunctionExtras.h:89
UniqueFunctionBase & operator=(UniqueFunctionBase &&RHS) noexcept
Definition: FunctionExtras.h:333
size_t getOutOfLineStorageSize() const
Definition: FunctionExtras.h:212
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:95
void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment)
Definition: FunctionExtras.h:219
NonTrivialCallbacks * getNonTrivialCallbacks() const
Definition: FunctionExtras.h:189
static ReturnT CallImpl(void *CallableAddr, AdjustedParamT< ParamTs >... Params)
Definition: FunctionExtras.h:224
UniqueFunctionBase()=default
UniqueFunctionBase(UniqueFunctionBase &&RHS) noexcept
Definition: FunctionExtras.h:304
size_t getOutOfLineStorageAlignment() const
Definition: FunctionExtras.h:215
UniqueFunctionBase(CallableT Callable, CalledAs< CalledAsT >)
Definition: FunctionExtras.h:268
static void DestroyImpl(void *CallableAddr) noexcept
Definition: FunctionExtras.h:237
void * getCalleePtr() const
Definition: FunctionExtras.h:204
static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept
Definition: FunctionExtras.h:231
CallPtrT getCallPtr() const
Definition: FunctionExtras.h:193
void * getOutOfLineStorage() const
Definition: FunctionExtras.h:208
~UniqueFunctionBase()
Definition: FunctionExtras.h:288
unique_function()=default
unique_function(const unique_function &)=delete
unique_function & operator=(unique_function &&)=default
R operator()(P... Params) const
Definition: FunctionExtras.h:414
unique_function & operator=(const unique_function &)=delete
unique_function(unique_function &&)=default
unique_function(std::nullptr_t)
Definition: FunctionExtras.h:400
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< const CallableT, R, P... > *=nullptr)
Definition: FunctionExtras.h:407
unique_function()=default
R operator()(P... Params)
Definition: FunctionExtras.h:388
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:381
unique_function(std::nullptr_t)
Definition: FunctionExtras.h:374
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:57
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:256
detail namespace with internal helper functions
Definition: xchar.h:20
type
Definition: core.h:556
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:85
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:74
std::enable_if_t< std::is_trivially_move_constructible< T >::value &&std::is_trivially_destructible< T >::value > EnableIfTrivial
Definition: FunctionExtras.h:71
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:258
Definition: FunctionExtras.h:250
static NonTrivialCallbacks Callbacks
Definition: FunctionExtras.h:251
Definition: FunctionExtras.h:262
Definition: FunctionExtras.h:92