Orrery
A GPU-accelerated N-body gravitational simulator
Loading...
Searching...
No Matches
function_ref.hpp
Go to the documentation of this file.
1#pragma once
2
28
29#include <functional>
30#include <memory>
31#include <type_traits>
32#include <utility>
33
34namespace orrery::core {
35
36template<typename Signature> class FunctionRef;
37
43template<typename Result, typename... Args> class FunctionRef<Result(Args...)> {
44public:
60 template<typename Callable, typename Erased = std::remove_reference_t<Callable>>
61 requires(!std::is_same_v<Erased, FunctionRef> && !std::is_const_v<Erased> &&
62 std::is_invocable_r_v<Result, Erased&, Args...>)
63 // NOLINTNEXTLINE(bugprone-forwarding-reference-overload,cppcoreguidelines-missing-std-forward)
64 constexpr FunctionRef(Callable&& callable) noexcept
65 : object_(std::addressof(callable)), invoke_(&invoke_erased<Erased>) {}
66
67 constexpr Result operator()(Args... args) const {
68 return invoke_(object_, std::forward<Args>(args)...);
69 }
70
71private:
73 template<typename Erased> static Result invoke_erased(void* object, Args... args) {
74 return std::invoke(*static_cast<Erased*>(object), std::forward<Args>(args)...);
75 }
76
77 void* object_;
78 Result (*invoke_)(void*, Args...);
79};
80
81} // namespace orrery::core
constexpr FunctionRef(Callable &&callable) noexcept
Refer to callable, which must outlive this reference.
Definition function_ref.hpp:64
Definition function_ref.hpp:36