tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
function_ref.hpp
Go to the documentation of this file.
1
2
3// SPDX-License-Identifier: BSL-1.0
4
5#ifndef TETL_FUNCTIONAL_FUNCTION_REF_HPP
6#define TETL_FUNCTIONAL_FUNCTION_REF_HPP
7
15#include <etl/_utility/swap.hpp>
16
17namespace etl {
18
19namespace detail {
20
21template <bool Noexcept, typename Signature>
22struct function_ref;
23
24template <bool Noexcept, typename R, typename... Args>
25struct function_ref<Noexcept, R(Args...)> {
26 template <typename F>
27 requires(not etl::is_same_v<decay_t<F>, function_ref> and etl::is_invocable_r_v<R, F &&, Args...>)
28 function_ref(F&& f) noexcept
29 : _obj(const_cast<void*>(reinterpret_cast<void const*>(etl::addressof(f))))
30 , _callable{+[](void* obj, Args... args) -> R {
31 auto* func = reinterpret_cast<etl::add_pointer_t<F>>(obj);
32 return etl::invoke_r<R>(*func, etl::forward<Args>(args)...);
33 }}
34 {
35 }
36
37 constexpr function_ref(function_ref const&) noexcept = default;
38 constexpr auto operator=(function_ref const&) noexcept -> function_ref& = default;
39
40 template <typename T>
41 auto operator=(T /*t*/) -> function_ref& = delete;
42
43 auto operator()(Args... args) const noexcept(Noexcept) -> R { return _callable(_obj, etl::forward<Args>(args)...); }
44
45private:
46 using internal_signature_t = R (*)(void*, Args...) noexcept(Noexcept);
47
48 void* _obj{nullptr};
49 internal_signature_t _callable{nullptr};
50};
51} // namespace detail
52
57template <typename Signature>
59
60template <typename R, typename... Args>
61struct function_ref<R(Args...)> : etl::detail::function_ref<false, R(Args...)> {
62 using etl::detail::function_ref<false, R(Args...)>::function_ref;
63};
64
65template <typename R, typename... Args>
66struct function_ref<R(Args...) noexcept> : etl::detail::function_ref<true, R(Args...)> {
67 using etl::detail::function_ref<true, R(Args...)>::function_ref;
68};
69
70template <typename R, typename... Args>
71function_ref(R (*)(Args...)) -> function_ref<R(Args...)>;
72
73} // namespace etl
74
75#endif // TETL_FUNCTIONAL_FUNCTION_REF_HPP
Definition adjacent_find.hpp:8
constexpr auto addressof(T &arg) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:15
constexpr auto invoke_r(F &&f, Args &&... args) -> R
Definition invoke_r.hpp:16
constexpr auto is_invocable_r_v
Definition is_invocable_r.hpp:15
constexpr bool is_same_v
Definition is_same.hpp:11
function_ref(R(*)(Args...)) -> function_ref< R(Args...)>
constexpr auto forward(remove_reference_t< T > &param) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18
Non-owning view of a callable.
Definition function_ref.hpp:58