Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions libraries/core/lib/morpheus/core/functional/function_ref.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#pragma once

#include <concepts>
#include <functional>
#include <morpheus/core/conformance/version.hpp>
#include <morpheus/core/meta/invocable_traits.hpp>

#include <concepts>
#include <functional>
#include <type_traits>
#include <utility>

#if (__cpp_lib_function_ref >= 202202L)

namespace morpheus::conf
{
namespace func_ref = std;
namespace fr = std;
}

#else

namespace morpheus::conf::func_ref
namespace morpheus::conf::fr
{

/// \struct nontype_t
Expand Down Expand Up @@ -98,7 +99,8 @@ class function_ref<Return(Args...)>
template <class F>
function_ref(F* f) noexcept
requires std::is_function_v<F> and isInvokableWith<F>
: mInvoke([](Storage storage, Args... args) noexcept(isNoexcept) { return invoke(function_ref::get<F>(storage), std::forward<Args>(args)...); })
: mInvoke([](Storage storage, Args... args) noexcept(isNoexcept) -> Return
{ return invoke(function_ref::get<F>(storage), std::forward<Args>(args)...); })
, mStorage(f)
{}

Expand Down Expand Up @@ -180,7 +182,7 @@ class function_ref<Return(Args...)>
template <class F>
requires std::is_function_v<F>
constexpr explicit Storage(F* f) noexcept
: fp(f)
: fp(reinterpret_cast<void (*)()>(f))
{}
};

Expand All @@ -193,7 +195,7 @@ class function_ref<Return(Args...)>
else if constexpr (std::is_object_v<T>)
return static_cast<T*>(storage.p);
else
return static_cast<T*>(storage.fp);
return reinterpret_cast<T*>(storage.fp);
}

using InternalInvocableType = Return(Storage, Args...);
Expand Down Expand Up @@ -226,5 +228,6 @@ function_ref(nontype_t<f>) -> function_ref<std::remove_pointer_t<decltype(f)>>;
function_ref(F) -> function_ref<see below>;
*/

} // namespace morpheus::conf::func_ref
} // namespace morpheus::conf::fr

#endif
6 changes: 3 additions & 3 deletions libraries/core/tests/functional/function_ref.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace morpheus::functional

TEST_CASE("Propagate constness and noexceptness to function_ref", "[morpheus.functional.function_ref]")
{
using ConcreteFunctionRef = conf::func_ref::function_ref<void()>;
using ConcreteFunctionRef = conf::fr::function_ref<void()>;
STATIC_REQUIRE(std::is_nothrow_copy_constructible_v<ConcreteFunctionRef>);
STATIC_REQUIRE(std::is_nothrow_copy_assignable_v<ConcreteFunctionRef>);
STATIC_REQUIRE(std::is_nothrow_move_constructible_v<ConcreteFunctionRef>);
Expand All @@ -27,7 +27,7 @@ TEST_CASE("Verify construction of function_ref", "[morpheus.functional.function_
{
WHEN("Constructing a function reference to the function")
{
conf::func_ref::function_ref<void()> functionView = testFunction;
conf::fr::function_ref<void()> functionView = testFunction;
THEN("Expect the function to be invocable by the function ref")
{
functionView();
Expand All @@ -45,7 +45,7 @@ TEST_CASE("Verify construction of function_ref", "[morpheus.functional.function_
WHEN("Constructing a function reference to the function")
{
TestForInvocable instance;
conf::func_ref::function_ref<void(int, int)> functionView = {conf::func_ref::nontype<&TestForInvocable::function>, instance};
conf::fr::function_ref<void(int, int)> functionView = {conf::fr::nontype<&TestForInvocable::function>, instance};
THEN("Expect the function to be invocable by the function ref")
{
functionView(0, 1);
Expand Down
Loading