32#ifndef WPIUTIL_WPI_FUNCTIONEXTRAS_H
33#define WPIUTIL_WPI_FUNCTIONEXTRAS_H
38#include "wpi/util/Compiler.hpp"
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>()...)),
92 template <
typename T,
class =
void>
97 T,
std::
enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {};
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 &&
124 template <
typename T>
131 using MovePtrT = void (*)(
void *LHSCallableAddr,
void *RHSCallableAddr);
166 "Should always use all of the out-of-line storage for inline storage!");
225 template <
typename CalledAsT>
228 auto &Func = *
reinterpret_cast<CalledAsT *
>(CallableAddr);
229 return Func(std::forward<ParamTs>(Params)...);
232 template <
typename CallableT>
233 static void MoveImpl(
void *LHSCallableAddr,
void *RHSCallableAddr)
noexcept {
234 new (LHSCallableAddr)
235 CallableT(std::move(*
reinterpret_cast<CallableT *
>(RHSCallableAddr)));
238 template <
typename CallableT>
240 reinterpret_cast<CallableT *
>(CallableAddr)->~CallableT();
251 template <
typename CallableT,
typename CalledAs,
typename Enable =
void>
258 template <
typename CallableT,
typename CalledAs>
269 template <
typename CallableT,
typename CalledAsT>
271 bool IsInlineStorage =
true;
275 IsInlineStorage =
false;
278 auto Size =
sizeof(CallableT);
279 auto Alignment =
alignof(CallableT);
285 new (CallableAddr) CallableT(std::move(Callable));
301 if (!IsInlineStorage)
316 StorageUnion.OutOfLineStorage = RHS.StorageUnion.OutOfLineStorage;
323 RHS.getInlineStorage());
328 RHS.CallbackAndInlineFlag = {};
330#if !defined(NDEBUG) && !LLVM_ADDRESS_SANITIZER_BUILD
353 explicit operator bool()
const {
358template <
typename R,
typename... P>
359template <
typename CallableT,
typename CalledAsT,
typename Enable>
360typename UniqueFunctionBase<R, P...>::NonTrivialCallbacks UniqueFunctionBase<
361 R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
362 &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
364template <
typename R,
typename... P>
365template <
typename CallableT,
typename CalledAsT>
366typename UniqueFunctionBase<R, P...>::TrivialCallback
367 UniqueFunctionBase<R, P...>::CallbacksHolder<
369 &CallImpl<CalledAsT>};
373template <
typename R,
typename... P>
385 template <
typename CallableT>
390 : Base(
std::forward<CallableT>(Callable),
391 typename Base::template CalledAs<CallableT>{}) {}
398template <
typename R,
typename... P>
411 template <
typename CallableT>
416 : Base(
std::forward<CallableT>(Callable),
417 typename Base::template CalledAs<const CallableT>{}) {}
424#if defined(__GNUC__) && !defined(__clang__)
425#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.
typename std::enable_if< B, T >::type enable_if_t
Definition base.h:307
PointerIntPair - This class implements a pair of a pointer and small integer.
Definition PointerIntPair.hpp:80
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition PointerUnion.hpp:163
Definition FunctionExtras.hpp:87
typename AdjustedParamTBase< T >::type AdjustedParamT
Definition FunctionExtras.hpp:125
CallPtrT getCallPtr() const
Definition FunctionExtras.hpp:195
NonTrivialCallbacks * getNonTrivialCallbacks() const
Definition FunctionExtras.hpp:191
UniqueFunctionBase()=default
UniqueFunctionBase & operator=(UniqueFunctionBase &&RHS) noexcept
Definition FunctionExtras.hpp:338
PointerUnion< TrivialCallback *, NonTrivialCallbacks * > CallbackPointerUnionT
Definition FunctionExtras.hpp:151
union wpi::util::detail::UniqueFunctionBase::StorageUnionT StorageUnion
void * getOutOfLineStorage() const
Definition FunctionExtras.hpp:210
size_t getOutOfLineStorageSize() const
Definition FunctionExtras.hpp:214
static ReturnT CallImpl(void *CallableAddr, AdjustedParamT< ParamTs >... Params)
Definition FunctionExtras.hpp:226
PointerIntPair< CallbackPointerUnionT, 1, bool > CallbackAndInlineFlag
Definition FunctionExtras.hpp:179
size_t getOutOfLineStorageAlignment() const
Definition FunctionExtras.hpp:217
void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment)
Definition FunctionExtras.hpp:221
void * getInlineStorage() const
Definition FunctionExtras.hpp:209
void(*)(void *CallableAddr) DestroyPtrT
Definition FunctionExtras.hpp:132
static void DestroyImpl(void *CallableAddr) noexcept
Definition FunctionExtras.hpp:239
~UniqueFunctionBase()
Definition FunctionExtras.hpp:290
void * getCalleePtr() const
Definition FunctionExtras.hpp:206
bool isInlineStorage() const
Definition FunctionExtras.hpp:181
UniqueFunctionBase(CallableT Callable, CalledAs< CalledAsT >)
Definition FunctionExtras.hpp:270
void(*)(void *LHSCallableAddr, void *RHSCallableAddr) MovePtrT
Definition FunctionExtras.hpp:131
UniqueFunctionBase(UniqueFunctionBase &&RHS) noexcept
Definition FunctionExtras.hpp:306
CallPtrT getTrivialCallback() const
Definition FunctionExtras.hpp:187
static constexpr size_t InlineStorageSize
Definition FunctionExtras.hpp:89
bool isTrivialCallback() const
Definition FunctionExtras.hpp:183
ReturnT(*)(void *CallableAddr, AdjustedParamT< ParamTs >... Params) CallPtrT
Definition FunctionExtras.hpp:129
static constexpr size_t InlineStorageAlign
Definition FunctionExtras.hpp:90
static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept
Definition FunctionExtras.hpp:233
unique_function(const unique_function &)=delete
R operator()(P... Params) const
Definition FunctionExtras.hpp:419
unique_function & operator=(const unique_function &)=delete
unique_function()=default
unique_function & operator=(unique_function &&)=default
unique_function(std::nullptr_t)
Definition FunctionExtras.hpp:405
unique_function(unique_function &&)=default
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< const CallableT, R, P... > *=nullptr)
Definition FunctionExtras.hpp:412
unique_function(unique_function &&)=default
unique_function & operator=(unique_function &&)=default
unique_function & operator=(const unique_function &)=delete
unique_function(const unique_function &)=delete
unique_function(CallableT Callable, detail::EnableUnlessSameType< CallableT, unique_function > *=nullptr, detail::EnableIfCallable< CallableT, R, P... > *=nullptr)
Definition FunctionExtras.hpp:386
unique_function()=default
R operator()(P... Params)
Definition FunctionExtras.hpp:393
unique_function(std::nullptr_t)
Definition FunctionExtras.hpp:379
unique_function is a type-erasing functor similar to std::function.
Definition FunctionExtras.hpp:57
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
Definition StringMap.hpp:773
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.hpp:76
UniqueFunctionBase< R, P... >::NonTrivialCallbacks UniqueFunctionBase< R, P... >::CallbacksHolder< CallableT, CalledAsT, Enable >::Callbacks
Definition FunctionExtras.hpp:361
std::enable_if_t<!std::is_same< remove_cvref_t< CallableT >, ThisT >::value > EnableUnlessSameType
Definition FunctionExtras.hpp:73
std::enable_if_t< std::is_trivially_move_constructible< T >::value && std::is_trivially_destructible< T >::value > EnableIfTrivial
Definition FunctionExtras.hpp:69
Definition raw_os_ostream.hpp:19
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.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.hpp:565
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.hpp:548
T & type
Definition FunctionExtras.hpp:121
T & type
Definition FunctionExtras.hpp:122
Definition FunctionExtras.hpp:108
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.hpp:111
static TrivialCallback Callbacks
Definition FunctionExtras.hpp:260
Definition FunctionExtras.hpp:252
static NonTrivialCallbacks Callbacks
Definition FunctionExtras.hpp:253
Definition FunctionExtras.hpp:264
Definition FunctionExtras.hpp:93
A struct we use to aggregate three callbacks when we need full set of operations.
Definition FunctionExtras.hpp:142
DestroyPtrT DestroyPtr
Definition FunctionExtras.hpp:145
MovePtrT MovePtr
Definition FunctionExtras.hpp:144
CallPtrT CallPtr
Definition FunctionExtras.hpp:143
Definition FunctionExtras.hpp:159
size_t Size
Definition FunctionExtras.hpp:161
void * StoragePtr
Definition FunctionExtras.hpp:160
size_t Alignment
Definition FunctionExtras.hpp:162
A struct to hold a single trivial callback with sufficient alignment for our bitpacking.
Definition FunctionExtras.hpp:136
CallPtrT CallPtr
Definition FunctionExtras.hpp:137
Definition FunctionExtras.hpp:156
struct wpi::util::detail::UniqueFunctionBase::StorageUnionT::OutOfLineStorageT OutOfLineStorage
std::byte InlineStorage[InlineStorageSize]
Definition FunctionExtras.hpp:173