aboutsummaryrefslogtreecommitdiff
path: root/src/util/function.tpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-05 13:26:55 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:54 +0200
commit6119f1428b3615e4738a121c6acf343942107fd9 (patch)
tree47eb5537674fe58ae8ad388521203b92bfb43529 /src/util/function.tpp
parentd27f1a89266e141a944f88e9080bdeca95c49da3 (diff)
refactor: move input config to own file & improve cleanup
Diffstat (limited to 'src/util/function.tpp')
-rw-r--r--src/util/function.tpp75
1 files changed, 75 insertions, 0 deletions
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 <utility>
+
+template <typename>
+struct remove_cv_seq;
+
+template <typename Return, typename... Args>
+struct remove_cv_seq<Return(Args...)>
+{
+ using type = Return(Args...);
+};
+
+template <typename Return, typename... Args>
+struct remove_cv_seq<Return(Args...) const>
+{
+ using type = Return(Args...);
+};
+
+template <typename Return, typename... Args>
+struct remove_cv_seq<Return(Args...) &>
+{
+ using type = Return(Args...);
+};
+
+template <typename Function>
+constexpr inline auto extract_signature(Function * /*unused*/) noexcept
+{
+ return Signature<typename remove_cv_seq<Function>::type>();
+}
+
+template <typename Class, typename Function>
+constexpr inline auto extract_signature(Function Class::* /*unused*/) noexcept
+{
+ return Signature<typename remove_cv_seq<Function>::type>();
+}
+
+template <typename Function>
+constexpr inline auto extract_signature(Function const & /*unused*/) noexcept
+ -> decltype(&Function::operator(), extract_signature(&Function::operator()))
+{
+ return extract_signature(&Function::operator());
+}
+
+template <typename Function, typename Return, typename... Args>
+inline auto get_normalized_lambda(Function &&func,
+ Signature<Return(Args...)> /*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<Function>(func));
+
+ return +[](Args... args) noexcept(
+ noexcept(std::declval<Function>()(std::forward<Args>(args)...))) -> Return
+ {
+ return static_func(std::forward<Args>(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 <typename Function>
+constexpr auto normalize_lambda(Function &&func) noexcept
+{
+ return get_normalized_lambda(std::forward<Function>(func),
+ extract_signature(std::forward<Function>(func)));
+}