X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcommon%2Ffunchelpers.h;h=ed9999e3d4e22b10cb14f7180d116d36fd4c4c37;hb=45645a28bdc5b6c1052b3e4cebf8cc80832d205a;hp=ede4e43e1332ba405ffa28d13d7d1c0809ef1149;hpb=a22e08480288685d73d9abd18c6a1087451c388b;p=quassel.git diff --git a/src/common/funchelpers.h b/src/common/funchelpers.h index ede4e43e..ed9999e3 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-2019 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -140,3 +140,29 @@ boost::optional invokeWithArgsList(const Callable& c, const QVariantLi } 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{}); +}