DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
Module.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
8#pragma once
9
10#include "stdafx.h"
11#include "Object.h"
12
13namespace DynExp
14{
15 class ModuleBase;
16 class ModuleInstance;
17 class EventListenersBase;
18 class InterModuleEventLibrary;
19 class QModuleBase;
20
24 using ModulePtrType = std::unique_ptr<ModuleBase>;
25
31 template <typename ModuleT>
33 {
34 return std::make_shared<typename ModuleT::ConfigType>();
35 }
36
44 template <typename ModuleT>
45 ModulePtrType MakeModule(const std::thread::id OwnerThreadID, ParamsBasePtrType&& Params)
46 {
47 dynamic_Params_cast<ModuleT>(Params.get())->ModuleData = std::make_unique<typename ModuleT::ModuleDataType>();
48
49 return std::make_unique<ModuleT>(OwnerThreadID, std::move(Params));
50 }
51
62 int ModuleThreadMain(std::unique_ptr<RunnableInstance>&& InstancePtr, RunnableObject* BaseObject);
63
68 {
69 public:
70 EventBase() = default;
71 virtual ~EventBase() = 0;
72
78 void Invoke(ModuleInstance& Instance) const { InvokeChild(Instance); }
79
80 private:
85 virtual void InvokeChild(ModuleInstance& Instance) const = 0;
87 };
88
95 template <typename ReceiverType, typename ArgTupleType>
96 class DefaultEvent : public EventBase
97 {
98 private:
103 template <size_t... Indices>
105 {
106 constexpr EventFuncTraits() = default;
107
113 using EventFuncPtrType = void (ReceiverType::*)(ModuleInstance*, std::tuple_element_t<Indices, ArgTupleType>...) const;
114
119 };
120
126 template <size_t... Indices>
127 constexpr static auto MakeEventFuncTraits(std::index_sequence<Indices...>) { return EventFuncTraits<Indices...>(); }
128
132 using ArgTupleIndexSequenceType = decltype(std::make_index_sequence<std::tuple_size_v<ArgTupleType>>());
133
137 using InstantiatedEventFuncTraitsType = decltype(DefaultEvent<ReceiverType, ArgTupleType>::MakeEventFuncTraits(std::declval<ArgTupleIndexSequenceType>()));
138
139 public:
140 using EventFuncPtrType = typename InstantiatedEventFuncTraitsType::EventFuncPtrType;
141 using EventFuncType = typename InstantiatedEventFuncTraitsType::EventFuncType;
142
148
149 virtual ~DefaultEvent() {}
150
151 private:
152 virtual void InvokeChild(ModuleInstance& Instance) const override { EventFunc(&Instance); }
153
158 };
159
173 {
174 public:
178 using EventPtrType = std::unique_ptr<EventBase>;
179
183 using EventQueueType = std::queue<EventPtrType>;
184
185 protected:
190 template <typename>
191 struct dispatch_tag {};
192
193 private:
198 {
199 friend class ModuleDataBase;
200 friend class ModuleBase;
201
207
208 void Reset() { Parent.Reset(); }
210 void SetException(std::exception_ptr Exception) noexcept { Parent.SetException(Exception); }
211
212 auto& GetNewEventNotifier() noexcept { return Parent.GetNewEventNotifier(); }
213
215 };
216
222 {
223 friend class ModuleDataBase;
224 friend int ModuleThreadMain(std::unique_ptr<RunnableInstance>&&, RunnableObject*);
225
231
232 auto& GetNewEventNotifier() noexcept { return Parent.GetNewEventNotifier(); }
233
235 };
236
237 public:
239 virtual ~ModuleDataBase() {}
240
246
253 void EnqueueEvent(EventPtrType&& Event);
254
261
267 const auto& GetEventFront() const noexcept { return *EventQueue.front().get(); }
268
273 auto& GetEventFront() noexcept { return *EventQueue.front().get(); }
274
279 size_t GetNumEnqueuedEvents() const noexcept { return EventQueue.size(); }
280
286
291 bool IsExceptionIndicated() const noexcept { return HasException; }
292
299 std::exception_ptr GetException() const noexcept;
300
303
304 private:
309 Util::OneToOneNotifier& GetNewEventNotifier() noexcept { return NewEventNotifier; }
310
315
319 void Reset();
320
328
334 void IndicateException() noexcept { HasException = true; }
335
340 void SetException(std::exception_ptr Exception) noexcept;
341
346
353
359 std::atomic<bool> HasException;
360
366 std::exception_ptr ModuleException;
367 };
368
373 {
374 friend class ModuleBase;
375
376 template <typename>
377 friend ModulePtrType MakeModule(const std::thread::id, ParamsBasePtrType&&);
378
379 public:
385
386 virtual ~ModuleParamsBase() = 0;
387
388 virtual const char* GetParamClassTag() const noexcept override { return "ModuleParamsBase"; }
389
390 private:
393
394 // Usage parameters may be used in the future also by modules.
395 bool ConfigureUsageTypeChild() const noexcept override final { return false; }
396
401 std::unique_ptr<ModuleDataBase> ModuleData;
402
403 DummyParam Dummy = { *this };
404 };
405
418
428 {
434 {
435 friend class ModuleBase;
436 friend int ModuleThreadMain(std::unique_ptr<RunnableInstance>&&, RunnableObject*);
437
443
444 void HandleEvent(ModuleInstance& Instance) { Parent.HandleEvent(Instance); } //<! @copydoc ModuleBase::HandleEvent
445 Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(ModuleInstance& Instance) { return Parent.ExecModuleMainLoop(Instance); } //<! @copydoc ModuleBase::ExecModuleMainLoop
446 void SetException(std::exception_ptr Exception) noexcept { Parent.SetException(Exception); }
447 void OnPause(ModuleInstance& Instance) { Parent.OnPause(Instance); } //<! @copydoc ModuleBase::OnPause
448 void OnResume(ModuleInstance& Instance) { Parent.OnResume(Instance); } //<! @copydoc ModuleBase::OnResume
449 void OnError(ModuleInstance& Instance) { Parent.OnError(Instance); } //<! @copydoc ModuleBase::OnError
450
451 void SetReasonWhyPaused(std::string Description) { Parent.ModuleSetReasonWhyPaused(std::move(Description)); } //<! @copydoc ModuleBase::ModuleSetReasonWhyPaused
452
454 };
455
461 {
462 friend class ModuleBase;
463
464 template <typename...>
466
472
473 void AddRegisteredEvent(EventListenersBase& EventListeners) const { Parent.AddRegisteredEvent(EventListeners); }
474 void RemoveRegisteredEvent(EventListenersBase& EventListeners) const { Parent.RemoveRegisteredEvent(EventListeners); }
475
477 };
478
479 public:
482
488
494
500
505 constexpr static auto Category() noexcept { return ""; }
506
512 ModuleBase(const std::thread::id OwnerThreadID, ParamsBasePtrType&& Params);
513
514 virtual ~ModuleBase() = 0;
515
516 virtual std::string GetCategory() const override { return Category(); }
517
522
527 virtual bool HasUI() const noexcept { return false; }
528
535 virtual bool TreatModuleExceptionsAsWarnings() const { return true; }
536
543 virtual std::chrono::milliseconds GetMainLoopDelay() const { return std::chrono::milliseconds(10); }
545
550 static constexpr auto GetModuleDataTimeoutDefault = std::chrono::milliseconds(1000);
551
554
559
565
572
582 ModuleDataTypeSyncPtrType GetModuleData(const std::chrono::milliseconds Timeout = GetModuleDataTimeoutDefault);
583
590 ModuleDataTypeSyncPtrConstType GetModuleData(const std::chrono::milliseconds Timeout = GetModuleDataTimeoutDefault) const;
591
595 void EnqueueEvent(ModuleDataBase::EventPtrType&& Event) const;
596
615 template <typename ReceiverType, typename EventType, typename... ArgsTs>
616 void MakeAndEnqueueEvent(ReceiverType* Receiver, EventType EventFuncPtr, ArgsTs&& ...Args) const;
618
624 ModuleDataTypeSyncPtrType (ModuleBase::*)(const std::chrono::milliseconds)>;
625
626 private:
631
637 ModuleDataTypeSyncPtrType GetNonConstModuleData(const std::chrono::milliseconds Timeout = GetModuleDataTimeoutDefault) const;
639
640 protected:
647 static auto GetExceptionUnsafe(const ModuleDataTypeSyncPtrConstType& ModuleDataPtr) { return ModuleDataPtr->GetException(); }
648
649 private:
654
658 void HandleEvent(ModuleInstance& Instance);
659
665 void AddRegisteredEvent(EventListenersBase& EventListeners);
666
672 void RemoveRegisteredEvent(EventListenersBase& EventListeners);
673
679
686 void SetException(std::exception_ptr Exception) noexcept;
687
694 void OnPause(ModuleInstance& Instance);
695
702 void OnResume(ModuleInstance& Instance);
703
709 void OnError(ModuleInstance& Instance);
711
716 virtual void OnPauseChild(ModuleInstance& Instance) const {}
717 virtual void OnResumeChild(ModuleInstance& Instance) const {}
718 virtual void OnErrorChild(ModuleInstance& Instance) const {}
719
722
731
732 void ResetImpl(dispatch_tag<RunnableObject>) override final;
734
739 void ModuleSetReasonWhyPaused(std::string Description) { SetReasonWhyPaused(std::move(Description)); }
740
745 void RunChild() override final;
746 void NotifyChild() override final;
747 void TerminateChild(const std::chrono::milliseconds Timeout) override final;
748 std::unique_ptr<BusyDialog> MakeStartupBusyDialogChild(QWidget* ParentWidget) const override final;
750
751 std::exception_ptr GetExceptionChild([[maybe_unused]] const std::chrono::milliseconds Timeout) const override final;
752 bool IsReadyChild() const override final;
753
758
764 virtual void OnInit(ModuleInstance* Instance) const {}
765
773 virtual void OnExit(ModuleInstance* Instance) const {}
774
782 void OnDeregisterEvents(ModuleInstance* Instance) const;
784
788 const std::unique_ptr<ModuleDataType> ModuleData;
789
797 std::vector<EventListenersBase*> RegisteredEvents;
798 };
799
809 template <typename ReceiverType, typename... ArgsTs>
810 auto MakeEvent(ReceiverType* Receiver,
811 typename DefaultEvent<ReceiverType, std::tuple<std::remove_reference_t<ArgsTs>...>>::EventFuncPtrType EventFuncPtr,
812 ArgsTs&& ...Args)
813 {
814 if (!EventFuncPtr)
815 throw Util::InvalidArgException("EventFuncPtr cannot be nullptr.");
816
817 // Use EventArgTs instead of std::tuple<ArgsTs...> since the latter would create a tuple of references!
818 using EventArgTs = Util::remove_first_from_tuple_t<Util::argument_of_t<decltype(EventFuncPtr)>>;
819
820 auto EventFunc = typename DefaultEvent<ReceiverType, EventArgTs>::EventFuncType(*Receiver, EventFuncPtr,
821 Util::argument_of_t<decltype(EventFuncPtr)>(nullptr, std::forward<ArgsTs>(Args)...));
822
823 return std::make_unique<DefaultEvent<ReceiverType, EventArgTs>>(EventFunc);
824 }
825
826 template <typename ReceiverType, typename EventType, typename... ArgsTs>
827 void ModuleBase::MakeAndEnqueueEvent(ReceiverType* Receiver, EventType EventFuncPtr, ArgsTs&& ...Args) const
828 {
829 if (Receiver != this)
830 throw Util::InvalidArgException("Receiver cannot point to a module different from the instance MakeAndEnqueueEvent() is called on.");
831
832 EnqueueEvent(MakeEvent(Receiver, EventFuncPtr, std::forward<ArgsTs>(Args)...));
833 }
834
840 {
841 public:
846 ModuleInstance(ModuleBase& Owner, std::promise<void>&& ThreadExitedPromise,
848
853
854 ~ModuleInstance() = default;
855
860 void Exit() noexcept { ShouldExit = true; }
861
867 bool IsExiting() const noexcept { return ShouldExit; }
868
873
874 private:
879 bool ShouldExit = false;
880 };
881
895 template <typename To, typename From, std::enable_if_t<
896 std::is_same_v<ModuleDataBase, std::remove_cv_t<From>>, int> = 0
897 >
899 {
900 if (!ModuleDataPtr)
901 throw Util::InvalidArgException("ModuleDataPtr must not be nullptr.");
902
904 std::conditional_t<std::is_const_v<From>, std::add_const_t<typename To::ModuleDataType>, typename To::ModuleDataType>
905 >(std::move(ModuleDataPtr));
906 }
907
913 {
914 protected:
917
918 public:
926 virtual void Deregister(const ModuleBase& Listener,
927 const std::chrono::milliseconds Timeout = std::chrono::milliseconds(0)) = 0;
928
937 virtual void Deregister(const ModuleBase& Listener, ItemIDType CommunicatorID,
938 const std::chrono::milliseconds Timeout = std::chrono::milliseconds(0)) = 0;
939 };
940
949 template <typename... EventFuncArgs>
951 {
957 {
962
969
975 inline bool operator==(const ListenersTypeKey& Other) const
976 {
977 return Module == Other.Module && CommunicatorID == Other.CommunicatorID;
978 }
979 };
980
986 {
992 inline size_t operator()(const ListenersTypeKey& v) const
993 {
994 size_t seed = std::hash<decltype(ListenersTypeKey::Module)>()(v.Module);
996
997 return seed;
998 }
999 };
1000
1001 public:
1008 using EventFunctionType = std::function<void(ModuleInstance*, EventFuncArgs...)>;
1009
1011 virtual ~TypedEventListeners() = default;
1012
1029 template <typename CallableT>
1030 void Register(const ModuleBase& Listener, CallableT EventFunc, ItemIDType CommunicatorID = ItemIDNotSet,
1031 const std::chrono::milliseconds Timeout = std::chrono::milliseconds(0))
1032 {
1033 auto lock = AcquireLock(Timeout);
1034 RegisterUnsafe(Listener, EventFunc, CommunicatorID);
1035 }
1036
1037 virtual void Deregister(const ModuleBase& Listener, ItemIDType CommunicatorID,
1038 const std::chrono::milliseconds Timeout = std::chrono::milliseconds(0)) override
1039 {
1040 auto lock = AcquireLock(Timeout);
1041 DeregisterUnsafe(Listener, CommunicatorID);
1042 }
1043
1044 virtual void Deregister(const ModuleBase& Listener,
1045 const std::chrono::milliseconds Timeout = std::chrono::milliseconds(0)) override
1046 {
1047 auto lock = AcquireLock(Timeout);
1048 DeregisterAllUnsafe(Listener);
1049 }
1050
1061 const std::chrono::milliseconds Timeout = DefaultTimeout) const
1062 {
1063 auto lock = AcquireLock(Timeout);
1064 return GetFuncUnsafe(Listener, CommunicatorID);
1065 }
1066
1067 private:
1078 template <typename CallableT>
1079 void RegisterUnsafe(const ModuleBase& Listener, CallableT EventFunc, ItemIDType CommunicatorID)
1080 {
1081 const auto NumListeners = Listeners.size();
1082 Listeners[ListenersTypeKey{ &Listener, CommunicatorID }] = [&Listener, EventFunc](ModuleInstance* Instance, EventFuncArgs... Args) {
1083 (dynamic_cast<std::add_const_t<typename Util::member_fn_ptr_traits<CallableT>::instance_type>&>(Listener).*EventFunc)(Instance, Args...);
1084 };
1085
1086 // New Listener has been added (not a pure update).
1087 if (Listeners.size() > NumListeners)
1088 Listener.EventListenersOnly.AddRegisteredEvent(*this);
1089 }
1090
1098 void DeregisterUnsafe(const ModuleBase& Listener, ItemIDType CommunicatorID)
1099 {
1100 auto ListenerIt = Listeners.find(ListenersTypeKey{ &Listener, CommunicatorID });
1101 if (ListenerIt == Listeners.cend())
1102 return;
1103
1104 Listeners.erase(ListenerIt);
1106 }
1107
1114 void DeregisterAllUnsafe(const ModuleBase& Listener)
1115 {
1116 const auto NumErased = std::erase_if(Listeners, [&Listener](const auto& v) { return v.first.Module == &Listener; });
1117
1118 for (std::remove_const_t<decltype(NumErased)> i = 0; i < NumErased; ++i)
1120 }
1121
1130 EventFunctionType GetFuncUnsafe(const ModuleBase& Listener, ItemIDType CommunicatorID) const
1131 {
1132 // First, find event function assigned to a module-communicator pair.
1133 auto ListenerIt = Listeners.find(ListenersTypeKey{ &Listener, CommunicatorID });
1134 if (ListenerIt != Listeners.cend())
1135 return ListenerIt->second;
1136
1137 // If none has been found, find event function assigned to a module, ignoring the communicator instance.
1138 ListenerIt = Listeners.find(ListenersTypeKey{ &Listener, ItemIDNotSet });
1139 return ListenerIt != Listeners.cend() ? ListenerIt->second : nullptr;
1140 }
1141
1146 std::unordered_map<const ListenersTypeKey, EventFunctionType, ListenersTypeKeyHasher> Listeners;
1147 };
1148
1153 {
1154 public:
1158 using InterModuleEventPtrType = std::unique_ptr<InterModuleEventBase>;
1159
1164
1172
1173 virtual ~InterModuleEventBase() = 0;
1174
1181
1182 virtual size_t GetID() const noexcept = 0;
1183
1188 virtual std::string GetName() const = 0;
1190
1194 auto GetCommunicatorID() const noexcept { return CommunicatorID; }
1195
1196 private:
1201 };
1202
1209 template <typename DerivedEvent, typename... EventFuncArgs>
1211 {
1212 public:
1218 using EventListenersType = TypedEventListeners<EventFuncArgs...>;
1219
1223 InterModuleEvent() = default;
1224
1230
1232
1237 static auto ID() { return EventID; }
1238
1245 static size_t Publish(InterModuleEventLibrary& Library);
1246
1251 static InterModuleEventPtrType Make() { return std::make_unique<DerivedEvent>(); }
1252
1253 virtual InterModuleEventPtrType Clone(ItemIDType CommunicatorID) const override final { return std::make_unique<DerivedEvent>(*static_cast<const DerivedEvent*>(this), CommunicatorID); }
1254 virtual size_t GetID() const noexcept override final { return ID(); }
1255 virtual std::string GetName() const override { return typeid(DerivedEvent).name(); }
1256
1261 template <typename CallableT>
1262 static void Register(const ModuleBase& Listener, CallableT EventFunc, ItemIDType CommunicatorID = ItemIDNotSet)
1263 {
1264 Listeners.Register(Listener, EventFunc, CommunicatorID);
1265 }
1266
1271 static void Deregister(const ModuleBase& Listener)
1272 {
1273 Listeners.Deregister(Listener);
1274 }
1275
1280 static void Deregister(const ModuleBase& Listener, ItemIDType CommunicatorID)
1281 {
1283 }
1284
1285 private:
1286 virtual void InvokeChild(ModuleInstance& Instance) const override final
1287 {
1288 auto EventFunc = Listeners.GetFunc(static_cast<const ModuleBase&>(Instance.GetOwner()), GetCommunicatorID());
1289
1290 if (EventFunc)
1291 InvokeWithParamsChild(Instance, EventFunc);
1292 }
1293
1298
1307
1311 static const size_t EventID;
1312
1318 };
1319
1324 {
1325 public:
1331
1336 static InterModuleEventLibrary& Get();
1337
1343 void Register(size_t ID, EventFactoryFuncPtrType EventFactoryFuncPtr) { Events.try_emplace(ID, std::move(EventFactoryFuncPtr)); }
1344
1349 auto& GetEvents() const noexcept { return Events; }
1350
1351 private:
1356
1361 std::map<size_t, EventFactoryFuncPtrType> Events;
1362 };
1363
1364 template <typename DerivedEvent, typename ...EventFuncArgs>
1366 {
1367 auto ID = Util::UniqueID::Get<DerivedEvent>();
1368
1369 Library.Register(ID, &Make);
1370
1371 return ID;
1372 }
1373
1379 template <typename DerivedEvent, typename... EventFuncArgs>
1380 const size_t InterModuleEvent<DerivedEvent, EventFuncArgs...>::EventID = InterModuleEvent<DerivedEvent, EventFuncArgs...>::Publish(InterModuleEventLibrary::Get());
1381
1386 template <typename DerivedEvent, typename... EventFuncArgs>
1387 typename InterModuleEvent<DerivedEvent, EventFuncArgs...>::EventListenersType InterModuleEvent<DerivedEvent, EventFuncArgs...>::Listeners;
1388
1394 class QModuleWidget : public QWidget
1395 {
1396 Q_OBJECT
1397
1398 friend class QModuleBase;
1399
1400 public:
1405 constexpr static Qt::WindowFlags GetQtWindowFlagsResizable();
1406
1411 constexpr static Qt::WindowFlags GetQtWindowFlagsNonResizable();
1412
1419 QModuleWidget(QModuleBase& Owner, QWidget* Parent = nullptr);
1420
1421 ~QModuleWidget() = default;
1422
1427 const auto& GetOwner() const noexcept { return Owner; }
1428
1433
1437 virtual bool AllowResize() const noexcept { return false; }
1439
1445 Qt::WindowFlags GetQtWindowFlags() const noexcept;
1446
1453 std::string GetDataSaveDirectory() const;
1454
1461 void SetDataSaveDirectory(std::string_view Directory) const;
1462
1463 private:
1468 void EnableDockWindowShortcut(bool Enable) noexcept;
1469
1476 virtual void closeEvent(QCloseEvent* Event) override;
1477
1480
1483
1484 private slots:
1485 void OnDockWindow();
1486 void OnFocusWindow();
1487 void OnFocusMainWindow();
1488 };
1489
1494 class QModuleDockWidget : public QDockWidget
1495 {
1496 Q_OBJECT
1497
1498 public:
1507 QModuleDockWidget(QModuleBase& Owner, QWidget* Parent, Qt::WindowFlags Flags);
1508
1510
1511 private:
1518 virtual void closeEvent(QCloseEvent* Event) override;
1519
1527 virtual void keyPressEvent(QKeyEvent* Event) override;
1528
1530 };
1531
1536 {
1537 public:
1538 QModuleDataBase() = default;
1539 virtual ~QModuleDataBase() = default;
1540
1541 private:
1542 void ResetImpl(dispatch_tag<ModuleDataBase>) override final;
1544 };
1545
1550 {
1551 public:
1555 enum WindowStateType { Normal, Minimized, Maximized };
1572 enum WindowDockingStateType { Docked, Undocked };
1588 WindowStyleParamsExtension(ParamsBase& Owner, const std::string& Prefix = "")
1589 : WindowPosX{ Owner, Prefix + "WindowPosX" },
1590 WindowPosY{ Owner, Prefix + "WindowPosY" },
1591 WindowWidth{ Owner, Prefix + "WindowWidth" },
1592 WindowHeight{ Owner, Prefix + "WindowHeight" },
1593 WindowState{ Owner, Prefix + "WindowState", WindowStateType::Normal },
1594 WindowDockingState{ Owner, Prefix + "WindowDockingState", WindowDockingStateType::Docked }
1595 {}
1596
1602 void ApplyTo(QWidget& Widget, bool Show = true) const;
1603
1608 void FromWidget(const QWidget& Widget);
1609
1616 };
1617
1622 {
1623 public:
1629 : ModuleParamsBase(ID, Core), WindowStyleParams(*this) {}
1630
1631 virtual ~QModuleParamsBase() = 0;
1632
1633 virtual const char* GetParamClassTag() const noexcept override { return "QModuleParamsBase"; }
1634
1636
1637 private:
1640 };
1641
1646 {
1647 public:
1650
1652 virtual ~QModuleConfiguratorBase() = 0;
1653 };
1654
1660 {
1661 public:
1665
1670 QModuleBase(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType&& Params);
1671
1672 virtual ~QModuleBase() = 0;
1673
1674 bool HasUI() const noexcept override final { return true; }
1675
1681
1690 QAction& InitUI(DynExpManager& DynExpMgr, QMdiArea* const MdiArea);
1691
1692 void HideUI();
1693 void DisableUI();
1694 void UpdateUI();
1695
1703 void UpdateModuleWindowFocusAction();
1704
1705 void DockWindow() noexcept;
1706 void UndockWindow() noexcept;
1707 void DockUndockWindow() noexcept;
1708 void SetFocus() noexcept;
1709 void FocusMainWindow() noexcept;
1710
1716 void SendKeyPressEventToMainWindow(QKeyEvent* Event) noexcept;
1717
1724 bool IsWindowDocked() noexcept;
1725
1733 bool IsActiveWindow();
1735
1736 protected:
1759 template <typename SenderType, typename SignalType, typename ReceiverType, typename EventType>
1760 void Connect(SenderType* Sender, SignalType Signal, ReceiverType* Receiver, EventType Event);
1761
1768 template <typename WidgetType>
1769 WidgetType* GetWidget() const;
1770
1771 private:
1772 virtual void RestoreWindowStatesFromParamsChild() override;
1773 virtual void UpdateParamsFromWindowStatesChild() override;
1774
1781 void SetWidgetProperties(QWidget* const WindowWidget) const;
1782
1783 void ResetImpl(dispatch_tag<ModuleBase>) override final;
1784 virtual void ResetImpl(dispatch_tag<QModuleBase>) = 0;
1785
1790
1796 virtual std::unique_ptr<QModuleWidget> MakeUIWidget() = 0;
1797
1804 virtual void UpdateUIChild(const ModuleBase::ModuleDataGetterType& ModuleDataGetter) {}
1806
1808 std::unique_ptr<QMdiSubWindow> MdiSubWindow;
1809 std::unique_ptr<QModuleDockWidget> DockWidget;
1810 std::unique_ptr<QAction> ModuleWindowFocusAction;
1811
1812 QMdiArea* MdiArea;
1813 };
1814
1815 template <typename SenderType, typename SignalType, typename ReceiverType, typename EventType>
1816 void QModuleBase::Connect(SenderType* Sender, SignalType Signal, ReceiverType* Receiver, EventType Event)
1817 {
1818 EnsureCallFromOwningThread();
1819
1820 if (!Sender || !Receiver)
1821 throw Util::InvalidArgException("Sender and Receiver cannot be nullptr.");
1822
1823 // Arguments must not be passed by reference or by pointer to Event since Event is invoked later when
1824 // references/pointers might not be valid anymore.
1825 auto Connection = QObject::connect(Sender, Signal, [this, Receiver, Event](auto... Args) {
1826 try
1827 {
1828 MakeAndEnqueueEvent(Receiver, Event, std::move(Args)...);
1829 }
1830 catch (const Util::Exception& e)
1831 {
1832 Util::EventLog().Log("Posting an event from a module's UI to the module's thread, the error listed below occurred.", Util::ErrorType::Error);
1833 Util::EventLog().Log(e);
1834 }
1835 catch (const std::exception& e)
1836 {
1837 Util::EventLog().Log("Posting an event from a module's UI to the module's thread, the error listed below occurred.", Util::ErrorType::Error);
1838 Util::EventLog().Log(e.what());
1839 }
1840 catch (...)
1841 {
1842 Util::EventLog().Log("Posting an event from a module's UI to the module's thread, an unknown error occurred.", Util::ErrorType::Error);
1843 }
1844 });
1845
1846 if (!Connection)
1847 throw Util::InvalidArgException("QObject::connect() cannot establish a connection for the given sender and signal types.");
1848 }
1849
1850 template <typename WidgetType>
1851 WidgetType* QModuleBase::GetWidget() const
1852 {
1853 // EnsureCallFromOwningThread(); considered here, but it also prohibits legitimate calls
1854 // to thread-safe (probably const) member functions of the widget. Note, accessing the UI
1855 // objects from another thread than the application's main thread is strongly forbidden!
1856
1857 if (!Widget)
1858 throw Util::InvalidStateException("UI widget has not been created yet.");
1859
1860 // Throws if it fails.
1861 return &dynamic_cast<WidgetType&>(*Widget);
1862 }
1863}
1864
1869namespace DynExpModule {};
Implementation of DynExp objects as the base for derived resources and implementation of the object p...
Implements DynExp's main window as a Qt-based user interface (UI).
Describes an event which consists of a receiver's member function and a set of arguments to call this...
Definition Module.h:97
decltype(DefaultEvent< ReceiverType, ArgTupleType >::MakeEventFuncTraits(std::declval< ArgTupleIndexSequenceType >())) InstantiatedEventFuncTraitsType
Type of the instantiated EventFuncTraits - not actually instantiating it.
Definition Module.h:137
DefaultEvent(EventFuncType EventFunc) noexcept
Constructs a DefaultEvent instance.
Definition Module.h:147
virtual ~DefaultEvent()
Definition Module.h:149
decltype(std::make_index_sequence< std::tuple_size_v< ArgTupleType > >()) ArgTupleIndexSequenceType
Index sequence for the (pseudo-)instantiation of EventFuncTraits.
Definition Module.h:132
virtual void InvokeChild(ModuleInstance &Instance) const override
Invokes the event passing the receiving module's instance reference to it. Only to be called from Mod...
Definition Module.h:152
const EventFuncType EventFunc
Util::CallableMemberWrapper to store the event receiver, the event function, and its arguments.
Definition Module.h:157
static constexpr auto MakeEventFuncTraits(std::index_sequence< Indices... >)
Instantiates EventFuncTraits. This function is not meant to be called. It is used for type deduction ...
Definition Module.h:127
typename InstantiatedEventFuncTraitsType::EventFuncPtrType EventFuncPtrType
Signature of an event function. Pointer to ModuleInstance to be passed as parameter instead of refere...
Definition Module.h:140
typename InstantiatedEventFuncTraitsType::EventFuncType EventFuncType
Type of a Util::CallableMemberWrapper to store the event.
Definition Module.h:141
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition DynExpCore.h:127
Common base class for all events to store them in a FIFO queue to be invoked later.
Definition Module.h:68
EventBase()=default
virtual ~EventBase()=0
Definition Module.cpp:150
void Invoke(ModuleInstance &Instance) const
Invokes the event passing the receiving module's instance reference to it. Only to be called from Mod...
Definition Module.h:78
virtual void InvokeChild(ModuleInstance &Instance) const =0
Invokes the event passing the receiving module's instance reference to it. Only to be called from Mod...
Common base class for all managers of event listeners of type TypedEventListeners....
Definition Module.h:913
virtual void Deregister(const ModuleBase &Listener, ItemIDType CommunicatorID, const std::chrono::milliseconds Timeout=std::chrono::milliseconds(0))=0
Deregisters/unsubscribes module Listener from the event, removing a single registration for a particu...
virtual ~EventListenersBase()
Definition Module.h:916
virtual void Deregister(const ModuleBase &Listener, const std::chrono::milliseconds Timeout=std::chrono::milliseconds(0))=0
Deregisters/unsubscribes module Listener from the event, regardless of the inter-module communicator ...
Common base class for all inter-module events.
Definition Module.h:1153
virtual std::string GetName() const =0
Returns the name of this event type.
virtual InterModuleEventPtrType Clone(ItemIDType CommunicatorID) const =0
Creates a deep copy of this inter-module instance.
const ItemIDType CommunicatorID
ID of the DynExpInstr::InterModuleCommunicator instance that sends the event.
Definition Module.h:1200
InterModuleEventBase(const InterModuleEventBase &Other, ItemIDType CommunicatorID)
Copy-constrcuts an inter-module event setting the CommunicatorID.
Definition Module.h:1170
virtual size_t GetID() const noexcept=0
Returns the unique ID of this event type.
std::unique_ptr< InterModuleEventBase > InterModuleEventPtrType
Pointer type to store an inter-module event (InterModuleEventBase).
Definition Module.h:1158
auto GetCommunicatorID() const noexcept
Getter for CommunicatorID.
Definition Module.h:1194
InterModuleEventBase()
Constructs an inter-module event.
Definition Module.h:1163
virtual ~InterModuleEventBase()=0
Definition Module.cpp:428
Library type that holds factory functions to all available inter-module events.
Definition Module.h:1324
std::map< size_t, EventFactoryFuncPtrType > Events
Maps the unique ID of inter-module events derived from InterModuleEventBase to their respective facto...
Definition Module.h:1361
InterModuleEventLibrary()=default
Constructs an instance of InterModuleEventLibrary. Private to make singleton class.
static InterModuleEventLibrary & Get()
Getter for the singleton instance of InterModuleEventLibrary.
Definition Module.cpp:432
std::function< InterModuleEventBase::InterModuleEventPtrType(void)> EventFactoryFuncPtrType
Type of a function pointer pointing to a factory function to create an instance of an inter-module ev...
Definition Module.h:1330
void Register(size_t ID, EventFactoryFuncPtrType EventFactoryFuncPtr)
Adds an inter-module event derived from InterModuleEventBase to this library.
Definition Module.h:1343
auto & GetEvents() const noexcept
Getter for Events.
Definition Module.h:1349
Typed base class for inter-module events to realize CRTP.
Definition Module.h:1211
static void Register(const ModuleBase &Listener, CallableT EventFunc, ItemIDType CommunicatorID=ItemIDNotSet)
Registers/Subscribes module Listener to the event with the event function EventFunc....
Definition Module.h:1262
static const size_t EventID
Unique ID assigned to this inter-module event.
Definition Module.h:1311
static size_t Publish(InterModuleEventLibrary &Library)
Publishes this event type to the InterModuleEventLibrary. This function should not be called manually...
Definition Module.h:1365
InterModuleEvent()=default
Constructs an inter-module event.
virtual void InvokeWithParamsChild(ModuleInstance &Instance, EventListenersType::EventFunctionType EventFunc) const =0
Called by InvokeChild(). Override to call EventFunc with Instance as the first argument and with furt...
virtual size_t GetID() const noexcept override final
Returns the unique ID of this event type.
Definition Module.h:1254
virtual ~InterModuleEvent()
Definition Module.h:1231
virtual void InvokeChild(ModuleInstance &Instance) const override final
Invokes the event passing the receiving module's instance reference to it. Only to be called from Mod...
Definition Module.h:1286
static InterModuleEventPtrType Make()
Factory function for events of type DerivedEvent.
Definition Module.h:1251
virtual InterModuleEventPtrType Clone(ItemIDType CommunicatorID) const override final
Creates a deep copy of this inter-module instance.
Definition Module.h:1253
static void Deregister(const ModuleBase &Listener, ItemIDType CommunicatorID)
Deregisters/unsubscribes module Listener from the event, removing a single registration for a particu...
Definition Module.h:1280
InterModuleEvent(const InterModuleEventBase &Other, ItemIDType CommunicatorID)
Copy-constrcuts an inter-module event setting the CommunicatorID.
Definition Module.h:1228
static auto ID()
Getter for EventID.
Definition Module.h:1237
static void Deregister(const ModuleBase &Listener)
Deregisters/unsubscribes module Listener from the event, regardless of the inter-module communicator ...
Definition Module.h:1271
static EventListenersType Listeners
Holds one EventListenersType instance per derived event, which manages all the subscribers of Derived...
Definition Module.h:1317
virtual std::string GetName() const override
Returns the name of this event type.
Definition Module.h:1255
Allow exclusive access to some of ModuleBase's private methods to any TypedEventListeners class.
Definition Module.h:461
ModuleBase & Parent
Owning ModuleBase instance.
Definition Module.h:476
constexpr EventListenersOnlyType(ModuleBase &Parent) noexcept
Construcs an instance - one for each ModuleBase instance.
Definition Module.h:471
void AddRegisteredEvent(EventListenersBase &EventListeners) const
Adds a manager of event listeners to RegisteredEvents. Called indirectly by TypedEventListeners::Regi...
Definition Module.h:473
void RemoveRegisteredEvent(EventListenersBase &EventListeners) const
Removes a manager of event listeners from RegisteredEvents if it was added before....
Definition Module.h:474
Allow exclusive access to some of ModuleBase's private methods to the module thread ModuleThreadMain(...
Definition Module.h:434
void OnPause(ModuleInstance &Instance)
Definition Module.h:447
ModuleBase & Parent
Owning ModuleBase instance.
Definition Module.h:453
void SetException(std::exception_ptr Exception) noexcept
Sets this module instance to an error state and tries to store the exception responsible for the erro...
Definition Module.h:446
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(ModuleInstance &Instance)
Definition Module.h:445
void OnError(ModuleInstance &Instance)
Definition Module.h:449
constexpr ModuleThreadOnlyType(ModuleBase &Parent) noexcept
Construcs an instance - one for each ModuleBase instance.
Definition Module.h:442
void SetReasonWhyPaused(std::string Description)
Definition Module.h:451
friend int ModuleThreadMain(std::unique_ptr< RunnableInstance > &&, RunnableObject *)
Modules run in their own thread. This is the module thread's main function.
Definition Module.cpp:10
void HandleEvent(ModuleInstance &Instance)
Definition Module.h:444
void OnResume(ModuleInstance &Instance)
Definition Module.h:448
Base class for modules. Modules implement programs on their own (e.g. measurement protocols or server...
Definition Module.h:428
void TerminateChild(const std::chrono::milliseconds Timeout) override final
Signals derived classes that terminating the RunnableObject instance's thread is about to be requeste...
Definition Module.cpp:360
ModuleDataTypeSyncPtrType GetModuleData(const std::chrono::milliseconds Timeout=GetModuleDataTimeoutDefault)
Locks the mutex of the module data class instance ModuleData assigned to this ModuleBase instance and...
Definition Module.cpp:219
virtual void OnErrorChild(ModuleInstance &Instance) const
This handler gets called just before the module thread terminates due to an exception....
Definition Module.h:718
void OnPause(ModuleInstance &Instance)
This handler gets called just after the module pauses due to e.g. an error in an instrument the modul...
Definition Module.cpp:298
void NotifyChild() override final
Notify derived classes that some state has changed (e.g. the termination of Thread is requested) and ...
Definition Module.cpp:355
void RunChild() override final
Refer to Run().
Definition Module.cpp:344
void ModuleSetReasonWhyPaused(std::string Description)
Sets the reason why this ModuleBase instance has been paused.
Definition Module.h:739
void OnDeregisterEvents(ModuleInstance *Instance) const
This event is triggered after the OnExit event. It calls EventListenersBase::Deregister() for all man...
Definition Module.cpp:406
static auto GetExceptionUnsafe(const ModuleDataTypeSyncPtrConstType &ModuleDataPtr)
Getter for ModuleDataBase::ModuleException assuming that the module data has already been locked.
Definition Module.h:647
virtual void ResetImpl(dispatch_tag< ModuleBase >)=0
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual void OnInit(ModuleInstance *Instance) const
This event is triggered right before the module thread starts. Override it to lock instruments this m...
Definition Module.h:764
const std::unique_ptr< ModuleDataType > ModuleData
Module data belonging to this ModuleBase instance.
Definition Module.h:788
virtual ~ModuleBase()=0
Definition Module.cpp:215
void ResetImpl(dispatch_tag< RunnableObject >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
Definition Module.cpp:336
virtual void OnResumeChild(ModuleInstance &Instance) const
This handler gets called just after the module resumed from a paused state. Override OnResumeChild() ...
Definition Module.h:717
void SetException(std::exception_ptr Exception) noexcept
Sets this module instance to an error state and tries to store the exception responsible for the erro...
Definition Module.cpp:284
void UpdateParamsFromWindowStates()
UpdateParamsFromWindowStates() only calls UpdateParamsFromWindowStatesChild(). Override UpdateParamsF...
Definition Module.h:571
void MakeAndEnqueueEvent(ReceiverType *Receiver, EventType EventFuncPtr, ArgsTs &&...Args) const
Calls MakeEvent() to construct a new event and subsequently enqueues the event into the module's even...
Definition Module.h:827
virtual std::string GetCategory() const override
Returns the category of this Object type.
Definition Module.h:516
virtual void OnExit(ModuleInstance *Instance) const
This event is triggered right before the module thread terminates (not due to an exception,...
Definition Module.h:773
void EnqueueEvent(ModuleDataBase::EventPtrType &&Event) const
Enqueues Event at the module event queue's back. Takes ownership of the event. Notifies the module ow...
Definition Module.cpp:229
void RemoveRegisteredEvent(EventListenersBase &EventListeners)
Removes a manager of event listeners from RegisteredEvents if it was added before....
Definition Module.cpp:266
void HandleEvent(ModuleInstance &Instance)
Executes and removes the next pending event from the module's event queue.
Definition Module.cpp:239
virtual void RestoreWindowStatesFromParamsChild()
RestoreWindowStatesFromParams() only calls RestoreWindowStatesFromParamsChild(). Override RestoreWind...
Definition Module.h:720
Util::SynchronizedPointer< const ModuleDataType > ModuleDataTypeSyncPtrConstType
Alias for the return type of ModuleBase::GetModuleData() const. Data class instances wrapped into Uti...
Definition Module.h:499
void OnResume(ModuleInstance &Instance)
This handler gets called just after the module resumed from a paused state. Override OnResumeChild() ...
Definition Module.cpp:305
ModuleThreadOnlyType ModuleThreadOnly
Allow exclusive access to some of ModuleBase's private methods to the module thread ModuleThreadMain(...
Definition Module.h:552
Util::SynchronizedPointer< ModuleDataType > ModuleDataTypeSyncPtrType
Alias for the return type of ModuleBase::GetModuleData(). Data class instances wrapped into Util::Syn...
Definition Module.h:493
EventListenersOnlyType EventListenersOnly
Allow exclusive access to some of ModuleBase's private methods to any TypedEventListeners class.
Definition Module.h:553
std::unique_ptr< BusyDialog > MakeStartupBusyDialogChild(QWidget *ParentWidget) const override final
Override to make this function return a pointer to a BusyDialog instance. Refer to Run().
Definition Module.cpp:377
std::vector< EventListenersBase * > RegisteredEvents
Holds a list of pointers to managers of event listeners of the events this module has registered/subs...
Definition Module.h:797
void OnError(ModuleInstance &Instance)
This handler gets called just before the module thread terminates due to an exception....
Definition Module.cpp:312
static constexpr auto Category() noexcept
Every derived class has to redefine this function.
Definition Module.h:505
virtual Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(ModuleInstance &Instance)=0
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
void AddRegisteredEvent(EventListenersBase &EventListeners)
Adds a manager of event listeners to RegisteredEvents. Called indirectly by TypedEventListeners::Regi...
Definition Module.cpp:259
virtual void UpdateParamsFromWindowStatesChild()
UpdateParamsFromWindowStates() only calls UpdateParamsFromWindowStatesChild(). Override UpdateParamsF...
Definition Module.h:721
static constexpr auto GetModuleDataTimeoutDefault
Determines the default timeout for GetModuleData() to lock the mutex synchronizing the module's data ...
Definition Module.h:550
std::exception_ptr GetExceptionChild(const std::chrono::milliseconds Timeout) const override final
Returns a pointer to the exception which has caused this Object instance to fail.
Definition Module.cpp:385
bool IsReadyChild() const override final
Returns wheter this Object instance is ready (e.g. it is running or connected to a hardware device) a...
Definition Module.cpp:395
virtual void OnPauseChild(ModuleInstance &Instance) const
This handler gets called just after the module pauses due to e.g. an error in an instrument the modul...
Definition Module.h:716
ModuleDataTypeSyncPtrType GetNonConstModuleData(const std::chrono::milliseconds Timeout=GetModuleDataTimeoutDefault) const
Always allows ModuleBase to obtain a non-const pointer to the module's data - even in const event fun...
Definition Module.cpp:234
virtual bool TreatModuleExceptionsAsWarnings() const
Determines whether this module should be terminated if an exception leaves the module's main loop or ...
Definition Module.h:535
void RestoreWindowStatesFromParams()
RestoreWindowStatesFromParams() only calls RestoreWindowStatesFromParamsChild(). Override RestoreWind...
Definition Module.h:564
virtual std::chrono::milliseconds GetMainLoopDelay() const
Specifies in which time intervals the module's event queue runs to handle pending events.
Definition Module.h:543
Util::DynExpErrorCodes::DynExpErrorCodes ExecModuleMainLoop(ModuleInstance &Instance)
Runs ModuleMainLoop().
Definition Module.cpp:277
virtual bool HasUI() const noexcept
Determines whether this module possesses a user interface (UI) which is shown in a window dedicated t...
Definition Module.h:527
Configurator class for ModuleBase.
Definition Module.h:410
virtual ~ModuleConfiguratorBase()=0
Definition Module.cpp:202
Allow exclusive access to some of ModuleDataBase's private methods to ModuleBase.
Definition Module.h:198
auto & GetNewEventNotifier() noexcept
Getter for NewEventNotifier.
Definition Module.h:212
ModuleDataBase & Parent
Owning ModuleDataBase instance.
Definition Module.h:214
void IndicateException() noexcept
Indicates to the main thread that an exception has happened in the module thread. Only performs atomi...
Definition Module.h:209
void Reset()
Resets the ModuleDataBase's instance and calls ResetImpl(dispatch_tag<ModuleDataBase>) subsequently.
Definition Module.h:208
constexpr ModuleBaseOnlyType(ModuleDataBase &Parent) noexcept
Construcs an instance - one for each ModuleDataBase instance.
Definition Module.h:206
void SetException(std::exception_ptr Exception) noexcept
Setter for ModuleException.
Definition Module.h:210
Allow exclusive access to some of ModuleDataBase's private methods to the module thread ModuleThreadM...
Definition Module.h:222
auto & GetNewEventNotifier() noexcept
Getter for NewEventNotifier.
Definition Module.h:232
ModuleDataBase & Parent
Owning ModuleDataBase instance.
Definition Module.h:234
constexpr ModuleThreadOnlyType(ModuleDataBase &Parent) noexcept
Construcs an instance - one for each ModuleDataBase instance.
Definition Module.h:230
friend int ModuleThreadMain(std::unique_ptr< RunnableInstance > &&, RunnableObject *)
Modules run in their own thread. This is the module thread's main function.
Definition Module.cpp:10
Data structure to contain data which is synchronized in between different threads....
Definition Module.h:173
ModuleThreadOnlyType ModuleThreadOnly
Allow exclusive access to some of ModuleDataBase's private methods to the module thread ModuleThreadM...
Definition Module.h:302
virtual ~ModuleDataBase()
Definition Module.h:239
const auto & GetEventFront() const noexcept
Returns a pointer to the event in the front of the module's event queue without transferring ownershi...
Definition Module.h:267
std::exception_ptr ModuleException
Used to transfer exceptions from the module thread to the main (user interface) thread....
Definition Module.h:366
Util::OneToOneNotifier NewEventNotifier
Notifies the thread of the module which owns the respective ModuleDataBase's instance when an event h...
Definition Module.h:352
std::queue< EventPtrType > EventQueueType
A module's event queue is a FIFO queue owning the enqueued events.
Definition Module.h:183
std::unique_ptr< EventBase > EventPtrType
Pointer owning an event.
Definition Module.h:178
void IndicateException() noexcept
Indicates to the main thread that an exception has happened in the module thread. Only performs atomi...
Definition Module.h:334
void SetException(std::exception_ptr Exception) noexcept
Setter for ModuleException.
Definition Module.cpp:191
void RunQueue()
Wakes up a module waiting for events and forces it to run its main loop once.
Definition Module.h:284
std::exception_ptr GetException() const noexcept
Getter for ModuleDataBase::ModuleException. If ModuleDataBase::ModuleException is nullptr and ModuleD...
Definition Module.cpp:174
Util::OneToOneNotifier & GetNewEventNotifier() noexcept
Getter for NewEventNotifier.
Definition Module.h:309
bool IsExceptionIndicated() const noexcept
Getter for HasException. Only performs atomic operations. Hence, this module data instance does not n...
Definition Module.h:291
void Reset()
Resets the ModuleDataBase's instance and calls ResetImpl(dispatch_tag<ModuleDataBase>) subsequently.
Definition Module.cpp:182
auto & GetEventFront() noexcept
Returns a pointer to the event in the front of the module's event queue without transferring ownershi...
Definition Module.h:273
size_t GetNumEnqueuedEvents() const noexcept
Getter for the module event queue's length.
Definition Module.h:279
void EnqueueEvent(EventPtrType &&Event)
Enqueues Event at the module event queue's back. Takes ownership of the event. Notifies the module ow...
Definition Module.cpp:154
EventQueueType EventQueue
FIFO event queue of the module which owns the respective ModuleDataBase's instance.
Definition Module.h:345
std::atomic< bool > HasException
If set to true, indicates to the main (user interface) thread that an exception has happened in the m...
Definition Module.h:359
virtual void ResetImpl(dispatch_tag< ModuleDataBase >)
Refer to DynExp::ModuleDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of ...
Definition Module.h:326
EventPtrType PopEvent()
Removes one event from the event queue's front and returns the event. Ownership of the event is trans...
Definition Module.cpp:163
ModuleBaseOnlyType ModuleBaseOnly
Allow exclusive access to some of ModuleDataBase's private methods to ModuleBase.
Definition Module.h:301
Refer to ParamsBase::dispatch_tag.
Definition Module.h:191
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition Module.h:840
void Exit() noexcept
Might be called from anywhere where this ModuleInstance instance is accessible to make the associated...
Definition Module.h:860
const ModuleBase::ModuleDataGetterType ModuleDataGetter
Getter for module's data. Refer to ModuleBase::ModuleDataGetterType.
Definition Module.h:872
bool IsExiting() const noexcept
Getter for ShouldExit.
Definition Module.h:867
bool ShouldExit
Indicates whether the module thread using this ModuleInstance instance should terminate.
Definition Module.h:879
Parameter class for ModuleBase.
Definition Module.h:373
std::unique_ptr< ModuleDataBase > ModuleData
Just used temporarily during the construction of a module. Refer to MakeModule() and ModuleBase::Modu...
Definition Module.h:401
DummyParam Dummy
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
Definition Module.h:403
bool ConfigureUsageTypeChild() const noexcept override final
Determines whether the Usage parameter should be configurable in the settings dialog....
Definition Module.h:395
void ConfigureParamsImpl(dispatch_tag< RunnableObjectParams >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition Module.h:391
friend ModulePtrType MakeModule(const std::thread::id, ParamsBasePtrType &&)
Factory function to generate a module of a specific type.
Definition Module.h:45
virtual void ConfigureParamsImpl(dispatch_tag< ModuleParamsBase >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition Module.h:392
virtual ~ModuleParamsBase()=0
Definition Module.cpp:198
ModuleParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a ModuleBase instance.
Definition Module.h:384
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
Definition Module.h:388
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition Object.h:2303
Refer to ParamsBase::dispatch_tag.
Definition Object.h:2018
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
Definition Object.h:522
Abstract base class for object parameter classes. Each class derived from class Object must be accomp...
Definition Object.h:326
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition Object.h:349
Base class for modules with a Qt-based user interface. Derive from this class to implement modules wi...
Definition Module.h:1660
WidgetType * GetWidget() const
Getter for Widget.
Definition Module.h:1851
QModuleWidget * Widget
User interface widget belonging to the module.
Definition Module.h:1807
void Connect(SenderType *Sender, SignalType Signal, ReceiverType *Receiver, EventType Event)
Uses Qt's connect mechanism to connect a QObject's signal to a DynExp module's event....
Definition Module.h:1816
bool HasUI() const noexcept override final
Determines whether this module possesses a user interface (UI) which is shown in a window dedicated t...
Definition Module.h:1674
QMdiArea * MdiArea
Pointer to DynExpManager's QMdiArea.
Definition Module.h:1812
std::unique_ptr< QModuleDockWidget > DockWidget
Frame for Widget when the module window is undocked from MdiArea.
Definition Module.h:1809
std::unique_ptr< QMdiSubWindow > MdiSubWindow
Frame for Widget when the module window is docked to MdiArea.
Definition Module.h:1808
std::unique_ptr< QAction > ModuleWindowFocusAction
Qt action to push module window into focus. When triggered, QModuleWidget::OnFocusWindow() in invoked...
Definition Module.h:1810
Configurator class for QModuleBase.
Definition Module.h:1646
Data class for QModuleBase.
Definition Module.h:1536
virtual ~QModuleDataBase()=default
virtual void ResetImpl(dispatch_tag< QModuleDataBase >)
Definition Module.h:1543
Provides a frame for QModuleWidget windows, which are undocked from the DynExpManager's QMdiArea.
Definition Module.h:1495
QModuleBase & Owner
Module owning this user interface window.
Definition Module.h:1529
Parameter class for QModuleBase.
Definition Module.h:1622
virtual void ConfigureParamsImpl(dispatch_tag< QModuleParamsBase >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition Module.h:1639
WindowStyleParamsExtension WindowStyleParams
Bundles several parameters to describe a UI window's style. Use in parameter classes.
Definition Module.h:1635
void ConfigureParamsImpl(dispatch_tag< ModuleParamsBase >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition Module.h:1638
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
Definition Module.h:1633
QModuleParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a QModuleBase instance.
Definition Module.h:1628
Window class for Qt-based user interfaces belonging to DynExp modules. User interface Qt window class...
Definition Module.h:1395
void OnDockWindow()
Qt slot called when DockWindowShortcut is activated, calls QModuleBase::DockUndockWindow().
Definition Module.cpp:496
virtual bool AllowResize() const noexcept
Indicates the resizing behavior of the user interface window. Override to adjust.
Definition Module.h:1437
friend class QModuleBase
Definition Module.h:1398
Qt::WindowFlags GetQtWindowFlags() const noexcept
Depending on thr return value of AllowResize(), returns either the return value of GetQtWindowFlagsRe...
Definition Module.cpp:460
void OnFocusWindow()
Qt slot called when QModuleBase::ModuleWindowFocusAction is triggered, calls QModuleBase::SetFocus().
Definition Module.cpp:501
virtual void closeEvent(QCloseEvent *Event) override
Qt event indicating that the module window is closed. Asks the user whether to terminate the module....
Definition Module.cpp:480
QModuleBase & Owner
Module owning this user interface window (reference, because it should never change nor be nullptr).
Definition Module.h:1478
static constexpr Qt::WindowFlags GetQtWindowFlagsResizable()
Default Qt window flags for resizable module windows.
Definition Module.cpp:439
void OnFocusMainWindow()
Qt slot called when FocusMainWindowShortcut is activated, calls QModuleBase::FocusMainWindow().
Definition Module.cpp:506
static constexpr Qt::WindowFlags GetQtWindowFlagsNonResizable()
Default Qt window flags for non-resizable module windows.
Definition Module.cpp:445
const auto & GetOwner() const noexcept
Getter for the owning module.
Definition Module.h:1427
QShortcut * FocusMainWindowShortcut
Shortcut (Ctrl + 0) to activate/focus DynExp's main window.
Definition Module.h:1482
std::string GetDataSaveDirectory() const
Recalls a path where modules might save recorded data to. Used by Util::PromptSaveFilePathModule() to...
Definition Module.cpp:465
QShortcut * DockWindowShortcut
Shortcut (Ctrl + D) to dock/undock a module window to/from DynExp's main window.
Definition Module.h:1481
void SetDataSaveDirectory(std::string_view Directory) const
Sets a path where modules might save recorded data to. Used by Util::PromptSaveFilePathModule() to st...
Definition Module.cpp:470
void EnableDockWindowShortcut(bool Enable) noexcept
Enables/disables DockWindowShortcut.
Definition Module.cpp:475
DynExpManager * DynExpMgr
DynExp's main window (pointer, because it is set later by QModuleBase::InitUI()).
Definition Module.h:1479
Defines data for a thread belonging to a RunnableObject instance. This data is only accessed by the R...
Definition Object.h:3513
const RunnableObject & Owner
RunnableObject instance which operates on this RunnableInstance (by its thread). The RunnableObject i...
Definition Object.h:3746
std::promise< void > ThreadExitedPromise
Signals the RunnableObject instance owning the thread that its thread has terminated....
Definition Object.h:3760
Configurator class for RunnableObject.
Definition Object.h:2412
Parameter class for RunnableObject.
Definition Object.h:2349
Defines an Object which possesses a thread it runs in. The RunnableObject can be started and stopped ...
Definition Object.h:2426
void SetReasonWhyPaused(std::string Description)
Sets the reason why this RunnableObject instance has been paused.
Definition Object.h:2607
Typed managers of event listeners class whose instances are owned by classes derived from InterModule...
Definition Module.h:951
virtual ~TypedEventListeners()=default
void DeregisterAllUnsafe(const ModuleBase &Listener)
This function is not thread-safe (assuming EventListenersBase's mutex has already been locked before ...
Definition Module.h:1114
std::unordered_map< const ListenersTypeKey, EventFunctionType, ListenersTypeKeyHasher > Listeners
Each module can register to each inter-module event with one event function of type EventFunctionType...
Definition Module.h:1146
EventFunctionType GetFunc(const ModuleBase &Listener, ItemIDType CommunicatorID=ItemIDNotSet, const std::chrono::milliseconds Timeout=DefaultTimeout) const
Looks up the event function the module Listener has registered/subscribed with.
Definition Module.h:1060
virtual void Deregister(const ModuleBase &Listener, const std::chrono::milliseconds Timeout=std::chrono::milliseconds(0)) override
Deregisters/unsubscribes module Listener from the event, regardless of the inter-module communicator ...
Definition Module.h:1044
EventFunctionType GetFuncUnsafe(const ModuleBase &Listener, ItemIDType CommunicatorID) const
This function is the version of GetFunc() which is not thread-safe (assuming EventListenersBase's mut...
Definition Module.h:1130
virtual void Deregister(const ModuleBase &Listener, ItemIDType CommunicatorID, const std::chrono::milliseconds Timeout=std::chrono::milliseconds(0)) override
Deregisters/unsubscribes module Listener from the event, removing a single registration for a particu...
Definition Module.h:1037
void DeregisterUnsafe(const ModuleBase &Listener, ItemIDType CommunicatorID)
This function is not thread-safe (assuming EventListenersBase's mutex has already been locked before ...
Definition Module.h:1098
void Register(const ModuleBase &Listener, CallableT EventFunc, ItemIDType CommunicatorID=ItemIDNotSet, const std::chrono::milliseconds Timeout=std::chrono::milliseconds(0))
Registers/Subscribes module Listener to the event with the event function EventFunc....
Definition Module.h:1030
std::function< void(ModuleInstance *, EventFuncArgs...)> EventFunctionType
Type of event functions to be invoked when the event is triggered. The first ModuleInstance argument ...
Definition Module.h:1008
void RegisterUnsafe(const ModuleBase &Listener, CallableT EventFunc, ItemIDType CommunicatorID)
This function is not thread-safe (assuming EventListenersBase's mutex has already been locked before ...
Definition Module.h:1079
Bundles several parameters to describe a UI window's style. Use in parameter classes.
Definition Module.h:1550
WindowStyleParamsExtension(ParamsBase &Owner, const std::string &Prefix="")
Constructs a WindowStyleParamsExtension instance.
Definition Module.h:1588
WindowDockingStateType
Indicates the window docking state.
Definition Module.h:1572
ParamsBase::Param< unsigned int > WindowWidth
Window width.
Definition Module.h:1612
ParamsBase::Param< int > WindowPosY
Window y coordinate.
Definition Module.h:1611
WindowStateType
Indicates the window state for showing it.
Definition Module.h:1555
ParamsBase::Param< WindowDockingStateType > WindowDockingState
Window docking state.
Definition Module.h:1615
ParamsBase::Param< int > WindowPosX
Window x coordinate.
Definition Module.h:1610
ParamsBase::Param< WindowStateType > WindowState
Window show state.
Definition Module.h:1614
ParamsBase::Param< unsigned int > WindowHeight
Window height.
Definition Module.h:1613
Wraps a member function of some object and stores its default arguments. Moving from CallableMemberWr...
Definition Util.h:448
void Log(const std::string &Message, const ErrorType Type=ErrorType::Info, const size_t Line=0, const std::string &Function="", const std::string &File="", const int ErrorCode=0, const std::stacktrace &Trace={}) noexcept
Logs an event from information specified manually.
Definition Util.cpp:317
DynExp exceptions are derived from this class. It contains basic information about the cause of the e...
Definition Exception.h:51
Interface to allow synchronizing the access to derived classes between different threads by providing...
Definition Util.h:56
LockType AcquireLock(const std::chrono::milliseconds Timeout=DefaultTimeout) const
Locks the internal mutex. Blocks until the mutex is locked or until the timeout duration is exceeded.
Definition Util.cpp:8
static constexpr std::chrono::milliseconds DefaultTimeout
Duration which is used as a default timeout within all methods of this class if no different duration...
Definition Util.h:62
Interface to allow synchronizing the access to derived classes between different threads by making th...
Definition Util.h:93
An invalid argument like a null pointer has been passed to a function.
Definition Exception.h:138
An operation cannot be performed currently since the related object is in an invalid state like an er...
Definition Exception.h:151
Helper class to communicate flags between different threads based on a condition variable and a mutex...
Definition Util.h:265
void Notify()
Set notification to stop waiting (sets EventOccurred to true).
Definition Util.cpp:69
Pointer to lock a class derived from ISynchronizedPointerLockable for synchronizing between threads....
Definition Util.h:170
DynExp's module namespace contains the implementation of DynExp modules which extend DynExp's core fu...
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
int ModuleThreadMain(std::unique_ptr< RunnableInstance > &&InstancePtr, RunnableObject *BaseObject)
Modules run in their own thread. This is the module thread's main function.
Definition Module.cpp:10
ConfiguratorBasePtrType MakeModuleConfig()
Factory function to generate a configurator for a specific module type.
Definition Module.h:32
ModulePtrType MakeModule(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Factory function to generate a module of a specific type.
Definition Module.h:45
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
auto MakeEvent(ReceiverType *Receiver, typename DefaultEvent< ReceiverType, std::tuple< std::remove_reference_t< ArgsTs >... > >::EventFuncPtrType EventFuncPtr, ArgsTs &&...Args)
Creates an event of type DefaultEvent.
Definition Module.h:810
auto dynamic_ModuleData_cast(Util::SynchronizedPointer< From > &&ModuleDataPtr)
Casts the data base class From into a derived ModuleBase's (To) data class keeping the data locked by...
Definition Module.h:898
std::unique_ptr< ModuleBase > ModulePtrType
Pointer type to store a module (DynExp::ModuleBase) with.
Definition Module.h:24
std::shared_ptr< ConfiguratorBase > ConfiguratorBasePtrType
Alias for a pointer to the configurator base class ConfiguratorBase.
Definition Object.h:1959
DynExpErrorCodes
DynExp's error codes
Definition Exception.h:22
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
typename remove_first_from_tuple< TupleT >::type remove_first_from_tuple_t
Alias for a tuple of types where the first type of the input tuple TupleT is removed.
Definition Util.h:392
EventLogger & EventLog()
This function holds a static EventLogger instance and returns a reference to it. DynExp uses only one...
Definition Util.cpp:517
void HashCombine(std::size_t &seed, const T &value)
Combines the std::hash seed with the hash of value. Resembles hash_combine() from the Boost library p...
Definition Util.h:611
typename member_fn_ptr_traits< CallableT >::argument_types argument_of_t
Alias for a tuple of argument types the member function callable of type CallableT expects.
Definition Util.h:374
Accumulates include statements to provide a precompiled header.
Helper struct to allow accessing elements within ArgTupleType.
Definition Module.h:105
void(ReceiverType::*)(ModuleInstance *, std::tuple_element_t< Indices, ArgTupleType >...) const EventFuncPtrType
Signature of an event function. Pointer to ModuleInstance to be passed as parameter instead of refere...
Definition Module.h:113
Hasher required to use ListenersTypeKey as the key type for the std::unordered_map Listeners.
Definition Module.h:986
size_t operator()(const ListenersTypeKey &v) const
Computes the hash of v.
Definition Module.h:992
Key type of the unordered_map storing module/inter-module communicator combinations that registered t...
Definition Module.h:957
const ModuleBase * Module
Module that registered to the event.
Definition Module.h:961
bool operator==(const ListenersTypeKey &Other) const
Checks for equality of Module and CommunicatorID.
Definition Module.h:975
const ItemIDType CommunicatorID
ID of the inter-module communicator instrument (instance of DynExpInstr::InterModuleCommunicator) bel...
Definition Module.h:968