1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
26 #include <type_traits>
30 #include <QVariantList>
32 // ---- Function traits --------------------------------------------------------------------------------------------------------------------
36 /// @cond DOXYGEN_CANNOT_PARSE_THIS
39 template<typename Func>
40 struct FuncHelper : public FuncHelper<decltype(&Func::operator())>
43 // Overload for free function
44 template<typename R, typename... Args>
45 struct FuncHelper<R(*)(Args...)>
47 using FunctionType = std::function<R(Args...)>;
49 using ArgsTuple = std::tuple<Args...>;
52 // Overload for member function with non-const call operator
53 template<typename C, typename R, typename... Args>
54 struct FuncHelper<R(C::*)(Args...)> : public FuncHelper<R(*)(Args...)>
59 // Overload for member function with const call operator
60 template<typename C, typename R, typename... Args>
61 struct FuncHelper<R(C::*)(Args...) const> : public FuncHelper<R(C::*)(Args...)>
69 * Provides traits for the given callable.
71 template<typename Callable>
72 using FunctionTraits = detail::FuncHelper<Callable>;
74 // ---- Invoke function with argument list -------------------------------------------------------------------------------------------------
78 // Helper for unpacking the argument list via an index sequence
79 template<typename Callable, std::size_t ...Is, typename ArgsTuple = typename FunctionTraits<Callable>::ArgsTuple>
80 bool invokeWithArgsList(const Callable& c, const QVariantList& args, std::index_sequence<Is...>)
82 // Sanity check that all types can be converted
83 std::array<bool, std::tuple_size<ArgsTuple>::value> convertible{{args[Is].canConvert<std::decay_t<std::tuple_element_t<Is, ArgsTuple>>>()...}};
84 for (size_t i = 0; i < convertible.size(); ++i) {
85 if (!convertible[i]) {
86 qWarning() << "Cannot convert parameter" << i << "from type" << args[static_cast<int>(i)].typeName() << "to expected argument type";
92 c(args[Is].value<std::decay_t<std::tuple_element_t<Is, ArgsTuple>>>()...);
99 * Invokes the given callable with the arguments contained in the given variant list.
101 * The types contained in the given QVariantList are converted to the types expected by the callable.
102 * If the conversion fails, or if the argument count does not match, this function returns false and
103 * the callable is not invoked.
106 * @param args Arguments to be given to the callable
107 * @returns true if the callable could be invoked with the given list of arguments
109 template<typename Callable>
110 bool invokeWithArgsList(const Callable& c, const QVariantList& args)
112 using ArgsTuple = typename FunctionTraits<Callable>::ArgsTuple;
113 constexpr auto tupleSize = std::tuple_size<ArgsTuple>::value;
115 if (tupleSize != args.size()) {
116 qWarning().nospace() << "Argument count mismatch! Expected: " << tupleSize << ", actual: " << args.size();
119 return detail::invokeWithArgsList(c, args, std::make_index_sequence<tupleSize>{});