X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Ffunchelpers.h;h=fcd5aa477ab4c7de5a2d588e127891430a6ba3f5;hp=01ee8222807796e3f426afa4c031402aa7098ea1;hb=0859ff9b7f8633925dde6482d57774f229a937a6;hpb=c1cf157116de7fc3da96203aa6f03c38c7ebb650 diff --git a/src/common/funchelpers.h b/src/common/funchelpers.h index 01ee8222..fcd5aa47 100644 --- a/src/common/funchelpers.h +++ b/src/common/funchelpers.h @@ -20,8 +20,16 @@ #pragma once +#include #include #include +#include +#include + +#include +#include + +// ---- Function traits -------------------------------------------------------------------------------------------------------------------- namespace detail { @@ -32,21 +40,27 @@ template struct FuncHelper : public FuncHelper {}; -// Overload for member function with const call operator -template -struct FuncHelper : public FuncHelper -{}; +// Overload for free function +template +struct FuncHelper +{ + using FunctionType = std::function; + using ReturnType = R; + using ArgsTuple = std::tuple; +}; // Overload for member function with non-const call operator template -struct FuncHelper +struct FuncHelper : public FuncHelper { using ClassType = C; - using FunctionType = std::function; - using ReturnType = R; - using ArgsTuple = std::tuple; }; +// Overload for member function with const call operator +template +struct FuncHelper : public FuncHelper +{}; + /// @endcond } // namespace detail @@ -55,4 +69,52 @@ struct FuncHelper * Provides traits for the given callable. */ template -using MemberFunction = detail::FuncHelper; +using FunctionTraits = detail::FuncHelper; + +// ---- Invoke function with argument list ------------------------------------------------------------------------------------------------- + +namespace detail { + +// Helper for unpacking the argument list via an index sequence +template::ArgsTuple> +bool invokeWithArgsList(const Callable& c, const QVariantList& args, std::index_sequence) +{ + // Sanity check that all types can be converted + std::array::value> convertible{{args[Is].canConvert>>()...}}; + for (size_t i = 0; i < convertible.size(); ++i) { + if (!convertible[i]) { + qWarning() << "Cannot convert parameter" << i << "from type" << args[static_cast(i)].typeName() << "to expected argument type"; + return false; + } + } + + // Invoke callable + c(args[Is].value>>()...); + return true; +} + +} // detail + +/** + * Invokes the given callable with the arguments contained in the given variant list. + * + * The types contained in the given QVariantList are converted to the types expected by the callable. + * If the conversion fails, or if the argument count does not match, this function returns false and + * the callable is not invoked. + * + * @param c Callable + * @param args Arguments to be given to the callable + * @returns true if the callable could be invoked with the given list of arguments + */ +template +bool invokeWithArgsList(const Callable& c, const QVariantList& args) +{ + using ArgsTuple = typename FunctionTraits::ArgsTuple; + constexpr auto tupleSize = std::tuple_size::value; + + if (tupleSize != args.size()) { + qWarning().nospace() << "Argument count mismatch! Expected: " << tupleSize << ", actual: " << args.size(); + return false; + } + return detail::invokeWithArgsList(c, args, std::make_index_sequence{}); +}