Use functor object as main argument of callAndWait 47/36747/4
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Wed, 11 Mar 2015 09:44:44 +0000 (10:44 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Tue, 17 Mar 2015 14:49:13 +0000 (15:49 +0100)
Previous solution was based on std::function, that causes problems
because std::function template arguments needed to be explicitely
defined.

Functor simplifies usage of callAndWait.

Change-Id: If4f8a640848e41042e6841e49354510c08db5357

src/common/timeout.h

index 7e985ab..98662e1 100644 (file)
@@ -25,6 +25,7 @@
 #include <chrono>
 #include <functional>
 #include <future>
+#include <type_traits>
 #include <utility>
 
 #include <tests_common.h>
@@ -34,9 +35,6 @@ namespace Timeout {
 template <class Rep, class Period>
 using Timeout = std::chrono::duration<Rep, Period>;
 
-template <class Ret, class... Args>
-using Function = std::function<Ret(Args...)>;
-
 typedef std::function<void(void)> CancelFunction;
 
 enum ExpectMode {
@@ -62,16 +60,15 @@ std::ostream& operator<<(std::ostream& os, const std::future_status &status)
     return os;
 }
 
-template <class Rep, class Period, class Ret, class... Args>
-Ret callAndWait(const Timeout<Rep, Period> &timeout,
+template <class Rep, class Period, class F, class... Args>
+    typename std::result_of<F(Args...)>::type
+    callAndWait(const Timeout<Rep, Period> &timeout,
                  ExpectMode expect,
                  CancelFunction cancelFunction,
-                 Function<Ret, Args...> function,
-                 Args... args) {
-    RUNNER_ASSERT_MSG(function,
-                         "not empty function must be passed to callAndWait");
+                 F&& function,
+                 Args&&... args) {
 
-    std::future<Ret> fut = std::async(std::launch::async, function, std::forward<Args>(args)...);
+    auto fut = std::async(std::launch::async, function, std::forward<Args>(args)...);
     std::future_status status = fut.wait_for(timeout);
 
     if (status == std::future_status::timeout && cancelFunction)