WPILibC++ 2025.1.1
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
12/**
13 * An implementation of std::function_ref, a lightweight non-owning reference to
14 * a callable.
15 */
16template <class F>
18
19template <class R, class... Args>
20class function_ref<R(Args...)> {
21 public:
22 constexpr function_ref() noexcept = delete;
23
24 /**
25 * Creates a `function_ref` which refers to the same callable as `rhs`.
26 */
27 constexpr function_ref(const function_ref<R(Args...)>& rhs) noexcept =
28 default;
29
30 /**
31 * Constructs a `function_ref` referring to `f`.
32 */
33 template <typename F>
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
46 /**
47 * Makes `*this` refer to the same callable as `rhs`.
48 */
49 constexpr function_ref<R(Args...)>& operator=(
50 const function_ref<R(Args...)>& rhs) noexcept = default;
51
52 /**
53 * Makes `*this` refer to `f`.
54 */
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
68 /**
69 * Swaps the referred callables of `*this` and `rhs`.
70 */
71 constexpr void swap(function_ref<R(Args...)>& rhs) noexcept {
72 std::swap(obj_, rhs.obj_);
73 std::swap(callback_, rhs.callback_);
74 }
75
76 /**
77 * Call the stored callable with the given arguments.
78 */
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
88/**
89 * Swaps the referred callables of `lhs` and `rhs`.
90 */
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>
98function_ref(R (*)(Args...)) -> function_ref<R(Args...)>;
99
100} // namespace sleipnir
constexpr function_ref< R(Args...)> & operator=(F &&f) noexcept
Makes *this refer to f.
Definition FunctionRef.hpp:57
constexpr void swap(function_ref< R(Args...)> &rhs) noexcept
Swaps the referred callables of *this and rhs.
Definition FunctionRef.hpp:71
R operator()(Args... args) const
Call the stored callable with the given arguments.
Definition FunctionRef.hpp:79
constexpr function_ref< R(Args...)> & operator=(const function_ref< R(Args...)> &rhs) noexcept=default
Makes *this refer to the same callable as rhs.
constexpr function_ref() noexcept=delete
An implementation of std::function_ref, a lightweight non-owning reference to a callable.
Definition FunctionRef.hpp:17
Definition Hessian.hpp:18
constexpr void swap(function_ref< R(Args...)> &lhs, function_ref< R(Args...)> &rhs) noexcept
Swaps the referred callables of lhs and rhs.
Definition FunctionRef.hpp:92
function_ref(R(*)(Args...)) -> function_ref< R(Args...)>
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.h:5258
typename std::decay< T >::type decay_t
Definition base.h:311