DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Managers.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
4 #include "Managers.h"
5 #include "DynExpManager.h"
6 
7 namespace DynExp
8 {
9  std::thread::id CommonResourceManagerBase::GetOwnerThreadID(const DynExpCore& Core) noexcept
10  {
11  return Core.GetOwnerThreadID();
12  }
13 
15  {
16  const auto NotConnectedResource = std::find_if(cbegin(), cend(), [](const auto& i) {
17  const auto ExceptionPtr = i.second.ResourcePointer->GetException();
18  if (ExceptionPtr)
19  std::rethrow_exception(ExceptionPtr);
20 
21  return !i.second.ResourcePointer->IsConnected();
22  });
23 
24  // No not-connected resource found, std::find_if() returns iterator to end
25  return NotConnectedResource == cend();
26  }
27 
29  {
30  // Tries to connect all hardware adapters and does not stop in case of an exception.
31  // The first occurring exception is instead stored and rethrown afterwards. All further
32  // exceptions are swallowed here. This is not optimal, but still better than having
33  // some hardware adapters being stuck in a "connecting" state due to an exception of
34  // another hardware adapter.
35  std::exception_ptr FirstException;
36 
37  std::for_each(cbegin(), cend(), [FunctionToCallWhenObjectStarted, &FirstException](const auto& i) {
38  try
39  {
40  i.second.ResourcePointer->EnsureReadyState(false);
41 
42  if (FunctionToCallWhenObjectStarted)
43  FunctionToCallWhenObjectStarted(i.second.ResourcePointer.get());
44  }
45  catch (...)
46  {
47  if (!FirstException)
48  FirstException = std::current_exception();
49  }
50  });
51 
52  if (FirstException)
53  std::rethrow_exception(FirstException);
54  }
55 
56  QDomElement HardwareAdapterManager::MakeXMLConfigHeadNode(QDomDocument& Document) const
57  {
58  QDomElement ManagerNode = Document.createElement("HardwareAdapters");
59 
60  return ManagerNode;
61  }
62 
64  {
65  return std::count_if(cbegin(), cend(), [](const auto& i) { return i.second.ResourcePointer->IsRunning(); });
66  }
67 
69  {
70  const auto NotInitializedResource = std::find_if(cbegin(), cend(), [](const auto& i) {
71  const auto ExceptionPtr = i.second.ResourcePointer->GetException();
72  if (ExceptionPtr)
73  std::rethrow_exception(ExceptionPtr);
74 
75  return !i.second.ResourcePointer->IsInitialized() && i.second.ResourcePointer->GetStartupType() == RunnableObjectParams::StartupType::OnCreation;
76  });
77 
78  // No not-initialized resource found, std::find_if() returns iterator to end
79  return NotInitializedResource == cend();
80  }
81 
83  {
84  std::for_each(cbegin(), cend(), [](const auto& i) { i.second.ResourcePointer->Terminate(); });
85  }
86 
87  void InstrumentManager::StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const
88  {
89  std::for_each(cbegin(), cend(), [FunctionToCallWhenObjectStarted](const auto& i) {
90  const bool HasBeenStarted = i.second.ResourcePointer->RunIfRunOnCreation();
91 
92  if (HasBeenStarted && FunctionToCallWhenObjectStarted)
93  FunctionToCallWhenObjectStarted(i.second.ResourcePointer.get());
94  });
95  }
96 
97  QDomElement InstrumentManager::MakeXMLConfigHeadNode(QDomDocument& Document) const
98  {
99  QDomElement ManagerNode = Document.createElement("Instruments");
100 
101  return ManagerNode;
102  }
103 
105  {
106  return std::count_if(cbegin(), cend(), [](const auto& i) { return i.second.ResourcePointer->IsRunning(); });
107  }
108 
110  {
111  std::for_each(cbegin(), cend(), [](const auto& i) { i.second.ResourcePointer->Terminate(); });
112  }
113 
115  {
116  std::for_each(cbegin(), cend(), [](const auto& i) { i.second.ResourcePointer->RestoreWindowStatesFromParams(); });
117  }
118 
120  {
121  std::for_each(cbegin(), cend(), [](const auto& i) { i.second.ResourcePointer->UpdateParamsFromWindowStates(); });
122  }
123 
124  void ModuleManager::StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const
125  {
126  std::for_each(cbegin(), cend(), [FunctionToCallWhenObjectStarted](const auto& i) {
127  const bool HasBeenStarted = i.second.ResourcePointer->RunIfRunOnCreation();
128 
129  if (HasBeenStarted && FunctionToCallWhenObjectStarted)
130  FunctionToCallWhenObjectStarted(i.second.ResourcePointer.get());
131  });
132  }
133 
134  QDomElement ModuleManager::MakeXMLConfigHeadNode(QDomDocument& Document) const
135  {
136  QDomElement ManagerNode = Document.createElement("Modules");
137 
138  return ManagerNode;
139  }
140 }
Implements DynExp's main UI window called DynExpManager.
Implementation of DynExp's resource managers to manage DynExp objects.
const std::function< void(Object *const)> FunctionToCallWhenObjectStartedType
Type of a callback function to invoke after a resource has been started in a call to ResourceManagerB...
Definition: Managers.h:65
static std::thread::id GetOwnerThreadID(const DynExpCore &Core) noexcept
Getter for the thread id of the thread which constructed (and owns) Core. Implementation necessary in...
Definition: Managers.cpp:9
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition: DynExpCore.h:127
virtual QDomElement MakeXMLConfigHeadNode(QDomDocument &Document) const override
Creates and returns the XML root node of the (single) resource manager instance of the derived manage...
Definition: Managers.cpp:56
virtual void StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const override
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
Definition: Managers.cpp:28
bool AllConnected() const
Determines whether all hardware adapters managed by this resource manager are connected....
Definition: Managers.cpp:14
virtual QDomElement MakeXMLConfigHeadNode(QDomDocument &Document) const override
Creates and returns the XML root node of the (single) resource manager instance of the derived manage...
Definition: Managers.cpp:97
bool AllInitialized() const
Determines whether all instruments managed by this resource manager are initialized....
Definition: Managers.cpp:68
virtual void StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const override
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
Definition: Managers.cpp:87
void TerminateAll() const
Calls RunnableObject::Terminate() on all instruments managed by this resource manager....
Definition: Managers.cpp:82
size_t GetNumRunningInstruments() const
Counts the instruments managed by this resource manager which are running (as determined by RunnableO...
Definition: Managers.cpp:63
void RestoreWindowStatesFromParams() const
Calls ModuleBase::RestoreWindowStatesFromParams() on all modules managed by this resource manager....
Definition: Managers.cpp:114
virtual void StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const override
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
Definition: Managers.cpp:124
virtual QDomElement MakeXMLConfigHeadNode(QDomDocument &Document) const override
Creates and returns the XML root node of the (single) resource manager instance of the derived manage...
Definition: Managers.cpp:134
void TerminateAll() const
Calls RunnableObject::Terminate() on all modules managed by this resource manager....
Definition: Managers.cpp:109
void UpdateParamsFromWindowStates() const
Calls ModuleBase::UpdateParamsFromWindowStates() on all modules managed by this resource manager....
Definition: Managers.cpp:119
size_t GetNumRunningModules() const
Counts the modules managed by this resource manager which are running (as determined by RunnableObjec...
Definition: Managers.cpp:104
auto cend() const noexcept
Returns an iterator behind the last resource stored in the resource manager.
Definition: Managers.h:226
auto cbegin() const noexcept
Returns an iterator to the first resource stored in the resource manager.
Definition: Managers.h:220
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
Accumulates include statements to provide a precompiled header.