funchelpers: Support the invocation of non-void callables
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 31 Oct 2018 23:39:21 +0000 (00:39 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 18 Nov 2018 10:06:43 +0000 (11:06 +0100)
Let invokeWithArgsList() handle and return the return value of
non-void callables wrapped in a QVariant. To avoid special-casing,
the invocation of a void callable also returns a (default-constructed,
thus invalid) QVariant.

To indicate failure, wrap the QVariant in a boost::optional that is
empty if the callable could not be invoked.

src/common/funchelpers.h
src/common/signalproxy.h
tests/common/funchelperstest.cpp

index fcd5aa4..ede4e43 100644 (file)
 #include <type_traits>
 #include <utility>
 
 #include <type_traits>
 #include <utility>
 
+#include <boost/optional.hpp>
+
 #include <QDebug>
 #include <QDebug>
+#include <QVariant>
 #include <QVariantList>
 
 // ---- Function traits --------------------------------------------------------------------------------------------------------------------
 #include <QVariantList>
 
 // ---- Function traits --------------------------------------------------------------------------------------------------------------------
@@ -75,22 +78,38 @@ using FunctionTraits = detail::FuncHelper<Callable>;
 
 namespace detail {
 
 
 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<typename Callable, typename ...Args>
+auto invokeWithArgs(const Callable& c, Args&&... args)
+    -> std::enable_if_t<std::is_void<typename FunctionTraits<Callable>::ReturnType>::value, QVariant>
+{
+    c(std::forward<Args>(args)...);
+    return QVariant{};
+}
+
+template<typename Callable, typename ...Args>
+auto invokeWithArgs(const Callable& c, Args&&... args)
+    -> std::enable_if_t<!std::is_void<typename FunctionTraits<Callable>::ReturnType>::value, QVariant>
+{
+    return QVariant::fromValue(c(std::forward<Args>(args)...));
+}
+
 // Helper for unpacking the argument list via an index sequence
 template<typename Callable, std::size_t ...Is, typename ArgsTuple = typename FunctionTraits<Callable>::ArgsTuple>
 // 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...>)
+boost::optional<QVariant> 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";
 {
     // 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;
+            return boost::none;
         }
     }
 
         }
     }
 
-    // Invoke callable
-    c(args[Is].value<std::decay_t<std::tuple_element_t<Is, ArgsTuple>>>()...);
-    return true;
+    // Invoke callable with unmarshalled arguments
+    return invokeWithArgs(c, args[Is].value<std::decay_t<std::tuple_element_t<Is, ArgsTuple>>>()...);
 }
 
 }  // detail
 }
 
 }  // 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.
  * 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
  *
  * @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<typename Callable>
  */
 template<typename Callable>
-bool invokeWithArgsList(const Callable& c, const QVariantList& args)
+boost::optional<QVariant> 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();
 {
     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 boost::none;
     }
     return detail::invokeWithArgsList(c, args, std::make_index_sequence<tupleSize>{});
 }
     }
     return detail::invokeWithArgsList(c, args, std::make_index_sequence<tupleSize>{});
 }
index 885ebe6..b58aae8 100644 (file)
@@ -422,7 +422,7 @@ public:
             qWarning() << "Cannot call slot in different thread!";
             return false;
         }
             qWarning() << "Cannot call slot in different thread!";
             return false;
         }
-        return invokeWithArgsList(_callable, params);
+        return invokeWithArgsList(_callable, params) ? true : false;
     }
 
 private:
     }
 
 private:
index 14a7837..c40cade 100644 (file)
@@ -38,7 +38,9 @@ TEST(FuncHelpersTest, invokeWithArgsList)
     // Good case
     {
         QVariantList argsList{42, "Hello World"};
     // Good case
     {
         QVariantList argsList{42, "Hello World"};
-        ASSERT_TRUE(invokeWithArgsList(callable, argsList));
+        auto ret = invokeWithArgsList(callable, argsList);
+        ASSERT_TRUE(ret);
+        EXPECT_FALSE(ret->isValid());  // Callable returns void, so the returned QVariant should be invalid
         EXPECT_EQ(42, intVal);
         EXPECT_EQ("Hello World", stringVal);
     }
         EXPECT_EQ(42, intVal);
         EXPECT_EQ("Hello World", stringVal);
     }
@@ -75,3 +77,35 @@ TEST(FuncHelpersTest, invokeWithArgsList)
         EXPECT_EQ("Hello World", stringVal);
     }
 }
         EXPECT_EQ("Hello World", stringVal);
     }
 }
+
+TEST(FuncHelpersTest, invokeWithArgsListAndReturnValue)
+{
+    {
+        int intVal{};
+        QString stringVal{};
+
+        auto callable = [&intVal, &stringVal](int i, const QString& s)
+        {
+            intVal = i;
+            stringVal = s;
+            return -i;
+        };
+
+        // Good case
+        {
+            QVariantList argsList{42, "Hello World"};
+            auto ret = invokeWithArgsList(callable, argsList);
+            ASSERT_TRUE(ret);
+            ASSERT_TRUE(ret->isValid());
+            EXPECT_EQ(-42, *ret);
+            EXPECT_EQ(42, intVal);
+            EXPECT_EQ("Hello World", stringVal);
+        }
+
+        // Failed invocation
+        {
+            QVariantList argsList{23};
+            ASSERT_FALSE(invokeWithArgsList(callable, argsList));
+        }
+    }
+}