]> git.sesse.net Git - nageru/blob - shared/post_to_main_thread.h
Fix some leftovers in warning messages.
[nageru] / shared / post_to_main_thread.h
1 #ifndef _POST_TO_MAIN_THREAD_H
2 #define _POST_TO_MAIN_THREAD_H 1
3
4 #include <QApplication>
5 #include <QObject>
6 #include <future>
7 #include <memory>
8
9 // http://stackoverflow.com/questions/21646467/how-to-execute-a-functor-in-a-given-thread-in-qt-gcd-style
10 template<typename F>
11 static inline void post_to_main_thread(F &&fun)
12 {
13         QObject signalSource;
14         QObject::connect(&signalSource, &QObject::destroyed, qApp, std::move(fun));
15 }
16
17 template<typename F>
18 static inline void post_to_main_thread_and_wait(F &&fun)
19 {
20         std::promise<void> done_promise;
21         std::future<void> done = done_promise.get_future();
22         post_to_main_thread(std::move(fun));
23         post_to_main_thread([&done_promise] { done_promise.set_value(); });
24         done.wait();
25 }
26
27 #endif  // !defined(_POST_TO_MAIN_THREAD_H)