1#ifndef CLASS_FUNCTION_REF_H
2#define CLASS_FUNCTION_REF_H
36template <
class Signature>
39template <
class R,
class... Args>
40class FunctionRef<R(Args...)> {
45 std::enable_if_t<std::is_function_v<F> && std::is_invocable_r_v<R, F&, Args...>,
int> = 0>
46 FunctionRef(F* function) noexcept : bound_(function), forward_(&ForwardFunction<F>) {}
51 std::enable_if_t<!std::is_same_v<std::remove_cv_t<F>, FunctionRef> &&
52 !std::is_function_v<F> && !std::is_pointer_v<F> &&
53 !std::is_member_pointer_v<F> && std::is_invocable_r_v<R, F&, Args...>,
55 FunctionRef(F& callable) noexcept
56 : bound_(std::addressof(callable)), forward_(&ForwardObject<F>) {}
58 FunctionRef(
const FunctionRef&)
noexcept =
default;
60 FunctionRef& operator=(
const FunctionRef&)
noexcept =
default;
65 std::enable_if_t<!std::is_same_v<T, FunctionRef> && !std::is_pointer_v<T>,
int> = 0>
66 FunctionRef& operator=(T) =
delete;
68 R operator()(Args... args)
const {
69 return forward_(bound_, std::forward<Args>(args)...);
80 explicit Bound(F* callable) noexcept : object(
const_cast<std::remove_cv_t<F>*
>(callable)) {}
81 template <
class F,
class... A>
82 explicit Bound(F (*fn)(A...)) noexcept : function(reinterpret_cast<
void (*)()>(fn)) {}
89 static R ForwardObject(Bound bound, Args... args) {
90 F& callable = *
static_cast<F*
>(bound.object);
91 if constexpr (std::is_void_v<R>) {
92 callable(std::forward<Args>(args)...);
95 return callable(std::forward<Args>(args)...);
100 static R ForwardFunction(Bound bound, Args... args) {
101 F* function =
reinterpret_cast<F*
>(bound.function);
102 if constexpr (std::is_void_v<R>) {
103 function(std::forward<Args>(args)...);
106 return function(std::forward<Args>(args)...);
112 R (*forward_)(Bound, Args...);