WPILibC++ 2024.3.2
FunctionExtras.h
Go to the documentation of this file.
1//===- FunctionExtras.h - Function type erasure utilities -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file provides a collection of function (or more generally, callable)
10/// type erasure utilities supplementing those provided by the standard library
11/// in `<function>`.
12///
13/// It provides `unique_function`, which works like `std::function` but supports
14/// move-only callable objects and const-qualification.
15///
16/// Future plans:
17/// - Add a `function` that provides ref-qualified support, which doesn't work
18/// with `std::function`.
19/// - Provide support for specifying multiple signatures to type erase callable
20/// objects with an overload set, such as those produced by generic lambdas.
21/// - Expand to include a copyable utility that directly replaces std::function
22/// but brings the above improvements.
23///
24/// Note that LLVM's utilities are greatly simplified by not supporting
25/// allocators.
26///
27/// If the standard library ever begins to provide comparable facilities we can
28/// consider switching to those.
29///
30//===----------------------------------------------------------------------===//
31
32#ifndef WPIUTIL_WPI_FUNCTIONEXTRAS_H
33#define WPIUTIL_WPI_FUNCTIONEXTRAS_H
34
35#include "wpi/PointerIntPair.h"
36#include "wpi/PointerUnion.h"
38#include "wpi/MemAlloc.h"
39#include "wpi/type_traits.h"
40#include <cstddef>
41#include <cstring>
42#include <memory>
43#include <type_traits>
44
45namespace wpi {
46
47/// unique_function is a type-erasing functor similar to std::function.
48///
49/// It can hold move-only function objects, like lambdas capturing unique_ptrs.
50/// Accordingly, it is movable but not copyable.
51///
52/// It supports const-qualification:
53/// - unique_function<int() const> has a const operator().
54/// It can only hold functions which themselves have a const operator().
55/// - unique_function<int()> has a non-const operator().
56/// It can hold functions with a non-const operator(), like mutable lambdas.
57template <typename FunctionT> class unique_function;
58
59// GCC warns on OutOfLineStorage
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"
64#endif
65
66namespace detail {
67
68template <typename T>
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>
76using EnableIfCallable = std::enable_if_t<std::disjunction<
77 std::is_void<Ret>,
78 std::is_same<decltype(std::declval<CallableT>()(std::declval<Params>()...)),
79 Ret>,
80 std::is_same<const decltype(std::declval<CallableT>()(
81 std::declval<Params>()...)),
82 Ret>,
83 std::is_convertible<decltype(std::declval<CallableT>()(
84 std::declval<Params>()...)),
85 Ret>>::value>;
86
87template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
88protected:
89 static constexpr size_t InlineStorageSize = sizeof(void *) * 4;
90
91 template <typename T, class = void>
92 struct IsSizeLessThanThresholdT : std::false_type {};
93
94 template <typename T>
96 T, std::enable_if_t<sizeof(T) <= 2 * sizeof(void *)>> : std::true_type {};
97
98 // Provide a type function to map parameters that won't observe extra copies
99 // or moves and which are small enough to likely pass in register to values
100 // and all other types to l-value reference types. We use this to compute the
101 // types used in our erased call utility to minimize copies and moves unless
102 // doing so would force things unnecessarily into memory.
103 //
104 // The heuristic used is related to common ABI register passing conventions.
105 // It doesn't have to be exact though, and in one way it is more strict
106 // because we want to still be able to observe either moves *or* copies.
107 template <typename T> struct AdjustedParamTBase {
108 static_assert(!std::is_reference<T>::value,
109 "references should be handled by template specialization");
110 using type =
111 std::conditional_t<std::is_trivially_copy_constructible<T>::value &&
112 std::is_trivially_move_constructible<T>::value &&
113 IsSizeLessThanThresholdT<T>::value,
114 T, T &>;
115 };
116
117 // This specialization ensures that 'AdjustedParam<V<T>&>' or
118 // 'AdjustedParam<V<T>&&>' does not trigger a compile-time error when 'T' is
119 // an incomplete type and V a templated type.
120 template <typename T> struct AdjustedParamTBase<T &> { using type = T &; };
121 template <typename T> struct AdjustedParamTBase<T &&> { using type = T &; };
122
123 template <typename T>
124 using AdjustedParamT = typename AdjustedParamTBase<T>::type;
125
126 // The type of the erased function pointer we use as a callback to dispatch to
127 // the stored callable when it is trivial to move and destroy.
128 using CallPtrT = ReturnT (*)(void *CallableAddr,
129 AdjustedParamT<ParamTs>... Params);
130 using MovePtrT = void (*)(void *LHSCallableAddr, void *RHSCallableAddr);
131 using DestroyPtrT = void (*)(void *CallableAddr);
132
133 /// A struct to hold a single trivial callback with sufficient alignment for
134 /// our bitpacking.
135 struct alignas(8) TrivialCallback {
136 CallPtrT CallPtr;
137 };
138
139 /// A struct we use to aggregate three callbacks when we need full set of
140 /// operations.
141 struct alignas(8) NonTrivialCallbacks {
142 CallPtrT CallPtr;
143 MovePtrT MovePtr;
144 DestroyPtrT DestroyPtr;
145 };
146
147 // Create a pointer union between either a pointer to a static trivial call
148 // pointer in a struct or a pointer to a static struct of the call, move, and
149 // destroy pointers.
150 using CallbackPointerUnionT =
151 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
152
153 // The main storage buffer. This will either have a pointer to out-of-line
154 // storage or an inline buffer storing the callable.
155 union StorageUnionT {
156 // For out-of-line storage we keep a pointer to the underlying storage and
157 // the size. This is enough to deallocate the memory.
158 struct OutOfLineStorageT {
159 void *StoragePtr;
160 size_t Size;
161 size_t Alignment;
162 } OutOfLineStorage;
163 static_assert(
164 sizeof(OutOfLineStorageT) <= InlineStorageSize,
165 "Should always use all of the out-of-line storage for inline storage!");
166
167 // For in-line storage, we just provide an aligned character buffer. We
168 // provide four pointers worth of storage here.
169 // This is mutable as an inlined `const unique_function<void() const>` may
170 // still modify its own mutable members.
171 alignas(void*) mutable std::byte InlineStorage[InlineStorageSize];
172 } StorageUnion;
173
174 // A compressed pointer to either our dispatching callback or our table of
175 // dispatching callbacks and the flag for whether the callable itself is
176 // stored inline or not.
177 PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag;
178
179 bool isInlineStorage() const { return CallbackAndInlineFlag.getInt(); }
180
181 bool isTrivialCallback() const {
182 return isa<TrivialCallback *>(CallbackAndInlineFlag.getPointer());
183 }
184
185 CallPtrT getTrivialCallback() const {
186 return cast<TrivialCallback *>(CallbackAndInlineFlag.getPointer())->CallPtr;
187 }
188
189 NonTrivialCallbacks *getNonTrivialCallbacks() const {
190 return cast<NonTrivialCallbacks *>(CallbackAndInlineFlag.getPointer());
191 }
192
193 CallPtrT getCallPtr() const {
194 return isTrivialCallback() ? getTrivialCallback()
195 : getNonTrivialCallbacks()->CallPtr;
196 }
197
198 // These three functions are only const in the narrow sense. They return
199 // mutable pointers to function state.
200 // This allows unique_function<T const>::operator() to be const, even if the
201 // underlying functor may be internally mutable.
202 //
203 // const callers must ensure they're only used in const-correct ways.
204 void *getCalleePtr() const {
205 return isInlineStorage() ? getInlineStorage() : getOutOfLineStorage();
206 }
207 void *getInlineStorage() const { return &StorageUnion.InlineStorage; }
208 void *getOutOfLineStorage() const {
209 return StorageUnion.OutOfLineStorage.StoragePtr;
210 }
211
212 size_t getOutOfLineStorageSize() const {
213 return StorageUnion.OutOfLineStorage.Size;
214 }
216 return StorageUnion.OutOfLineStorage.Alignment;
217 }
218
219 void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment) {
220 StorageUnion.OutOfLineStorage = {Ptr, Size, Alignment};
221 }
222
223 template <typename CalledAsT>
224 static ReturnT CallImpl(void *CallableAddr,
225 AdjustedParamT<ParamTs>... Params) {
226 auto &Func = *reinterpret_cast<CalledAsT *>(CallableAddr);
227 return Func(std::forward<ParamTs>(Params)...);
228 }
229
230 template <typename CallableT>
231 static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept {
232 new (LHSCallableAddr)
233 CallableT(std::move(*reinterpret_cast<CallableT *>(RHSCallableAddr)));
234 }
235
236 template <typename CallableT>
237 static void DestroyImpl(void *CallableAddr) noexcept {
238 reinterpret_cast<CallableT *>(CallableAddr)->~CallableT();
239 }
240
241 // The pointers to call/move/destroy functions are determined for each
242 // callable type (and called-as type, which determines the overload chosen).
243 // (definitions are out-of-line).
244
245 // By default, we need an object that contains all the different
246 // type erased behaviors needed. Create a static instance of the struct type
247 // here and each instance will contain a pointer to it.
248 // Wrap in a struct to avoid https://gcc.gnu.org/PR71954
249 template <typename CallableT, typename CalledAs, typename Enable = void>
251 static NonTrivialCallbacks Callbacks;
252 };
253 // See if we can create a trivial callback. We need the callable to be
254 // trivially moved and trivially destroyed so that we don't have to store
255 // type erased callbacks for those operations.
256 template <typename CallableT, typename CalledAs>
257 struct CallbacksHolder<CallableT, CalledAs, EnableIfTrivial<CallableT>> {
258 static TrivialCallback Callbacks;
259 };
260
261 // A simple tag type so the call-as type to be passed to the constructor.
262 template <typename T> struct CalledAs {};
263
264 // Essentially the "main" unique_function constructor, but subclasses
265 // provide the qualified type to be used for the call.
266 // (We always store a T, even if the call will use a pointer to const T).
267 template <typename CallableT, typename CalledAsT>
269 bool IsInlineStorage = true;
270 void *CallableAddr = getInlineStorage();
271 if (sizeof(CallableT) > InlineStorageSize ||
272 alignof(CallableT) > alignof(decltype(StorageUnion.InlineStorage))) {
273 IsInlineStorage = false;
274 // Allocate out-of-line storage. FIXME: Use an explicit alignment
275 // parameter in C++17 mode.
276 auto Size = sizeof(CallableT);
277 auto Alignment = alignof(CallableT);
278 CallableAddr = allocate_buffer(Size, Alignment);
279 setOutOfLineStorage(CallableAddr, Size, Alignment);
280 }
281
282 // Now move into the storage.
283 new (CallableAddr) CallableT(std::move(Callable));
284 CallbackAndInlineFlag.setPointerAndInt(
286 }
287
289 if (!CallbackAndInlineFlag.getPointer())
290 return;
291
292 // Cache this value so we don't re-check it after type-erased operations.
293 bool IsInlineStorage = isInlineStorage();
294
295 if (!isTrivialCallback())
296 getNonTrivialCallbacks()->DestroyPtr(
297 IsInlineStorage ? getInlineStorage() : getOutOfLineStorage());
298
299 if (!IsInlineStorage)
302 }
303
305 // Copy the callback and inline flag.
306 CallbackAndInlineFlag = RHS.CallbackAndInlineFlag;
307
308 // If the RHS is empty, just copying the above is sufficient.
309 if (!RHS)
310 return;
311
312 if (!isInlineStorage()) {
313 // The out-of-line case is easiest to move.
314 StorageUnion.OutOfLineStorage = RHS.StorageUnion.OutOfLineStorage;
315 } else if (isTrivialCallback()) {
316 // Move is trivial, just memcpy the bytes across.
317 memcpy(getInlineStorage(), RHS.getInlineStorage(), InlineStorageSize);
318 } else {
319 // Non-trivial move, so dispatch to a type-erased implementation.
321 RHS.getInlineStorage());
322 }
323
324 // Clear the old callback and inline flag to get back to as-if-null.
325 RHS.CallbackAndInlineFlag = {};
326
327#ifndef NDEBUG
328 // In debug builds, we also scribble across the rest of the storage.
329 memset(RHS.getInlineStorage(), 0xAD, InlineStorageSize);
330#endif
331 }
332
334 if (this == &RHS)
335 return *this;
336
337 // Because we don't try to provide any exception safety guarantees we can
338 // implement move assignment very simply by first destroying the current
339 // object and then move-constructing over top of it.
340 this->~UniqueFunctionBase();
341 new (this) UniqueFunctionBase(std::move(RHS));
342 return *this;
343 }
344
346
347public:
348 explicit operator bool() const {
349 return (bool)CallbackAndInlineFlag.getPointer();
350 }
351};
352
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>};
358
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>};
365
366} // namespace detail
367
368template <typename R, typename... P>
369class unique_function<R(P...)> : public detail::UniqueFunctionBase<R, P...> {
370 using Base = detail::UniqueFunctionBase<R, P...>;
371
372public:
373 unique_function() = default;
374 unique_function(std::nullptr_t) {}
379
380 template <typename CallableT>
382 CallableT Callable,
385 : Base(std::forward<CallableT>(Callable),
386 typename Base::template CalledAs<CallableT>{}) {}
387
388 R operator()(P... Params) {
389 return this->getCallPtr()(this->getCalleePtr(), Params...);
390 }
391};
392
393template <typename R, typename... P>
394class unique_function<R(P...) const>
395 : public detail::UniqueFunctionBase<R, P...> {
396 using Base = detail::UniqueFunctionBase<R, P...>;
397
398public:
399 unique_function() = default;
400 unique_function(std::nullptr_t) {}
405
406 template <typename CallableT>
408 CallableT Callable,
411 : Base(std::forward<CallableT>(Callable),
412 typename Base::template CalledAs<const CallableT>{}) {}
413
414 R operator()(P... Params) const {
415 return this->getCallPtr()(this->getCalleePtr(), Params...);
416 }
417};
418
419#if defined(__GNUC__) && !defined(__clang__)
420#pragma GCC diagnostic pop
421#endif
422
423} // end namespace wpi
424
425#endif // WPIUTIL_WPI_FUNCTIONEXTRAS_H
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(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(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
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
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: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.
Definition: FunctionExtras.h:250
static NonTrivialCallbacks Callbacks
Definition: FunctionExtras.h:251
Definition: FunctionExtras.h:262