X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Ffunchelpers.h;h=ede4e43e1332ba405ffa28d13d7d1c0809ef1149;hp=fcd5aa477ab4c7de5a2d588e127891430a6ba3f5;hb=a22e08480288685d73d9abd18c6a1087451c388b;hpb=b7936062b08a743e6dec917ac5e727e7b2cd3bb8 diff --git a/src/common/funchelpers.h b/src/common/funchelpers.h index fcd5aa47..ede4e43e 100644 --- a/src/common/funchelpers.h +++ b/src/common/funchelpers.h @@ -26,7 +26,10 @@ #include #include +#include + #include +#include #include // ---- Function traits -------------------------------------------------------------------------------------------------------------------- @@ -75,22 +78,38 @@ using FunctionTraits = detail::FuncHelper; 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> -bool invokeWithArgsList(const Callable& c, const QVariantList& args, std::index_sequence) +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 false; + return boost::none; } } - // Invoke callable - c(args[Is].value>>()...); - return true; + // Invoke callable with unmarshalled arguments + return invokeWithArgs(c, args[Is].value>>()...); } } // detail @@ -99,22 +118,25 @@ bool invokeWithArgsList(const Callable& c, const QVariantList& args, std::index_ * 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. + * 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 true if the callable could be invoked with the given list of arguments + * @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 -bool invokeWithArgsList(const Callable& c, const QVariantList& args) +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 false; + return boost::none; } return detail::invokeWithArgsList(c, args, std::make_index_sequence{}); }