5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_FUNCTION_REF_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_FUNCTIONAL_FUNCTION_REF_HPP_
16#include "arene/base/stdlib_choice/addressof.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/forward.hpp"
19#include "arene/base/stdlib_choice/ignore.hpp"
20#include "arene/base/stdlib_choice/is_function.hpp"
21#include "arene/base/stdlib_choice/is_fundamental.hpp"
22#include "arene/base/stdlib_choice/is_same.hpp"
23#include "arene/base/stdlib_choice/is_trivially_copyable.hpp"
24#include "arene/base/stdlib_choice/is_void.hpp"
25#include "arene/base/stdlib_choice/max_value_overload.hpp"
26#include "arene/base/stdlib_choice/memcpy.hpp"
27#include "arene/base/stdlib_choice/remove_cv.hpp"
28#include "arene/base/stdlib_choice/remove_reference.hpp"
46template <
typename Signature,
bool IsNoexcept = false>
51template <
typename Signature>
54namespace function_ref_detail {
60template <
typename Func>
61auto checked_deref(Func* ptr)
noexcept -> Func& {
82template <
bool IsConst,
bool IsNoexcept,
typename R,
typename... Args>
83class function_ref_base {
89 using call_type = R (*)(storage_buffer, arg_passing_type<Args>...);
91 static_assert(
sizeof(call_type) ==
sizeof(void (*)()),
"Function pointers must have the same size");
108 typename ReturnType = R,
110 static auto invoke_wrapped_callable(storage_buffer buffer, arg_passing_type<Args>... args) -> R {
116 return static_cast<R
>((*local_f)(
static_cast<Args&&
>(args)...));
128 typename ReturnType = R,
130 static void invoke_wrapped_callable(storage_buffer buffer, arg_passing_type<Args>... args) {
135 static_cast<void>((*local_f)(
static_cast<Args&&
>(args)...));
142 template <
typename Po
inteeType,
typename CallType>
144 : call_(&invoke_wrapped_callable<CallType>) {
155 template <
typename F>
156 static constexpr bool
164 auto operator()(Args... args)
const noexcept(IsNoexcept) -> R {
166 return call_(buffer_, args...);
191 function_ref_base(F&& func) noexcept
213 function_ref_base(F* func) noexcept
214 : function_ref_base(function_ref_detail::checked_deref(func),
type_identity<F>{}) {}
219 function_ref_base(function_ref_base
const& other) =
default;
222 function_ref_base(function_ref_base&& other) =
default;
226 ~function_ref_base() =
default;
230 auto operator=(function_ref_base
const& other) -> function_ref_base& =
default;
233 auto operator=(function_ref_base&& other) -> function_ref_base& =
default;
237 alignas(
void*)
alignas(call_type) storage_buffer buffer_{};
251template <
typename R,
typename... Args,
bool IsNoexcept>
252class function_ref<R(Args...), IsNoexcept> : function_ref_detail::function_ref_base<false, IsNoexcept, R, Args...> {
254 using base_class = function_ref_detail::function_ref_base<
false, IsNoexcept, R, Args...>;
258 using base_class::base_class;
260 using base_class::operator();
269template <
typename R,
typename... Args,
bool IsNoexcept>
271 : function_ref_detail::function_ref_base<true, IsNoexcept, R, Args...> {
273 using base_class = function_ref_detail::function_ref_base<
true, IsNoexcept, R, Args...>;
277 using base_class::base_class;
279 using base_class::operator();
A fixed-size array of T objects.
Definition array.hpp:77
An implementation of the proposed std::function_ref from P0792. Provides a type-erased callable like ...
Definition function_ref.hpp:47
Polyfill of std::type_identity added in C++20.
Definition type_identity.hpp:28
static constexpr value_type value
Definition integral_constant.hpp:34
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
typename static_if< B >::template then_else< T, F > conditional_t
lazily select between two types based on a boolean condition
Definition conditional.hpp:28
constexpr bool is_invocable_r_v
Determine if a functor type is invocable with the specified return and argument types.
constexpr bool is_nothrow_invocable_r_v
Determine if a functor type is no-throw invocable with the specified return and argument types.
function_ref< Signature, true > noexcept_function_ref
A convience alias for function_ref with IsNoexcept set true.
Definition function_ref.hpp:52
enum ARENE_MAY_ALIAS byte
A type that represents a byte of data.
Definition byte.hpp:39
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
constexpr auto max(initializer_list< T > list, Compare comp) noexcept(noexcept(comp(std::declval< T & >(), std::declval< T & >())) &&std::is_nothrow_copy_constructible_v< T >) -> T
Returns the largest value in the std::initializer_list.
Definition max_initializer_list_overload.hpp:49
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto memcpy(void *dest, void const *src, std::size_t count) -> void *
copies one buffer to another
Definition memcpy.hpp:29
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
constexpr auto addressof(T &value) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:24
typename remove_reference< T >::type remove_reference_t
An alias for T, but with top-level reference qualifiers removed.
Definition remove_reference.hpp:47
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24