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 <ThirdPartyWarningsDisable.h>
9 #include <utility>
10 #include <mutex>
11 #include <ThirdPartyWarningsRestore.h>
12 
13 // Block warnings since it's not done by background task includes below.
14 #include <ThirdPartyWarningsDisable.h>
17 #include <ThirdPartyWarningsRestore.h>
18 
19 
20 ML_START_NAMESPACE
21 
22 
24 {
25 public:
26 
27  template <typename Callable>
28  void callLater(Callable&& 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>
36  void call(Callable&& 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 
54 private:
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 
101 public:
102 
104 
106 
107  Handle handle() const;
108 
109 private:
110 
111  std::shared_ptr<std::mutex> mutex;
112  std::shared_ptr<UnprotectedMainThreadCommunicator> implementation;
113 };
114 
115 
116 ML_END_NAMESPACE
#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)
T & access(T &storage)
Definition: mlLightweight.h:67
static void call(MLDataType dataType, Processor &processor)
Call the process<DataType>() method on the given processor, where the DataType type is dynamically se...
BackgroundTaskMessage * NewBackgroundTaskMethodCall(Object *object, Method method)
create new method call with 0 arguments
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).