From 6119f1428b3615e4738a121c6acf343942107fd9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 5 Mar 2022 13:26:55 +0100 Subject: refactor: move input config to own file & improve cleanup --- src/util/function.hpp | 11 ++++++++ src/util/function.tpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/util/function.hpp create mode 100644 src/util/function.tpp (limited to 'src/util') diff --git a/src/util/function.hpp b/src/util/function.hpp new file mode 100644 index 0000000..13f5356 --- /dev/null +++ b/src/util/function.hpp @@ -0,0 +1,11 @@ +#pragma once + +template +struct Signature +{ +}; + +template +constexpr auto normalize_lambda(Function &&func) noexcept; + +#include "function.tpp" diff --git a/src/util/function.tpp b/src/util/function.tpp new file mode 100644 index 0000000..6ff939b --- /dev/null +++ b/src/util/function.tpp @@ -0,0 +1,75 @@ +#pragma once + +#include "function.hpp" + +#include + +template +struct remove_cv_seq; + +template +struct remove_cv_seq +{ + using type = Return(Args...); +}; + +template +struct remove_cv_seq +{ + using type = Return(Args...); +}; + +template +struct remove_cv_seq +{ + using type = Return(Args...); +}; + +template +constexpr inline auto extract_signature(Function * /*unused*/) noexcept +{ + return Signature::type>(); +} + +template +constexpr inline auto extract_signature(Function Class::* /*unused*/) noexcept +{ + return Signature::type>(); +} + +template +constexpr inline auto extract_signature(Function const & /*unused*/) noexcept + -> decltype(&Function::operator(), extract_signature(&Function::operator())) +{ + return extract_signature(&Function::operator()); +} + +template +inline auto get_normalized_lambda(Function &&func, + Signature /*unused*/) noexcept +{ + // Static copy of func. This will make it accessible for the lambda without using a + // lamda capture + static auto static_func = Function(std::forward(func)); + + return +[](Args... args) noexcept( + noexcept(std::declval()(std::forward(args)...))) -> Return + { + return static_func(std::forward(args)...); + }; +} + +/** + * Normalizes the type signature of a lambda function. + * + * This will make a lambda with captures passable to other functions. + * + * @param func A lambda function + * @returns The lambda function normalized. + */ +template +constexpr auto normalize_lambda(Function &&func) noexcept +{ + return get_normalized_lambda(std::forward(func), + extract_signature(std::forward(func))); +} -- cgit v1.2.3-18-g5258