X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcommon%2Ffunchelpers.h;h=9e7057629a512aee6d0ef1d6a0627c866d817bf8;hb=e14649614fbbf9b386505a5d782b88b1ac313c1f;hp=6c7aeb6342cf9d99bed00f2706968aa41131d7a7;hpb=004c48c0d8c943f366b94651e5a75f5e568f8a57;p=quassel.git diff --git a/src/common/funchelpers.h b/src/common/funchelpers.h index 6c7aeb63..9e705762 100644 --- a/src/common/funchelpers.h +++ b/src/common/funchelpers.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2018 by the Quassel Project * + * Copyright (C) 2005-2020 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -20,8 +20,19 @@ #pragma once +#include #include #include +#include +#include + +#include + +#include +#include +#include + +// ---- Function traits -------------------------------------------------------------------------------------------------------------------- namespace detail { @@ -29,27 +40,129 @@ namespace detail { // Primary template template -struct FuncHelper : public FuncHelper {}; - -// Overload for member function with const call operator -template -struct FuncHelper : public FuncHelper {}; +struct FuncHelper : public FuncHelper +{}; -// Overload for member function with non-const call operator -template -struct FuncHelper { - using ClassType = C; +// 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 : public FuncHelper +{ + using ClassType = C; +}; + +// Overload for member function with const call operator +template +struct FuncHelper : public FuncHelper +{}; + /// @endcond -} // detail +} // namespace detail /** * Provides traits for the given callable. */ template -using MemberFunction = detail::FuncHelper; +using FunctionTraits = detail::FuncHelper; + +// ---- Invoke function with argument list ------------------------------------------------------------------------------------------------- + +namespace detail { + +// Helper for invoking the callable, wrapping its return value in a QVariant (default-constructed if callable returns void). +// The correct overload is selected via SFINAE. +template +auto invokeWithArgs(const Callable& c, Args&&... args) + -> std::enable_if_t::ReturnType>::value, QVariant> +{ + c(std::forward(args)...); + return QVariant{}; +} + +template +auto invokeWithArgs(const Callable& c, Args&&... args) + -> std::enable_if_t::ReturnType>::value, QVariant> +{ + return QVariant::fromValue(c(std::forward(args)...)); +} + +// Helper for unpacking the argument list via an index sequence +template::ArgsTuple> +boost::optional 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 boost::none; + } + } + + // Invoke callable with unmarshalled arguments + return invokeWithArgs(c, args[Is].value>>()...); +} + +} // 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 invocation is successful, the returned optional contains a QVariant with the return value, + * or an invalid QVariant if the callable returns void. + * If the conversion fails, or if the argument count does not match, this function returns boost::none + * and the callable is not invoked. + * + * @param c Callable + * @param args Arguments to be given to the callable + * @returns An optional containing a QVariant with the return value if the callable could be invoked with + * the given list of arguments; otherwise boost::none + */ +template +boost::optional 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 boost::none; + } + return detail::invokeWithArgsList(c, args, std::make_index_sequence{}); +} + +/** + * Invokes the given member function pointer on the given object with the arguments contained in the given variant list. + * + * The types contained in the given QVariantList are converted to the types expected by the member function. + * If the invocation is successful, the returned optional contains a QVariant with the return value, + * or an invalid QVariant if the member function returns void. + * If the conversion fails, or if the argument count does not match, this function returns boost::none + * and the member function is not invoked. + * + * @param c Callable + * @param args Arguments to be given to the member function + * @returns An optional containing a QVariant with the return value if the member function could be invoked with + * the given list of arguments; otherwise boost::none + */ +template +boost::optional invokeWithArgsList(C* object, R(C::*func)(Args...), const QVariantList& args) +{ + if (sizeof...(Args) != args.size()) { + qWarning().nospace() << "Argument count mismatch! Expected: " << sizeof...(Args) << ", actual: " << args.size(); + return boost::none; + } + return detail::invokeWithArgsList([object, func](Args&&... args) { + return (object->*func)(std::forward(args)...); + }, args, std::make_index_sequence{}); +}