|
| 1 | +// Copyright (c) 2025 Vector 35 Inc |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to |
| 5 | +// deal in the Software without restriction, including without limitation the |
| 6 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +// IN THE SOFTWARE. |
| 20 | + |
| 21 | +#pragma once |
| 22 | + |
| 23 | +#include <type_traits> |
| 24 | +#include <utility> |
| 25 | +#include <functional> |
| 26 | + |
| 27 | +namespace bn::base { |
| 28 | + |
| 29 | +template <typename Sig> |
| 30 | +class function_ref; |
| 31 | + |
| 32 | +// A non-owning reference to a callable object, inspired by C++26's std::function_ref. |
| 33 | +// If the callable needs to be stored or copied, use std::function instead. |
| 34 | +template <typename R, typename... Args> |
| 35 | +class function_ref<R(Args...)> |
| 36 | +{ |
| 37 | +private: |
| 38 | + union Storage |
| 39 | + { |
| 40 | + const void* object = nullptr; |
| 41 | + R (*func)(Args...); |
| 42 | + }; |
| 43 | + |
| 44 | + Storage m_storage; |
| 45 | + R (*m_invoker)(const Storage&, Args...) = nullptr; |
| 46 | + |
| 47 | + static R invoke_function(const Storage& storage, Args... args) |
| 48 | + { |
| 49 | + auto fn = storage.func; |
| 50 | + return fn(std::forward<Args>(args)...); |
| 51 | + } |
| 52 | + |
| 53 | + template <typename T> |
| 54 | + static R invoke_callable(const Storage& storage, Args... args) |
| 55 | + { |
| 56 | + return std::invoke(*static_cast<const T*>(storage.object), std::forward<Args>(args)...); |
| 57 | + } |
| 58 | + |
| 59 | +public: |
| 60 | + function_ref() = delete; |
| 61 | + |
| 62 | + // Constructor that accepts a function pointer |
| 63 | + function_ref(R (*f)(Args...)) noexcept |
| 64 | + : m_storage{.func = f} |
| 65 | + , m_invoker(&invoke_function) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + // Constructor that accepts any callable object that is not FunctionRef |
| 70 | + template<typename F> |
| 71 | + requires (!std::is_same_v<std::remove_cvref_t<F>, function_ref>) && |
| 72 | + std::is_invocable_r_v<R, F&, Args...> |
| 73 | + function_ref(const F& f) noexcept |
| 74 | + : m_storage{.object = &f} |
| 75 | + , m_invoker(&invoke_callable<std::remove_cvref_t<F>>) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + R operator()(Args... args) const |
| 80 | + { |
| 81 | + return m_invoker(m_storage, std::forward<Args>(args)...); |
| 82 | + } |
| 83 | + |
| 84 | + function_ref(const function_ref&) noexcept = default; |
| 85 | + function_ref(function_ref&&) noexcept = default; |
| 86 | + function_ref& operator=(const function_ref&) noexcept = default; |
| 87 | + function_ref& operator=(function_ref&&) noexcept = default; |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace bn::base |
0 commit comments