funchelpers: Add a way to invoke a callable with a list of arguments
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 15 Oct 2018 22:39:31 +0000 (00:39 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 18 Nov 2018 10:06:43 +0000 (11:06 +0100)
In various places, we serialize function arguments as a QVariantList,
and later use QMetaMethod and friends to call a slot with that list.

In order to avoid this kind of runtime method lookup, we need a way
to directly invoke a given callable (e.g. a function pointer) with
a list of serialized arguments. We'll also want to use the type-based
conversion functionality of QVariant instead of using undocumented
methods and reinterpret_cast'ing raw memory, as is currently the case.

Provide a method invokeWithArgsList() that takes any callable and
a list of arguments serialized in a QVariantList, performs some
runtime sanity checks (argument count and types), and invokes the
callable with the arguments converted to the correct type, if
possible.

Also add a test case for this function.

src/common/funchelpers.h
tests/common/CMakeLists.txt
tests/common/funchelperstest.cpp [new file with mode: 0644]

index c56cdb9..fcd5aa4 100644 (file)
 
 #pragma once
 
 
 #pragma once
 
+#include <array>
 #include <functional>
 #include <tuple>
 #include <functional>
 #include <tuple>
+#include <type_traits>
+#include <utility>
+
+#include <QDebug>
+#include <QVariantList>
+
+// ---- Function traits --------------------------------------------------------------------------------------------------------------------
 
 namespace detail {
 
 
 namespace detail {
 
@@ -62,3 +70,51 @@ struct FuncHelper<R(C::*)(Args...) const> : public FuncHelper<R(C::*)(Args...)>
  */
 template<typename Callable>
 using FunctionTraits = detail::FuncHelper<Callable>;
  */
 template<typename Callable>
 using FunctionTraits = detail::FuncHelper<Callable>;
+
+// ---- Invoke function with argument list -------------------------------------------------------------------------------------------------
+
+namespace detail {
+
+// Helper for unpacking the argument list via an index sequence
+template<typename Callable, std::size_t ...Is, typename ArgsTuple = typename FunctionTraits<Callable>::ArgsTuple>
+bool invokeWithArgsList(const Callable& c, const QVariantList& args, std::index_sequence<Is...>)
+{
+    // Sanity check that all types can be converted
+    std::array<bool, std::tuple_size<ArgsTuple>::value> convertible{{args[Is].canConvert<std::decay_t<std::tuple_element_t<Is, ArgsTuple>>>()...}};
+    for (size_t i = 0; i < convertible.size(); ++i) {
+        if (!convertible[i]) {
+            qWarning() << "Cannot convert parameter" << i << "from type" << args[static_cast<int>(i)].typeName() << "to expected argument type";
+            return false;
+        }
+    }
+
+    // Invoke callable
+    c(args[Is].value<std::decay_t<std::tuple_element_t<Is, ArgsTuple>>>()...);
+    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<typename Callable>
+bool invokeWithArgsList(const Callable& c, const QVariantList& args)
+{
+    using ArgsTuple = typename FunctionTraits<Callable>::ArgsTuple;
+    constexpr auto tupleSize = std::tuple_size<ArgsTuple>::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<tupleSize>{});
+}
index 2b6b2ed..6b84646 100644 (file)
@@ -1,5 +1,7 @@
 quassel_add_test(ExpressionMatchTest)
 
 quassel_add_test(ExpressionMatchTest)
 
+quassel_add_test(FuncHelpersTest)
+
 quassel_add_test(SignalProxyTest
     LIBRARIES
         Quassel::Test::Util
 quassel_add_test(SignalProxyTest
     LIBRARIES
         Quassel::Test::Util
diff --git a/tests/common/funchelperstest.cpp b/tests/common/funchelperstest.cpp
new file mode 100644 (file)
index 0000000..14a7837
--- /dev/null
@@ -0,0 +1,77 @@
+/***************************************************************************
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#include "testglobal.h"
+
+#include <QVariantList>
+
+#include "funchelpers.h"
+
+TEST(FuncHelpersTest, invokeWithArgsList)
+{
+    int intVal{};
+    QString stringVal{};
+
+    auto callable = [&intVal, &stringVal](int i, const QString& s)
+    {
+        intVal = i;
+        stringVal = s;
+    };
+
+    // Good case
+    {
+        QVariantList argsList{42, "Hello World"};
+        ASSERT_TRUE(invokeWithArgsList(callable, argsList));
+        EXPECT_EQ(42, intVal);
+        EXPECT_EQ("Hello World", stringVal);
+    }
+
+    // Too many arguments
+    {
+        QVariantList argsList{23, "Hi Universe", 2.3};
+        ASSERT_FALSE(invokeWithArgsList(callable, argsList));
+        // Values shouldn't have changed
+        EXPECT_EQ(42, intVal);
+        EXPECT_EQ("Hello World", stringVal);
+    }
+
+    // Too few arguments
+    {
+        QVariantList argsList{23};
+        ASSERT_FALSE(invokeWithArgsList(callable, argsList));
+        // Values shouldn't have changed
+        EXPECT_EQ(42, intVal);
+        EXPECT_EQ("Hello World", stringVal);
+    }
+
+    // Cannot convert argument type
+    {
+        // Ensure type cannot be converted
+        QVariantList wrong{"Foo", "Bar"};
+        QVariant v{wrong};
+        ASSERT_FALSE(v.canConvert<QString>());
+
+        QVariantList argsList{23, wrong};
+        ASSERT_FALSE(invokeWithArgsList(callable, argsList));
+        // Values shouldn't have changed
+        EXPECT_EQ(42, intVal);
+        EXPECT_EQ("Hello World", stringVal);
+    }
+}