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 {};
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 &&
123 template <
typename T>
130 using MovePtrT = void (*)(
void *LHSCallableAddr,
void *RHSCallableAddr);
165 "Should always use all of the out-of-line storage for inline storage!");
223 template <
typename CalledAsT>
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;
273 IsInlineStorage =
false;
276 auto Size =
sizeof(CallableT);
277 auto Alignment =
alignof(CallableT);
283 new (CallableAddr) CallableT(std::move(Callable));
299 if (!IsInlineStorage)
321 RHS.getInlineStorage());
325 RHS.CallbackAndInlineFlag = {};
327#if !defined(NDEBUG) && !LLVM_ADDRESS_SANITIZER_BUILD
350 explicit operator bool()
const {
355template <
typename R,
typename... P>
356template <
typename CallableT,
typename CalledAsT,
typename Enable>
357typename UniqueFunctionBase<R, P...>::NonTrivialCallbacks UniqueFunctionBase<
358 R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
359 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
361template <
typename R,
typename... P>
362template <
typename CallableT,
typename CalledAsT>
363typename UniqueFunctionBase<R, P...>::TrivialCallback
364 UniqueFunctionBase<R, P...>::CallbacksHolder<
366 &CallImpl<CalledAsT>};
370template <
typename R,
typename... P>
382 template <
typename CallableT>
387 :
Base(
std::forward<CallableT>(Callable),
388 typename
Base::template CalledAs<CallableT>{}) {}
391 return this->getCallPtr()(this->getCalleePtr(), Params...);
395template <
typename R,
typename... P>
408 template <
typename CallableT>
413 :
Base(
std::forward<CallableT>(Callable),
414 typename
Base::template CalledAs<const CallableT>{}) {}
417 return this->getCallPtr()(this->getCalleePtr(), Params...);
421#if defined(__GNUC__) && !defined(__clang__)
422#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.
PointerIntPair - This class implements a pair of a pointer and small integer.
Definition PointerIntPair.h:80
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition PointerUnion.h:163
Definition FunctionExtras.h:87
void(*)(void *CallableAddr) DestroyPtrT
Definition FunctionExtras.h:131
void * getInlineStorage() const
Definition FunctionExtras.h:207
union wpi::detail::UniqueFunctionBase::StorageUnionT StorageUnion
static constexpr size_t InlineStorageSize
Definition FunctionExtras.h:89
UniqueFunctionBase & operator=(UniqueFunctionBase &&RHS) noexcept
Definition FunctionExtras.h:335
size_t getOutOfLineStorageSize() const
Definition FunctionExtras.h:212
void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment)
Definition FunctionExtras.h:219
NonTrivialCallbacks * getNonTrivialCallbacks() const
Definition FunctionExtras.h:189
typename AdjustedParamTBase< T >::type AdjustedParamT
Definition FunctionExtras.h:124
static ReturnT CallImpl(void *CallableAddr, AdjustedParamT< ParamTs >... Params)
Definition FunctionExtras.h:224
bool isTrivialCallback() const
Definition FunctionExtras.h:181
UniqueFunctionBase()=default
UniqueFunctionBase(UniqueFunctionBase &&RHS) noexcept
Definition FunctionExtras.h:304
void(*)(void *LHSCallableAddr, void *RHSCallableAddr) MovePtrT
Definition FunctionExtras.h:130
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
ReturnT(*)(void *CallableAddr, AdjustedParamT< ParamTs >... Params) CallPtrT
Definition FunctionExtras.h:128
PointerIntPair< CallbackPointerUnionT, 1, bool > CallbackAndInlineFlag
Definition FunctionExtras.h:177
static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept
Definition FunctionExtras.h:231
bool isInlineStorage() const
Definition FunctionExtras.h:179
CallPtrT getCallPtr() const
Definition FunctionExtras.h:193
CallPtrT getTrivialCallback() const
Definition FunctionExtras.h:185
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:416
unique_function & operator=(const unique_function &)=delete
unique_function(unique_function &&)=default
unique_function(std::nullptr_t)
Definition FunctionExtras.h:402
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< const CallableT, R, P... > *=nullptr)
Definition FunctionExtras.h:409
unique_function()=default
R operator()(P... Params)
Definition FunctionExtras.h:390
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:383
unique_function(std::nullptr_t)
Definition FunctionExtras.h:376
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
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
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:76
std::enable_if_t< std::is_trivially_move_constructible< T >::value && std::is_trivially_destructible< T >::value > EnableIfTrivial
Definition FunctionExtras.h:69
std::enable_if_t<!std::is_same< remove_cvref_t< CallableT >, ThisT >::value > EnableUnlessSameType
Definition FunctionExtras.h:73
Foonathan namespace.
Definition ntcore_cpp.h:26
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
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.
T & type
Definition FunctionExtras.h:120
T & type
Definition FunctionExtras.h:121
Definition FunctionExtras.h:107
std::conditional_t< std::is_trivially_copy_constructible< T >::value && std::is_trivially_move_constructible< T >::value && IsSizeLessThanThresholdT< T >::value, T, T & > type
Definition FunctionExtras.h:110
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
A struct we use to aggregate three callbacks when we need full set of operations.
Definition FunctionExtras.h:141
MovePtrT MovePtr
Definition FunctionExtras.h:143
CallPtrT CallPtr
Definition FunctionExtras.h:142
DestroyPtrT DestroyPtr
Definition FunctionExtras.h:144
Definition FunctionExtras.h:158
void * StoragePtr
Definition FunctionExtras.h:159
size_t Alignment
Definition FunctionExtras.h:161
size_t Size
Definition FunctionExtras.h:160
A struct to hold a single trivial callback with sufficient alignment for our bitpacking.
Definition FunctionExtras.h:135
CallPtrT CallPtr
Definition FunctionExtras.h:136
Definition FunctionExtras.h:155
struct wpi::detail::UniqueFunctionBase::StorageUnionT::OutOfLineStorageT OutOfLineStorage
std::byte InlineStorage[InlineStorageSize]
Definition FunctionExtras.h:171
typename std::enable_if< B, T >::type enable_if_t
Definition base.h:297