ThreadSchedule 4.1.0
Modern C++ thread management library
Loading...
Searching...
No Matches
function_ref.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "../lifetime.hpp"
10
11#include <functional>
12#include <memory>
13#include <type_traits>
14#include <utility>
15
17{
18
19template <typename Signature>
21
22template <typename R, typename... Args>
24{
25public:
26 function_ref() = delete;
27
28 function_ref(R (*fn)(Args...)) noexcept : function_(fn)
29 {
30 callback_ = [](void*, R (*function)(Args...), Args... args) -> R
31 {
32 if constexpr (std::is_void_v<R>)
33 {
34 function(std::forward<Args>(args)...);
35 return;
36 }
37 else
38 {
39 return function(std::forward<Args>(args)...);
40 }
41 };
42 }
43
44 template <typename F, typename = std::enable_if_t<!std::is_same_v<remove_cvref_t<F>, function_ref>
45 && std::is_invocable_r_v<R, F&, Args...>>>
47 : object_(const_cast<void*>(static_cast<void const*>(std::addressof(fn))))
48 {
49 callback_ = [](void* object, R (*)(Args...), Args... args) -> R
50 {
51 auto& callable = *static_cast<remove_cvref_t<F>*>(object);
52 if constexpr (std::is_void_v<R>)
53 {
54 callable(std::forward<Args>(args)...);
55 return;
56 }
57 else
58 {
59 return callable(std::forward<Args>(args)...);
60 }
61 };
62 }
63
64 auto
65 operator()(Args... args) const -> R
66 {
67 if constexpr (std::is_void_v<R>)
68 {
69 callback_(object_, function_, std::forward<Args>(args)...);
70 return;
71 }
72 else
73 {
74 return callback_(object_, function_, std::forward<Args>(args)...);
75 }
76 }
77
78private:
79 void* object_ = nullptr;
80 R (*function_)(Args...) = nullptr;
81 R (*callback_)(void*, R (*)(Args...), Args...);
82};
83
84} // namespace threadschedule::detail
function_ref(F &&fn THREADSCHEDULE_LIFETIMEBOUND) noexcept
Optional compiler diagnostics for borrowed references and pointers.
#define THREADSCHEDULE_LIFETIMEBOUND
Definition lifetime.hpp:28
#define THREADSCHEDULE_POINTER
Definition lifetime.hpp:32
Aggregates multiple thread_registry_backend instances into a single queryable view.
class THREADSCHEDULE_POINTER function_ref
std::remove_cv_t< std::remove_reference_t< T > > remove_cvref_t