CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
function_ref.h
Go to the documentation of this file.
1#ifndef CLASS_FUNCTION_REF_H
2#define CLASS_FUNCTION_REF_H
3
32#include <memory>
33#include <type_traits>
34#include <utility>
35
36template <class Signature>
37class FunctionRef;
38
39template <class R, class... Args>
40class FunctionRef<R(Args...)> {
41 public:
43 template <
44 class F,
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>) {}
47
49 template <
50 class 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...>,
54 int> = 0>
55 FunctionRef(F& callable) noexcept
56 : bound_(std::addressof(callable)), forward_(&ForwardObject<F>) {}
57
58 FunctionRef(const FunctionRef&) noexcept = default;
59 // The defaulted copy assigns the union. NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
60 FunctionRef& operator=(const FunctionRef&) noexcept = default;
61
64 template <class T,
65 std::enable_if_t<!std::is_same_v<T, FunctionRef> && !std::is_pointer_v<T>, int> = 0>
66 FunctionRef& operator=(T) = delete;
67
68 R operator()(Args... args) const {
69 return forward_(bound_, std::forward<Args>(args)...);
70 }
71
72 private:
73 /* The type erasure itself: a union holding what was erased, and the casts that restore
74 it. Converting a function pointer to void* is only conditionally supported, hence the
75 union; casting a function pointer to another function pointer type and back is well
76 defined. */
77 // NOLINTBEGIN(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-type-const-cast)
78 union Bound {
79 template <class F>
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)) {}
83
84 void* object;
85 void (*function)();
86 };
87
88 template <class F>
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)...);
93 }
94 else {
95 return callable(std::forward<Args>(args)...);
96 }
97 }
98
99 template <class F>
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)...);
104 }
105 else {
106 return function(std::forward<Args>(args)...);
107 }
108 }
109 // NOLINTEND(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-type-const-cast)
110
111 Bound bound_;
112 R (*forward_)(Bound, Args...);
113};
114
115#endif // CLASS_FUNCTION_REF_H