Sleipnir C++ API
Loading...
Searching...
No Matches
FunctionRef.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <functional>
6#include <memory>
7#include <type_traits>
8#include <utility>
9
10namespace sleipnir {
11
16template <class F>
18
19template <class R, class... Args>
20class function_ref<R(Args...)> {
21 public:
23
28 default;
29
34 requires(!std::is_same_v<std::decay_t<F>, function_ref> &&
35 std::is_invocable_r_v<R, F &&, Args...>)
36 constexpr function_ref(F&& f) noexcept // NOLINT(google-explicit-constructor)
37 : obj_(const_cast<void*>(
38 reinterpret_cast<const void*>(std::addressof(f)))) {
39 callback_ = [](void* obj, Args... args) -> R {
40 return std::invoke(
41 *reinterpret_cast<typename std::add_pointer<F>::type>(obj),
42 std::forward<Args>(args)...);
43 };
44 }
45
49 constexpr function_ref<R(Args...)>& operator=(
50 const function_ref<R(Args...)>& rhs) noexcept = default;
51
55 template <typename F>
56 requires std::is_invocable_r_v<R, F&&, Args...>
57 constexpr function_ref<R(Args...)>& operator=(F&& f) noexcept {
58 obj_ = reinterpret_cast<void*>(std::addressof(f));
59 callback_ = [](void* obj, Args... args) {
60 return std::invoke(
61 *reinterpret_cast<typename std::add_pointer<F>::type>(obj),
62 std::forward<Args>(args)...);
63 };
64
65 return *this;
66 }
67
71 constexpr void swap(function_ref<R(Args...)>& rhs) noexcept {
72 std::swap(obj_, rhs.obj_);
73 std::swap(callback_, rhs.callback_);
74 }
75
79 R operator()(Args... args) const {
80 return callback_(obj_, std::forward<Args>(args)...);
81 }
82
83 private:
84 void* obj_ = nullptr;
85 R (*callback_)(void*, Args...) = nullptr;
86};
87
91template <typename R, typename... Args>
92constexpr void swap(function_ref<R(Args...)>& lhs,
93 function_ref<R(Args...)>& rhs) noexcept {
94 lhs.swap(rhs);
95}
96
97template <typename R, typename... Args>
99
100} // namespace sleipnir
constexpr function_ref< R(Args...)> & operator=(F &&f) noexcept
Definition FunctionRef.hpp:57
constexpr void swap(function_ref< R(Args...)> &rhs) noexcept
Definition FunctionRef.hpp:71
R operator()(Args... args) const
Definition FunctionRef.hpp:79
constexpr function_ref< R(Args...)> & operator=(const function_ref< R(Args...)> &rhs) noexcept=default
constexpr function_ref() noexcept=delete
Definition FunctionRef.hpp:17
Definition Expression.hpp:18
IntrusiveSharedPtr< T > AllocateIntrusiveShared(Alloc alloc, Args &&... args)
Definition IntrusiveSharedPtr.hpp:275
constexpr void swap(function_ref< R(Args...)> &lhs, function_ref< R(Args...)> &rhs) noexcept
Definition FunctionRef.hpp:92