X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Ffunchelpers.h;h=fcd5aa477ab4c7de5a2d588e127891430a6ba3f5;hp=c56cdb94e0b7b51f1eac8f530edbbd6ae5378287;hb=0859ff9b7f8633925dde6482d57774f229a937a6;hpb=03ee70025a98912c163155955fe3cc3045f629b9 diff --git a/src/common/funchelpers.h b/src/common/funchelpers.h index c56cdb94..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 { @@ -62,3 +70,51 @@ struct FuncHelper : public FuncHelper */ template 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{}); +}