From f514f2f49ba6b206a46b2ec19997e79111cb1277 Mon Sep 17 00:00:00 2001 From: Marcel Koch Date: Wed, 3 Apr 2024 10:48:10 +0200 Subject: [PATCH] [core] deduce constness in run --- core/base/block_operator.cpp | 5 ++-- core/base/dispatch_helper.hpp | 49 ++++++++++++++++++++++++++++++----- core/matrix/dense.cpp | 4 +-- core/matrix/permutation.cpp | 4 +-- core/matrix/row_gatherer.cpp | 6 ++--- 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/core/base/block_operator.cpp b/core/base/block_operator.cpp index 1efc5e3b152..b8190bad02d 100644 --- a/core/base/block_operator.cpp +++ b/core/base/block_operator.cpp @@ -22,9 +22,8 @@ namespace { template auto dispatch_dense(Fn&& fn, LinOp* v) { - return run*, matrix::Dense*, - matrix::Dense>*, - matrix::Dense>*>(v, std::forward(fn)); + return run, + std::complex>(v, std::forward(fn)); } diff --git a/core/base/dispatch_helper.hpp b/core/base/dispatch_helper.hpp index 35422a75fd2..8fb104c084e 100644 --- a/core/base/dispatch_helper.hpp +++ b/core/base/dispatch_helper.hpp @@ -16,6 +16,12 @@ namespace gko { namespace detail { +template +using with_same_constness_t = std::conditional_t< + std::is_const>::value, + const T, T>; + + /** * * @copydoc run -ReturnType run_impl(T obj, Func&&, Args&&...) +ReturnType run_impl(T* obj, Func&&, Args&&...) { GKO_NOT_SUPPORTED(obj); } @@ -37,9 +43,9 @@ ReturnType run_impl(T obj, Func&&, Args&&...) */ template -ReturnType run_impl(T obj, Func&& f, Args&&... args) +ReturnType run_impl(T* obj, Func&& f, Args&&... args) { - if (auto dobj = dynamic_cast(obj)) { + if (auto dobj = dynamic_cast*>(obj)) { return f(dobj, std::forward(args)...); } else { return run_impl(obj, std::forward(f), @@ -140,13 +146,16 @@ ReturnType run_impl(T obj, Func&& f, Args&&... args) */ template -auto run(T obj, Func&& f, Args&&... args) +auto run(T* obj, Func&& f, Args&&... args) { #if __cplusplus < 201703L // result_of_t is deprecated in C++17 - using ReturnType = std::result_of_t; + using ReturnType = + std::result_of_t*, Args...)>; #else - using ReturnType = std::invoke_result_t; + using ReturnType = + std::invoke_result_t*, + Args...>; #endif return detail::run_impl( obj, std::forward(f), std::forward(args)...); @@ -188,6 +197,34 @@ auto run(std::shared_ptr obj, Func&& f, Args&&... args) } +/** + * run uses template to go through the list and select the valid + * template and run it. + * + * @tparam Base the Base class with one template + * @tparam K the current template type of B. pointer of const Base is tried + * in the conversion. + * @tparam ...Types the other types will be tried in the conversion if K fails + * @tparam T the type of input object waiting converted + * @tparam Func the function will run if the object can be converted to pointer + * of const Base + * @tparam ...Args the additional arguments for the Func + * + * @param obj the input object waiting converted + * @param f the function will run if obj can be converted successfully + * @param args the additional arguments for the function + * + * @return the result of f invoked with obj cast to the first matching type + */ +template