diff options
Diffstat (limited to 'compat/run-in-thread.hpp')
-rw-r--r-- | compat/run-in-thread.hpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/compat/run-in-thread.hpp b/compat/run-in-thread.hpp index d377f625..90aa143b 100644 --- a/compat/run-in-thread.hpp +++ b/compat/run-in-thread.hpp @@ -1,12 +1,16 @@ #pragma once -#include <QObject> +#include "ndebug-guard.hpp" +#include <cassert> #include <thread> #include <condition_variable> #include <utility> -namespace detail { +#include <QObject> +#include <QThread> + +namespace qt_impl_detail { template<typename t> struct run_in_thread_traits @@ -25,14 +29,14 @@ struct run_in_thread_traits<void> using ret_type = void; static inline void assign(unsigned char&, unsigned char&&) {} static inline void pass(type&&) {} - template<typename F> static type&& call(F& fun) { fun(); return std::move(type(0)); } + template<typename F> static type call(F& fun) { fun(); return type(0); } }; } template<typename F> auto run_in_thread_sync(QObject* obj, F&& fun) - -> typename detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>::ret_type + -> typename qt_impl_detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>::ret_type { using lock_guard = std::unique_lock<std::mutex>; @@ -42,7 +46,7 @@ auto run_in_thread_sync(QObject* obj, F&& fun) std::thread::id waiting_thread = std::this_thread::get_id(); - using traits = detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>; + using traits = qt_impl_detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>; typename traits::type ret; @@ -50,7 +54,9 @@ auto run_in_thread_sync(QObject* obj, F&& fun) { QObject src; - src.moveToThread(obj->thread()); + QThread* t(obj->thread()); + assert(t); + src.moveToThread(t); QObject::connect(&src, &QObject::destroyed, obj, @@ -80,6 +86,8 @@ template<typename F> void run_in_thread_async(QObject* obj, F&& fun) { QObject src; - src.moveToThread(obj->thread()); + QThread* t(obj->thread()); + assert(t); + src.moveToThread(t); QObject::connect(&src, &QObject::destroyed, obj, std::move(fun), Qt::AutoConnection); } |