DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Instrument.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 
13 namespace DynExp
14 {
15  class InstrumentBase;
16  class InstrumentInstance;
17  class ExceptionContainer;
18  class TaskBase;
19  class InitTaskBase;
20  class ExitTaskBase;
21  class UpdateTaskBase;
22 
26  using InstrumentPtrType = std::shared_ptr<InstrumentBase>;
27 
33  template <typename InstrumentT>
35  {
36  return std::make_shared<typename InstrumentT::ConfigType>();
37  }
38 
46  template <typename InstrumentT>
47  InstrumentPtrType MakeInstrument(const std::thread::id OwnerThreadID, ParamsBasePtrType&& Params)
48  {
49  dynamic_Params_cast<InstrumentT>(Params.get())->InstrumentData = std::make_unique<typename InstrumentT::InstrumentDataType>();
50 
51  return std::make_shared<InstrumentT>(OwnerThreadID, std::move(Params));
52  }
53 
62  template <typename TaskT, typename... ArgTs>
63  std::unique_ptr<TaskT> MakeTask(ArgTs&& ...Args)
64  {
65  return std::make_unique<TaskT>(std::forward<ArgTs>(Args)...);
66  }
67 
78  int InstrumentThreadMain(InstrumentInstance Instance, InstrumentBase* const Instrument);
79 
86  {
87  public:
92  ExceptionContainer(const std::exception_ptr& Exception = nullptr) noexcept
93  : Exception(Exception) {}
94 
98  void Throw() const { if (Exception) std::rethrow_exception(Exception); }
99 
104  bool IsError() const { return static_cast<bool>(Exception); }
105 
110  auto GetException() const { return Exception; }
111 
117  void ClearError() { Exception = nullptr; }
118 
119  private:
120  std::exception_ptr Exception;
121  };
122 
135  {
136  public:
137  using TaskQueueType = std::list<std::unique_ptr<TaskBase>>;
138  using TaskQueueIteratorType = TaskQueueType::const_iterator;
139 
140  protected:
145  template <typename>
146  struct dispatch_tag {};
147 
148  private:
153  {
154  friend class InstrumentDataBase;
155  friend class InstrumentBase;
156 
162 
163  void Reset() { Parent.Reset(); }
164  void EnqueueTask(std::unique_ptr<TaskBase>&& Task, bool CallFromInstrThread, bool NotifyReceiver) { Parent.EnqueueTask(std::move(Task), CallFromInstrThread, NotifyReceiver); }
165  void EnqueuePriorityTask(std::unique_ptr<TaskBase>&& Task, bool CallFromInstrThread, bool NotifyReceiver) { Parent.EnqueuePriorityTask(std::move(Task), CallFromInstrThread, NotifyReceiver); }
169  void CloseQueue() { Parent.CloseQueue(); }
170 
171  auto& GetNewTaskNotifier() noexcept { return Parent.GetNewTaskNotifier(); }
172 
174  };
175 
181  {
182  friend class InstrumentDataBase;
184 
190 
191  auto& GetNewTaskNotifier() noexcept { return Parent.GetNewTaskNotifier(); }
192  void SetLastUpdateTime(std::chrono::system_clock::time_point LastUpdate) { Parent.LastUpdate = LastUpdate; }
194 
196  };
197 
198  public:
200  virtual ~InstrumentDataBase() {}
201 
202  public:
208 
213  void EnqueueTask(std::unique_ptr<TaskBase>&& Task) { EnqueueTask(std::move(Task), false, true); }
214 
220  void EnqueuePriorityTask(std::unique_ptr<TaskBase>&& Task) { EnqueuePriorityTask(std::move(Task), false, true); }
221 
229  std::unique_ptr<TaskBase> PopTaskFront();
230 
238  std::unique_ptr<TaskBase> PopTaskBack();
239 
244  auto GetTaskFront() noexcept { return TaskQueue.begin(); }
245 
250  auto GetTaskBack() noexcept { return TaskQueue.end(); }
251 
256  size_t GetNumEnqueuedTasks() const noexcept { return TaskQueue.size(); }
257 
264  std::unique_ptr<TaskBase> PopFinishedTask();
265 
270  size_t GetNumFinishedTasks() const noexcept { return FinishedTasks.size(); }
271 
276  bool IsQueueClosed() const noexcept { return QueueClosed; }
278 
283  auto GetLastUpdateTime() const { return LastUpdate; }
284 
289  auto GetException() const noexcept { return InstrumentException; }
290 
293 
294  private:
303  void EnqueueTask(std::unique_ptr<TaskBase>&& Task, bool CallFromInstrThread, bool NotifyReceiver);
304 
313  void EnqueuePriorityTask(std::unique_ptr<TaskBase>&& Task, bool CallFromInstrThread, bool NotifyReceiver);
314 
321 
325  void RemoveAllTasks();
326 
332 
336  void CloseQueue() { QueueClosed = true; }
337 
343 
348 
352  void Reset();
353 
361 
365  void CheckError() const;
366 
374  void CheckQueueState(bool CallFromInstrThread) const;
375 
378  bool QueueClosed;
379 
386 
387  std::chrono::system_clock::time_point LastUpdate;
388 
394  std::exception_ptr InstrumentException;
395  };
396 
401  {
402  friend class InstrumentBase;
403 
404  template <typename>
405  friend InstrumentPtrType MakeInstrument(const std::thread::id, ParamsBasePtrType&&);
406 
407  public:
413 
414  virtual ~InstrumentParamsBase() = 0;
415 
416  virtual const char* GetParamClassTag() const noexcept override { return "InstrumentParamsBase"; }
417 
418  private:
421 
426  std::unique_ptr<InstrumentDataBase> InstrumentData;
427 
428  DummyParam Dummy = { *this };
429  };
430 
435  {
436  public:
439 
441  virtual ~InstrumentConfiguratorBase() = 0;
442  };
443 
451  {
457  {
458  friend class InstrumentBase;
460 
466 
467  bool HandleTask(InstrumentInstance& Instance) { return Parent.HandleTask(Instance); }
469  void OnError() { Parent.OnError(); }
470  void SetInitialized() { Parent.Initialized = true; }
471 
473  };
474 
475  public:
478 
484 
490 
496 
497 
506 
511  constexpr static auto Category() noexcept { return "General"; }
512 
518  InstrumentBase(const std::thread::id OwnerThreadID, ParamsBasePtrType&& Params);
519 
520  virtual ~InstrumentBase() = 0;
521 
522  virtual std::string GetCategory() const override { return Category(); }
523 
525 
530 
536  virtual std::chrono::milliseconds GetTaskQueueDelay() const { return std::chrono::milliseconds(std::chrono::milliseconds::max()); }
538 
543  static constexpr auto GetInstrumentDataTimeoutDefault = std::chrono::milliseconds(1000);
544 
549 
559 
567 
573  InstrumentDataTypeSyncPtrType (InstrumentBase::*)(const std::chrono::milliseconds)>;
574 
578  void UpdateData() const;
579 
584  void EnqueueArriveAtLatchTask(std::latch& Latch) const;
585 
590  bool IsInitialized() const { return Initialized; }
592 
593  private:
598 
606 
607  protected:
612  static auto GetExceptionUnsafe(const InstrumentDataTypeSyncPtrConstType& InstrumentDataPtr) { return InstrumentDataPtr->GetException(); }
613 
626  template <typename TaskT, typename... ArgTs>
627  void MakeAndEnqueueTask(ArgTs&& ...Args) const
628  {
629  auto Task = MakeTask<TaskT>(std::forward<ArgTs>(Args)...);
630 
631  // Locks InstrumentData
632  GetNonConstInstrumentData()->InstrumentBaseOnly.EnqueueTask(std::move(Task), IsCallFromRunnableThread(), true);
633  }
634 
635  public:
648  template <typename DerivedInstrT, typename... TaskFuncArgTs, typename... ArgTs>
649  ExceptionContainer AsSyncTask(void (DerivedInstrT::* TaskFunc)(TaskFuncArgTs...) const, ArgTs&& ...Args) const
650  {
651  std::atomic<bool> FinishedFlag = false;
652  ExceptionContainer Exception;
653  auto CallbackFunc = [&FinishedFlag, &Exception](const TaskBase& Task, auto E) {
654  Exception = E;
655 
656  // Must come last!
657  FinishedFlag = true;
658  };
659 
660  (dynamic_cast<const DerivedInstrT&>(*this).*TaskFunc)(std::forward<ArgTs>(Args)..., CallbackFunc);
661 
662  while (!FinishedFlag)
663  std::this_thread::yield();
664 
665  return Exception;
666  }
667 
668  private:
673 
678  bool HandleTask(InstrumentInstance& Instance);
679 
684  void UpdateDataInternal();
685 
690  void OnError();
692 
693  void ResetImpl(dispatch_tag<RunnableObject>) override final;
695 
700  void RunChild() override final;
701  void NotifyChild() override final;
702  void TerminateChild(const std::chrono::milliseconds Timeout) override final;
703 
709  void OnPrepareExit();
711 
712  std::exception_ptr GetExceptionChild([[maybe_unused]] const std::chrono::milliseconds Timeout) const override final;
713  bool IsReadyChild() const override final;
714 
719  virtual void OnErrorChild() const {}
720  virtual void OnPrepareExitChild() const {}
721 
726  virtual bool HandleAdditionalTask() { return true; }
727 
733  virtual bool UpdateAdditionalData() { return true; }
734 
742  virtual std::unique_ptr<InitTaskBase> MakeInitTask() const { return nullptr; }
743 
751  virtual std::unique_ptr<ExitTaskBase> MakeExitTask() const { return nullptr; }
752 
760  virtual std::unique_ptr<UpdateTaskBase> MakeUpdateTask() const { return nullptr; }
762 
763  const std::unique_ptr<InstrumentDataType> InstrumentData;
764  std::atomic<bool> Initialized = false;
765  };
766 
772  {
773  public:
780 
785 
786  ~InstrumentInstance() = default;
787 
792  };
793 
807  template <typename To, typename From, std::enable_if_t<
808  std::is_same_v<InstrumentDataBase, std::remove_cv_t<From>>, int> = 0
809  >
811  {
812  if (!InstrumentDataPtr)
813  throw Util::InvalidArgException("InstrumentDataPtr must not be nullptr.");
814 
816  std::conditional_t<std::is_const_v<From>, std::add_const_t<typename To::InstrumentDataType>, typename To::InstrumentDataType>
817  >(std::move(InstrumentDataPtr));
818  }
819 
824  {
825  public:
842  enum class AbortedType : bool { NotAborted, Aborted };
859  const AbortedType Aborted = AbortedType::NotAborted, const int ErrorCode = 0) noexcept
861 
866  constexpr bool ShouldContinue() const noexcept { return ContinueTaskHandling == ContinueTaskHandlingType::Continue; }
867 
872  constexpr bool HasAborted() const noexcept { return Aborted == AbortedType::Aborted; }
873 
878  constexpr int GetErrorCode() const noexcept { return ErrorCode; }
879 
880  private:
883  const int ErrorCode;
884  };
885 
891  class TaskBase
892  {
897  {
898  friend class TaskBase;
899  friend class InstrumentBase;
900 
906 
907  void Lock() { Parent.Lock(); }
908  bool Run(InstrumentInstance& Instance) { return Parent.Run(Instance); }
909 
911  };
912 
917  {
918  friend class TaskBase;
919  friend class InstrumentDataBase;
920 
926 
927  bool KeepFinishedTask() const noexcept { return Parent.KeepFinishedTask(); }
928 
930  };
931 
932  public:
939  using CallbackType = std::function<void(const TaskBase&, ExceptionContainer&)>;
940 
979  TaskBase(CallbackType CallbackFunc = nullptr) noexcept
981  CallbackFunc(std::move(CallbackFunc)), State(TaskState::Waiting), ErrorCode(0), ShouldAbort(false) {}
982 
987  virtual ~TaskBase() = 0;
988 
993 
997  TaskState GetState() const noexcept { return State; }
998 
1003  bool IsLocked() const noexcept;
1004 
1010  bool IsAborting() const noexcept { return ShouldAbort; }
1011 
1016  int GetErrorCode() const noexcept { return ErrorCode; }
1017 
1023  void Abort() { ShouldAbort = true; }
1025 
1028 
1029  private:
1034  void Lock();
1035 
1050  bool Run(InstrumentInstance& Instance);
1051 
1056  virtual TaskResultType RunChild(InstrumentInstance& Instance) = 0;
1057 
1064  virtual bool KeepFinishedTask() const noexcept { return false; }
1066 
1073 
1078  std::atomic<TaskState> State;
1079  std::atomic<int> ErrorCode;
1081 
1086  std::atomic<bool> ShouldAbort;
1088  };
1089 
1096  class DefaultTask final : public TaskBase
1097  {
1098  public:
1100 
1101  private:
1102  virtual TaskResultType RunChild(InstrumentInstance& Instance) override { return {}; }
1103  };
1104 
1112  class InitTaskBase : public TaskBase
1113  {
1114  protected:
1119  template <typename>
1120  struct dispatch_tag {};
1121 
1122  private:
1123  TaskResultType RunChild(InstrumentInstance& Instance) override final;
1124 
1134  };
1135 
1143  class ExitTaskBase : public TaskBase
1144  {
1145  protected:
1150  template <typename>
1151  struct dispatch_tag {};
1152 
1153  private:
1154  TaskResultType RunChild(InstrumentInstance& Instance) override final;
1155 
1165  };
1166 
1174  class UpdateTaskBase : public TaskBase
1175  {
1176  protected:
1181  template <typename>
1182  struct dispatch_tag {};
1183 
1184  private:
1185  TaskResultType RunChild(InstrumentInstance& Instance) override final;
1186 
1196  };
1197 
1204  class ArriveAtLatchTask final : public TaskBase
1205  {
1206  public:
1213 
1219 
1220  private:
1221  virtual TaskResultType RunChild(InstrumentInstance& Instance) override;
1222 
1223  std::latch& Latch;
1224  bool HasArrived = false;
1225  };
1226 
1233  template <typename... InstrTs>
1234  void WaitForInstruments(InstrTs&... Instruments)
1235  {
1236  static_assert(std::conjunction_v<std::is_base_of<InstrumentBase, InstrTs>...>);
1237  static_assert(sizeof...(InstrTs) > 0);
1238  static_assert(sizeof...(InstrTs) <= std::latch::max());
1239 
1240  std::latch Latch(sizeof...(InstrTs));
1241  std::array<std::reference_wrapper<const InstrumentBase>, sizeof...(InstrTs)> Instrs{ Instruments... };
1242 
1243  for (auto& Instr : Instrs)
1244  Instr.get().EnqueueArriveAtLatchTask(Latch);
1245 
1246  Latch.wait();
1247  }
1248 }
1249 
1254 namespace DynExpInstr {};
Implementation of DynExp objects as the base for derived resources and implementation of the object p...
Defines a task which arrives at a std::latch when it is executed. This is useful to synchronize multi...
Definition: Instrument.h:1205
~ArriveAtLatchTask()
If the task has been aborted or never executed, the destructor arrives at the latch in order to avoid...
Definition: Instrument.cpp:550
ArriveAtLatchTask(std::latch &Latch, CallbackType CallbackFunc=nullptr)
Constructs an instrument task.
Definition: Instrument.h:1211
bool HasArrived
Indicates whether the task has already arrived at the latch.
Definition: Instrument.h:1224
std::latch & Latch
Latch the task arrives at when it is executed.
Definition: Instrument.h:1223
virtual TaskResultType RunChild(InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
Definition: Instrument.cpp:556
Default task which does not do anything. Though, calling it ensures that TaskBase::CallbackFunc gets ...
Definition: Instrument.h:1097
DefaultTask(CallbackType CallbackFunc) noexcept
Constructs an instrument task.
Definition: Instrument.h:1099
virtual TaskResultType RunChild(InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
Definition: Instrument.h:1102
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition: DynExpCore.h:127
Wrapper holding a pointer to an exception and providing functionality for accessing it....
Definition: Instrument.h:86
auto GetException() const
Getter for Exception.
Definition: Instrument.h:110
std::exception_ptr Exception
Exception stored in the wrapper.
Definition: Instrument.h:120
void ClearError()
Removes the stored exception. If this method is called by a task's CallbackFunc in case of an excepti...
Definition: Instrument.h:117
ExceptionContainer(const std::exception_ptr &Exception=nullptr) noexcept
Constructs an ExceptionContainer instance.
Definition: Instrument.h:92
bool IsError() const
Checks whether the wrapper holds an exception.
Definition: Instrument.h:104
void Throw() const
Throws the stored exception. Doesn't do anything if there isn't a stored exception.
Definition: Instrument.h:98
Defines a task for deinitializing an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1144
TaskResultType RunChild(InstrumentInstance &Instance) override final
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
Definition: Instrument.cpp:532
virtual void ExitFuncImpl(dispatch_tag< ExitTaskBase >, InstrumentInstance &Instance)=0
Deinitializes the respective instrument within the instrument inheritance hierarchy....
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1151
Defines a task for initializing an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1113
virtual void InitFuncImpl(dispatch_tag< InitTaskBase >, InstrumentInstance &Instance)=0
Initializes the respective instrument within the instrument inheritance hierarchy....
TaskResultType RunChild(InstrumentInstance &Instance) override final
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
Definition: Instrument.cpp:521
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1120
Allow exclusive access to some of InstrumentBase's private methods to the instrument thread Instrumen...
Definition: Instrument.h:457
constexpr InstrumenThreadOnlyType(InstrumentBase &Parent) noexcept
Construcs an instance - one for each InstrumentBase instance.
Definition: Instrument.h:465
void UpdateData()
Inserts an update task (UpdateTaskBase) into the instrument's task queue. Override UpdateAdditionalDa...
Definition: Instrument.h:468
InstrumentBase & Parent
Owning InstrumentBase instance.
Definition: Instrument.h:472
void OnError()
Derived classes can perform critical shutdown actions after an error has occurred....
Definition: Instrument.h:469
void SetInitialized()
Sets InstrumentBase::Initialized to true.
Definition: Instrument.h:470
bool HandleTask(InstrumentInstance &Instance)
Executes and removes the next pending task from the instrument's task queue.
Definition: Instrument.h:467
friend int InstrumentThreadMain(InstrumentInstance, InstrumentBase *const)
Instruments run in their own thread. This is the instrument thread's main function.
Definition: Instrument.cpp:8
Base class for instruments. Instruments comprise virtual devices (meta instruments) and physial devic...
Definition: Instrument.h:451
static auto GetExceptionUnsafe(const InstrumentDataTypeSyncPtrConstType &InstrumentDataPtr)
Getter for InstrumentDataBase::InstrumentException.
Definition: Instrument.h:612
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: Instrument.cpp:419
void ResetImpl(dispatch_tag< RunnableObject >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
Definition: Instrument.cpp:317
virtual bool UpdateAdditionalData()
Determines whether to enqueue update tasks (UpdateTaskBase).
Definition: Instrument.h:733
InstrumentBase(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs an instrument instance.
Definition: Instrument.cpp:210
constexpr static auto Category() noexcept
Every derived class has to redefine this function.
Definition: Instrument.h:511
bool HandleTask(InstrumentInstance &Instance)
Executes and removes the next pending task from the instrument's task queue.
Definition: Instrument.cpp:254
virtual void ResetImpl(dispatch_tag< InstrumentBase >)=0
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual std::unique_ptr< InitTaskBase > MakeInitTask() const
Factory function for an init task (InitTaskBase). Override to define the desired initialization task ...
Definition: Instrument.h:742
void UpdateData() const
Enqueues an update task (instance of class UpdateTaskBase).
Definition: Instrument.cpp:237
std::atomic< bool > Initialized
Determines whether the init task (InitTaskBase) has run.
Definition: Instrument.h:764
void OnPrepareExit()
This function enables derived classes to enqueue tasks to be executed directly before the final exit ...
Definition: Instrument.cpp:385
void EnqueueArriveAtLatchTask(std::latch &Latch) const
Enqueues a task which arrives at a latch when executed (instance of class ArriveAtLatchTask).
Definition: Instrument.cpp:247
virtual std::unique_ptr< ExitTaskBase > MakeExitTask() const
Factory function for an exit task (ExitTaskBase). Override to define the desired deinitialization tas...
Definition: Instrument.h:751
void MakeAndEnqueueTask(ArgTs &&...Args) const
Calls MakeTask() to construct a new task and subsequently enqueues the task into the instrument's tas...
Definition: Instrument.h:627
virtual std::chrono::milliseconds GetTaskQueueDelay() const
Specifies in which time intervals the instrument's task queue runs to handle pending tasks.
Definition: Instrument.h:536
virtual void OnPrepareExitChild() const
This function enables derived classes to enqueue tasks to be executed directly before the final exit ...
Definition: Instrument.h:720
void UpdateDataInternal()
Inserts an update task (UpdateTaskBase) into the instrument's task queue. Override UpdateAdditionalDa...
Definition: Instrument.cpp:280
static constexpr auto GetInstrumentDataTimeoutDefault
Determines the default timeout for GetInstrumentData() to lock the mutex synchronizing the instrument...
Definition: Instrument.h:543
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: Instrument.cpp:347
virtual void OnErrorChild() const
Derived classes can perform critical shutdown actions after an error has occurred....
Definition: Instrument.h:719
void RunChild() override final
Refer to Run().
Definition: Instrument.cpp:327
std::exception_ptr GetExceptionChild([[maybe_unused]] const std::chrono::milliseconds Timeout) const override final
Definition: Instrument.cpp:409
InstrumentDataTypeSyncPtrType GetInstrumentData(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault)
Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase ...
Definition: Instrument.cpp:222
virtual std::unique_ptr< UpdateTaskBase > MakeUpdateTask() const
Factory function for an update task (UpdateTaskBase). Override to define the desired update task in d...
Definition: Instrument.h:760
Util::SynchronizedPointer< InstrumentDataType > InstrumentDataTypeSyncPtrType
Alias for the return type of InstrumentBase::GetInstrumentData(). Data class instances wrapped into U...
Definition: Instrument.h:489
InstrumenThreadOnlyType InstrumentThreadOnly
Allow exclusive access to some of InstrumentBase's private methods to the instrument thread Instrumen...
Definition: Instrument.h:524
virtual bool HandleAdditionalTask()
Determines whether task handling should continue.
Definition: Instrument.h:726
ExceptionContainer AsSyncTask(void(DerivedInstrT::*TaskFunc)(TaskFuncArgTs...) const, ArgTs &&...Args) const
Calls a (derived) instrument's function which inserts a task into the instrument's task queue synchro...
Definition: Instrument.h:649
InstrumentDataTypeSyncPtrType GetNonConstInstrumentData(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault) const
Always allows InstrumentBase to obtain a non-const pointer to the instrument's data - even in const t...
Definition: Instrument.cpp:232
void OnError()
Derived classes can perform critical shutdown actions after an error has occurred....
Definition: Instrument.cpp:290
virtual ~InstrumentBase()=0
Definition: Instrument.cpp:218
virtual std::string GetCategory() const override
Returns the category of this Object type.
Definition: Instrument.h:522
const std::unique_ptr< InstrumentDataType > InstrumentData
Instrument data belonging to this InstrumentBase instance.
Definition: Instrument.h:763
bool IsInitialized() const
Getter for Initialized.
Definition: Instrument.h:590
void NotifyChild() override final
Notify derived classes that some state has changed (e.g. the termination of Thread is requested) and ...
Definition: Instrument.cpp:340
Configurator class for InstrumentBase.
Definition: Instrument.h:435
Allow exclusive access to some of InstrumentDataBase's private methods to the instrument thread Instr...
Definition: Instrument.h:181
constexpr InstrumenThreadOnlyType(InstrumentDataBase &Parent) noexcept
Construcs an instance - one for each InstrumentDataBase instance.
Definition: Instrument.h:189
void SetException(std::exception_ptr InstrumentException) noexcept
Setter for InstrumentDataBase::InstrumentException.
Definition: Instrument.h:193
InstrumentDataBase & Parent
Owning InstrumentDataBase instance.
Definition: Instrument.h:195
void SetLastUpdateTime(std::chrono::system_clock::time_point LastUpdate)
Setter for InstrumentDataBase::LastUpdate.
Definition: Instrument.h:192
friend int InstrumentThreadMain(InstrumentInstance, InstrumentBase *const)
Instruments run in their own thread. This is the instrument thread's main function.
Definition: Instrument.cpp:8
auto & GetNewTaskNotifier() noexcept
Getter for NewTaskNotifier.
Definition: Instrument.h:191
Allow exclusive access to some of InstrumentDataBase's private methods to InstrumentBase.
Definition: Instrument.h:153
auto & GetNewTaskNotifier() noexcept
Getter for NewTaskNotifier.
Definition: Instrument.h:171
constexpr InstrumentBaseOnlyType(InstrumentDataBase &Parent) noexcept
Construcs an instance - one for each InstrumentDataBase instance.
Definition: Instrument.h:161
void Reset()
Resets the InstrumentDataBase's instance and calls ResetImpl(dispatch_tag<InstrumentDataBase>) subseq...
Definition: Instrument.h:163
InstrumentDataBase & Parent
Owning InstrumentDataBase instance.
Definition: Instrument.h:173
void CloseQueue()
Clsoes the instrument's task queue setting QueueClosed to true.
Definition: Instrument.h:169
void RemoveTaskFromQueue(TaskQueueIteratorType &Task)
Removes a task from the instrument's task queue and inserts it into the instrument's list of finished...
Definition: Instrument.h:166
void RemoveAllTasks()
Clears the instrument's task queue.
Definition: Instrument.h:167
void EnqueuePriorityTask(std::unique_ptr< TaskBase > &&Task, bool CallFromInstrThread, bool NotifyReceiver)
Enqueues a task at the front of an instrument's task queue and notifies the instrument about the new ...
Definition: Instrument.h:165
void EnqueueTask(std::unique_ptr< TaskBase > &&Task, bool CallFromInstrThread, bool NotifyReceiver)
Enqueues a task at the back of an instrument's task queue and notifies the instrument about the new t...
Definition: Instrument.h:164
void RemoveAllTasksExceptFront()
Clears the instrument's task queue but keeps the front task (the task with highest priority which is ...
Definition: Instrument.h:168
Data structure to contain data which is synchronized in between different threads....
Definition: Instrument.h:135
Util::OneToOneNotifier & GetNewTaskNotifier() noexcept
Getter for NewTaskNotifier.
Definition: Instrument.h:342
virtual void ResetImpl(dispatch_tag< InstrumentDataBase >)
Refer to DynExp::InstrumentDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()...
Definition: Instrument.h:359
std::list< std::unique_ptr< TaskBase > > TaskQueueType
Type of an instrument task queue owning the tasks within.
Definition: Instrument.h:137
TaskQueueType TaskQueue
FIFO task queue of the instrument owning this InstrumentDataBase instance.
Definition: Instrument.h:376
std::chrono::system_clock::time_point LastUpdate
Time point when the instrument thread called InstrumentBase::UpdateDataInternal() the last time.
Definition: Instrument.h:387
auto GetLastUpdateTime() const
Getter for LastUpdate.
Definition: Instrument.h:283
void CheckError() const
Throws InstrumentException if it is not nullptr using Util::ForwardException().
Definition: Instrument.cpp:189
bool IsQueueClosed() const noexcept
Determines whether the instrument task queue is closed.
Definition: Instrument.h:276
void Reset()
Resets the InstrumentDataBase's instance and calls ResetImpl(dispatch_tag<InstrumentDataBase>) subseq...
Definition: Instrument.cpp:178
std::unique_ptr< TaskBase > PopTaskBack()
Removes a task from the back of an instrument's task queue.
Definition: Instrument.cpp:112
void EnqueuePriorityTask(std::unique_ptr< TaskBase > &&Task)
Enqueues a task at the front of an instrument's task queue and notifies the instrument about the new ...
Definition: Instrument.h:220
InstrumentBaseOnlyType InstrumentBaseOnly
Allow exclusive access to some of InstrumentDataBase's private methods to InstrumentBase.
Definition: Instrument.h:291
void RemoveAllTasks()
Clears the instrument's task queue.
Definition: Instrument.cpp:165
TaskQueueType::const_iterator TaskQueueIteratorType
Const iterator type to elements of TaskQueueType.
Definition: Instrument.h:138
void CloseQueue()
Clsoes the instrument's task queue setting QueueClosed to true.
Definition: Instrument.h:336
auto GetTaskFront() noexcept
Getter for first enqueued task.
Definition: Instrument.h:244
void EnqueueTask(std::unique_ptr< TaskBase > &&Task)
Enqueues a task at the back of an instrument's task queue and notifies the instrument about the new t...
Definition: Instrument.h:213
std::unique_ptr< TaskBase > PopTaskFront()
Removes a task from the front of an instrument's task queue.
Definition: Instrument.cpp:98
Util::OneToOneNotifier NewTaskNotifier
Used to notify the instrument thread about new tasks when enqueuing tasks into the task queue....
Definition: Instrument.h:385
InstrumenThreadOnlyType InstrumentThreadOnly
Allow exclusive access to some of InstrumentDataBase's private methods to the instrument thread Instr...
Definition: Instrument.h:292
size_t GetNumFinishedTasks() const noexcept
Getter for the length of the instrument's list of finished tasks.
Definition: Instrument.h:270
auto GetException() const noexcept
Getter for InstrumentDataBase::InstrumentException.
Definition: Instrument.h:289
std::exception_ptr InstrumentException
Used to transfer exceptions from the instrument thread to the main thread. Stores the exception respo...
Definition: Instrument.h:394
std::unique_ptr< TaskBase > PopFinishedTask()
Removes a task from the front of an instrument's list of finished tasks.
Definition: Instrument.cpp:126
void RemoveAllTasksExceptFront()
Clears the instrument's task queue but keeps the front task (the task with highest priority which is ...
Definition: Instrument.cpp:170
void CheckQueueState(bool CallFromInstrThread) const
Checks whether it is currently allowed to enqueue tasks into the instrument task queue.
Definition: Instrument.cpp:194
size_t GetNumEnqueuedTasks() const noexcept
Getter for the instrument task queue's length.
Definition: Instrument.h:256
void RemoveTaskFromQueue(TaskQueueIteratorType &Task)
Removes a task from the instrument's task queue and inserts it into the instrument's list of finished...
Definition: Instrument.cpp:157
bool QueueClosed
If set to true, no new tasks can be enqueued (useful if an instrument is e.g. stopped).
Definition: Instrument.h:378
auto GetTaskBack() noexcept
Getter for last enqueued task.
Definition: Instrument.h:250
TaskQueueType FinishedTasks
List of the instrument's finished tasks. Tasks are moved here from TaskQueue after completion.
Definition: Instrument.h:377
Refer to ParamsBase::dispatch_tag.
Definition: Instrument.h:146
Defines data for a thread belonging to a InstrumentBase instance. Refer to RunnableInstance.
Definition: Instrument.h:772
const InstrumentBase::InstrumentDataGetterType InstrumentDataGetter
Getter for instrument's data. Refer to InstrumentBase::InstrumentDataGetterType.
Definition: Instrument.h:791
InstrumentInstance(InstrumentBase &Owner, std::promise< void > &&ThreadExitedPromise, const InstrumentBase::InstrumentDataGetterType InstrumentDataGetter)
Constructs a non-empty RunnableInstance instance.
Definition: Instrument.cpp:430
Parameter class for InstrumentBase.
Definition: Instrument.h:401
virtual void ConfigureParamsImpl(dispatch_tag< InstrumentParamsBase >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition: Instrument.h:420
DummyParam Dummy
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
Definition: Instrument.h:428
std::unique_ptr< InstrumentDataBase > InstrumentData
Just used temporarily during the construction of an instrument. Refer to MakeInstrument() and to Inst...
Definition: Instrument.h:426
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: Instrument.h:416
InstrumentParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a InstrumentBase instance.
Definition: Instrument.h:412
void ConfigureParamsImpl(dispatch_tag< RunnableObjectParams >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition: Instrument.h:419
friend InstrumentPtrType MakeInstrument(const std::thread::id, ParamsBasePtrType &&)
Factory function to generate an instrument of a specific type.
Definition: Instrument.h:47
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
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
Defines data for a thread belonging to a RunnableObject instance. This data is only accessed by the R...
Definition: Object.h:3505
const RunnableObject & Owner
RunnableObject instance which operates on this RunnableInstance (by its thread). The RunnableObject i...
Definition: Object.h:3705
std::promise< void > ThreadExitedPromise
Signals the RunnableObject instance owning the thread that its thread has terminated....
Definition: Object.h:3712
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
bool IsCallFromRunnableThread() const
Checks whether Thread's id matches the id of the calling thread. This is thread-safe if the function ...
Definition: Object.cpp:656
Allow exclusive access to some of TaskBase's private methods to InstrumentBase.
Definition: Instrument.h:897
void Lock()
Locks the task to prepare it for execution.
Definition: Instrument.h:907
constexpr InstrumentBaseOnlyType(TaskBase &Parent) noexcept
Construcs an instance - one for each TaskBase instance.
Definition: Instrument.h:905
bool Run(InstrumentInstance &Instance)
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
Definition: Instrument.h:908
TaskBase & Parent
Owning TaskBase instance.
Definition: Instrument.h:910
Allow exclusive access to some of TaskBase's private methods to InstrumentDataBase.
Definition: Instrument.h:917
TaskBase & Parent
Owning TaskBase instance.
Definition: Instrument.h:929
bool KeepFinishedTask() const noexcept
Determines whether the task should be kept to check its results after execution. InstrumentDataBase::...
Definition: Instrument.h:927
constexpr InstrumentDataBaseOnlyType(TaskBase &Parent) noexcept
Construcs an instance - one for each TaskBase instance.
Definition: Instrument.h:925
Base class for all tasks being processed by instruments. The class must not contain public virtual fu...
Definition: Instrument.h:892
void Abort()
Requests the task to abort. There is no guarantee that the derived task does call IsAborting() to che...
Definition: Instrument.h:1023
void Lock()
Locks the task to prepare it for execution.
Definition: Instrument.cpp:473
std::function< void(const TaskBase &, ExceptionContainer &)> CallbackType
Type of a callback function which is invoked when a task has finished, failed or has been aborted....
Definition: Instrument.h:939
int GetErrorCode() const noexcept
Getter for the error code related to an error possibly occurred while the task was executed.
Definition: Instrument.h:1016
bool IsAborting() const noexcept
Determines whether the task should abort. A derived task is encouraged to call this method before per...
Definition: Instrument.h:1010
virtual TaskResultType RunChild(InstrumentInstance &Instance)=0
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
TaskBase(CallbackType CallbackFunc=nullptr) noexcept
Constructs an instrument task.
Definition: Instrument.h:979
std::atomic< int > ErrorCode
Holds the error code of an error which occurred during execution of the task function.
Definition: Instrument.h:1079
std::atomic< TaskState > State
Holds the task's current state. Refer to TaskBase::TaskState.
Definition: Instrument.h:1078
InstrumentBaseOnlyType InstrumentBaseOnly
Allow exclusive access to some of TaskBase's private methods to InstrumentBase.
Definition: Instrument.h:1026
bool IsLocked() const noexcept
Determines whether the task is locked.
Definition: Instrument.cpp:465
bool Run(InstrumentInstance &Instance)
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
Definition: Instrument.cpp:481
InstrumentDataBaseOnlyType InstrumentDataBaseOnly
Allow exclusive access to some of TaskBase's private methods to InstrumentDataBase.
Definition: Instrument.h:1027
std::atomic< bool > ShouldAbort
Indicates whether the task should abort. Refer to Abort() and IsAborting().
Definition: Instrument.h:1086
const CallbackType CallbackFunc
This callback function is called after the task has finished (either successfully or not) with a refe...
Definition: Instrument.h:1072
TaskState GetState() const noexcept
Getter for the instrument task's current state.
Definition: Instrument.h:997
virtual bool KeepFinishedTask() const noexcept
Determines whether the task should be kept to check its results after execution. InstrumentDataBase::...
Definition: Instrument.h:1064
virtual ~TaskBase()=0
The destructor aborts a waiting task setting State to TaskState::Aborted. Then, it calls CallbackFunc...
Definition: Instrument.cpp:442
TaskState
Defines states an instrument's task can undergo. Possible state transitions are:
Definition: Instrument.h:948
Defines the return type of task functions.
Definition: Instrument.h:824
constexpr int GetErrorCode() const noexcept
Getter for the error code of an error which occurred during execution of the task function.
Definition: Instrument.h:878
const int ErrorCode
DynExp error code from DynExpErrorCodes::DynExpErrorCodes. Anything else than 0 indicates an error.
Definition: Instrument.h:883
constexpr TaskResultType(const ContinueTaskHandlingType ContinueTaskHandling=ContinueTaskHandlingType::Continue, const AbortedType Aborted=AbortedType::NotAborted, const int ErrorCode=0) noexcept
Constructs a TaskResultType instance.
Definition: Instrument.h:858
const AbortedType Aborted
Determines whether a task has been aborted.
Definition: Instrument.h:882
constexpr bool ShouldContinue() const noexcept
Determines whether the instrument having handled this task should continue or terminate.
Definition: Instrument.h:866
const ContinueTaskHandlingType ContinueTaskHandling
Determines whether an instrument should terminate after handling the task.
Definition: Instrument.h:881
constexpr bool HasAborted() const noexcept
Determines whether this task has been aborted.
Definition: Instrument.h:872
AbortedType
Determines whether a task has been aborted.
Definition: Instrument.h:842
ContinueTaskHandlingType
Determines whether an instrument should terminate after handling the task.
Definition: Instrument.h:829
Defines a task for updating an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1175
TaskResultType RunChild(InstrumentInstance &Instance) override final
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
Definition: Instrument.cpp:543
virtual void UpdateFuncImpl(dispatch_tag< UpdateTaskBase >, InstrumentInstance &Instance)=0
Updates the respective instrument within the instrument inheritance hierarchy. Call UpdateFuncImpl() ...
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1182
Wraps a member function of some object and stores its default arguments. Moving from CallableMemberWr...
Definition: Util.h:448
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:137
Helper class to communicate flags between different threads based on a condition variable and a mutex...
Definition: Util.h:265
Pointer to lock a class derived from ISynchronizedPointerLockable for synchronizing between threads....
Definition: Util.h:170
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
Definition: Instrument.h:1254
constexpr auto Instrument
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
InstrumentPtrType MakeInstrument(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Factory function to generate an instrument of a specific type.
Definition: Instrument.h:47
std::shared_ptr< InstrumentBase > InstrumentPtrType
Pointer type to store an instrument (DynExp::InstrumentBase) with.
Definition: Instrument.h:26
void WaitForInstruments(InstrTs &... Instruments)
Blocks until every instrument passed to the function as a reference parameter has arrived at a synchr...
Definition: Instrument.h:1234
std::unique_ptr< TaskT > MakeTask(ArgTs &&...Args)
Factory function to create a task to be enqueued in an instrument's task queue.
Definition: Instrument.h:63
auto dynamic_InstrumentData_cast(Util::SynchronizedPointer< From > &&InstrumentDataPtr)
Casts the data base class From into a derived InstrumentBase's (To) data class keeping the data locke...
Definition: Instrument.h:810
int InstrumentThreadMain(InstrumentInstance Instance, InstrumentBase *const Instrument)
Instruments run in their own thread. This is the instrument thread's main function.
Definition: Instrument.cpp:8
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.
ConfiguratorBasePtrType MakeInstrumentConfig()
Factory function to generate a configurator for a specific instrument type.
Definition: Instrument.h:34
std::shared_ptr< ConfiguratorBase > ConfiguratorBasePtrType
Alias for a pointer to the configurator base class ConfiguratorBase.
Definition: Object.h:1959
Accumulates include statements to provide a precompiled header.