MeVisLab Toolbox Reference
mlMainThreadCommunicator.h
Go to the documentation of this file.
1// Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2// **InsertLicense** code
3
4#pragma once
5
7
8#include <FMEThirdPartyWarningsDisable.h>
9#include <utility>
10#include <mutex>
11#include <FMEThirdPartyWarningsRestore.h>
12
13// Block warnings since it's not done by background task includes below.
14#include <FMEThirdPartyWarningsDisable.h>
17#include <FMEThirdPartyWarningsRestore.h>
18
19
21
22
24{
25public:
26
27 template <typename Callable>
29 {
30 auto* message = NewBackgroundTaskMethodCall(this, &UnprotectedMainThreadCommunicator::execute<Callable>, std::forward<Callable>(callable));
31 auto& mgr = BackgroundTaskManager::self();
32 mgr.sendMessageToGUI(message);
33 }
34
35 template<typename Callable>
37 {
38 if (BackgroundTaskManager::ensureGUIThread()) {
39 std::forward<Callable>(callable)();
40 }
41 else {
42 callLater(std::forward<Callable>(callable));
43 }
44 }
45
46 template<typename Object, typename Method, typename... Args>
47 void call(Object* object, Method method, const Args&... args)
48 {
49 call([object, method, args...] {
50 (object->*method)(args...);
51 });
52 }
53
54private:
55
56 template<typename Callable>
57 void execute(const Callable& callable)
58 {
59 callable();
60 }
61};
62
63
65{
67 {
68 public:
69
70 Access(std::mutex& mutex, std::weak_ptr<UnprotectedMainThreadCommunicator> implementation);
71
72 operator bool() const;
73
74 UnprotectedMainThreadCommunicator* operator->() const;
75
77
78 private:
79
80 std::unique_lock<std::mutex> lock;
81 std::shared_ptr<UnprotectedMainThreadCommunicator> implementation;
82 };
83
85 {
86 public:
87
88 Handle(std::shared_ptr<std::mutex> mutex, const std::shared_ptr<UnprotectedMainThreadCommunicator>& implementation);
89
90 Access access() const;
91
92 private:
93
94 std::shared_ptr<std::mutex> mutex;
95 std::weak_ptr<UnprotectedMainThreadCommunicator> implementation;
96 };
97
99 MainThreadCommunicator& operator=(const MainThreadCommunicator&) = delete;
100
101public:
102
104
106
107 Handle handle() const;
108
109private:
110
111 std::shared_ptr<std::mutex> mutex;
112 std::shared_ptr<UnprotectedMainThreadCommunicator> implementation;
113};
114
115
#define MLMAINTHREADCOMMUNICATOR_EXPORT
The background task receiver is a light-weight base class which can be inherited (e....
void call(Object *object, Method method, const Args &... args)
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
BackgroundTaskMessage * NewBackgroundTaskMethodCall(Object *object, Method method)
create new method call with 0 arguments