DynExp
Highly flexible laboratory automation for dynamically changing experiments.
DynExpCore.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 "Libraries.h"
12 #include "Managers.h"
13 
19 namespace DynExp
20 {
24  class ProjectParams final : public ParamsBase
25  {
26  friend class DynExpCore;
27 
28  public:
48 
55  MainWindowStyleParams(*this, "Main"), CircuitWindowStyleParams(*this, "Circuit") {}
56 
58 
59  const char* GetParamClassTag() const noexcept override final { return "ProjectParams"; }
60 
61  Param<ParamsConfigDialog::TextType> Authors = { *this, "Authors", "Authors", "Name of the project's authors.", false };
62  Param<ParamsConfigDialog::TextType> Version = { *this, "Version", "Version", "Current version of the project.", false };
63  Param<ParamsConfigDialog::TextType> Comment = { *this, "Comment", "Comment", "Comments related to the project.", false };
64 
68  Param<StoreWindowStatesType> StoreWindowStates = { *this, AvlblStoreWindowStatesTypeStrList(), "StoreWindowStates", "Remember window states",
69  "Determines whether module windows' geometries and states are to be remembered when loading the project from file.",
70  false, StoreWindowStatesType::ApplyStoredWindowStates };
71 
76 
83 
87  ListParam<int> HSplitterWidgetWidths = { *this, "HSplitterWidgetWidths", {}, 0 };
88 
92  ListParam<int> VSplitterWidgetHeights = { *this, "VSplitterWidgetHeights", {}, 0 };
93 
94  private:
95  void ConfigureParamsImpl(dispatch_tag<ParamsBase>) override final;
96 
102 
105  std::filesystem::path ProjectFilename;
106 
110  std::filesystem::path LastDataSaveDirectory;
112  };
113 
126  class DynExpCore final : public Util::INonCopyable
127  {
128  public:
134 
140 
145  static constexpr std::chrono::milliseconds GetParamsTimeoutDefault = std::chrono::milliseconds(100);
146 
155  ModuleLibraryVectorType ModuleLib, std::string ProjectFileToOpen = "");
156  ~DynExpCore();
157 
162  void Shutdown();
163 
171  void Reset(bool Force = false);
172 
181  void SaveProject(std::string_view Filename, const QMainWindow& MainWindow, const QDialog& CircuitDiagramDlg, QSplitter& HSplitter, QSplitter& VSplitter);
182 
189  void OpenProject(std::string_view Filename);
190 
196  void EditProjectSettings(QWidget* const DialogParent);
197 
204  auto ConnectHardwareAdapters(CommonResourceManagerBase::FunctionToCallWhenObjectStartedType FunctionToCallWhenHardwareAdapterConnecting = nullptr)
205  {
206  return std::async(std::launch::async, &HardwareAdapterManager::Startup, &HardwareAdapterMgr, FunctionToCallWhenHardwareAdapterConnecting);
207  }
208 
213  bool AllHardwareAdaptersConnected() const;
214 
219  bool AllInstrumentsInitialized() const;
220 
225  void RunInstruments(CommonResourceManagerBase::FunctionToCallWhenObjectStartedType FunctionToCallWhenInstrumentStarted = nullptr);
226 
231  void RunModules(CommonResourceManagerBase::FunctionToCallWhenObjectStartedType FunctionToCallWhenModuleStarted = nullptr);
232 
236  void ShutdownProject();
237 
244  void ResetFailedItems(QWidget& ParentWindow);
245 
256  void RestoreWindowStatesFromParams(QMainWindow& MainWindow, QDialog& CircuitDiagramDlg, QSplitter& HSplitter, QSplitter& VSplitter,
257  bool OnlyMainWindow = false);
258 
263  auto& GetHardwareAdapterLib() const noexcept { return HardwareAdapterLib; }
264 
269  auto& GetInstrumentLib() const noexcept { return InstrumentLib; }
270 
275  auto& GetModuleLib() const noexcept { return ModuleLib; }
276 
281  auto& GetHardwareAdapterManager() noexcept { return HardwareAdapterMgr; }
282 
287  auto& GetInstrumentManager() noexcept { return InstrumentMgr; }
288 
293  auto& GetModuleManager() noexcept { return ModuleMgr; }
294 
295  auto& GetHardwareAdapterManager() const noexcept { return HardwareAdapterMgr; }
296  auto& GetInstrumentManager() const noexcept { return InstrumentMgr; }
297  auto& GetModuleManager() const noexcept { return ModuleMgr; }
298 
306 
314 
322 
330 
335 
340  auto GetProjectFilename(const std::chrono::milliseconds Timeout = GetParamsTimeoutDefault) const { return GetParams(Timeout)->ProjectFilename; }
341 
347  auto GetLastDataSaveDirectory(const std::chrono::milliseconds Timeout = GetParamsTimeoutDefault) const { return GetParams(Timeout)->LastDataSaveDirectory; }
348 
354  bool IsProjectOpened(const std::chrono::milliseconds Timeout = GetParamsTimeoutDefault) const noexcept { return !GetProjectFilename(Timeout).empty(); }
355 
361  std::filesystem::path ToAbsolutePath(const std::filesystem::path& Path) const;
362 
370  ParamsConstTypeSyncPtrType GetParams(const std::chrono::milliseconds Timeout = GetParamsTimeoutDefault) const;
371 
376  const auto GetOwnerThreadID() const noexcept { return OwnerThreadID; }
378 
387  void MoveQWorkerToWorkerThread(Util::QWorker& Worker, ItemIDType ID) const;
388 
393  std::string GetDataSaveDirectory(const std::chrono::milliseconds Timeout = GetParamsTimeoutDefault) const;
394 
399  void SetDataSaveDirectory(const std::filesystem::path& Directory, const std::chrono::milliseconds Timeout = GetParamsTimeoutDefault);
400 
401  private:
407  bool OpenProjectSafe(const std::string& Filename) noexcept;
408 
421  template <typename LibEntryT, typename ParamsT, typename ManagerT>
422  ItemIDType MakeItem(const LibraryEntry<LibEntryT>& LibEntry, ParamsT&& Params, ManagerT& ResourceManager, const std::string ItemTypeName);
423 
429  ParamsTypeSyncPtrType GetParams(const std::chrono::milliseconds Timeout = GetParamsTimeoutDefault);
430 
439  void UpdateParamsFromWindowStates(const QMainWindow& MainWindow, const QDialog& CircuitDiagramDlg, QSplitter& HSplitter, QSplitter& VSplitter);
440 
444 
455 
461 
465  std::unique_ptr<ProjectParams> Params;
466 
472  const std::thread::id OwnerThreadID;
473 
484  mutable QThread WorkerThread;
485  };
486 
487  template <typename LibEntryT, typename ParamsT, typename ManagerT>
488  ItemIDType DynExpCore::MakeItem(const LibraryEntry<LibEntryT>& LibEntry, ParamsT&& Params, ManagerT& ResourceManager, const std::string ItemTypeName)
489  {
490  auto Item = LibEntry.ObjectFactoryPtr(GetOwnerThreadID(), std::move(Params));
491  auto ID = ResourceManager.InsertResource(std::move(Item));
492  auto Res = ResourceManager.GetResource(ID);
493 
494  Util::EventLog().Log(ItemTypeName + " \"" + Res->GetParams()->ObjectName.Get()
495  + "\" (" + Res->GetCategoryAndName() + ") has been created successfully.");
496 
497  return ID;
498  }
499 }
Provides templates to bundle DynExp resources at compile time in libraries.
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
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition: DynExpCore.h:127
std::unique_ptr< ProjectParams > Params
Project parameters (configuration) of the current DynExp project. Must never be nullptr.
Definition: DynExpCore.h:465
DynExpCore(HardwareAdapterLibraryVectorType HardwareAdapterLib, InstrumentLibraryVectorType InstrumentLib, ModuleLibraryVectorType ModuleLib, std::string ProjectFileToOpen="")
Constructs a DynExpCore instance.
Definition: DynExpCore.cpp:23
void SaveProject(std::string_view Filename, const QMainWindow &MainWindow, const QDialog &CircuitDiagramDlg, QSplitter &HSplitter, QSplitter &VSplitter)
Saves the current DynExp project to an XML project file.
Definition: DynExpCore.cpp:81
auto & GetModuleManager() const noexcept
Getter for the module manager.
Definition: DynExpCore.h:297
static constexpr std::chrono::milliseconds GetParamsTimeoutDefault
Default timeout used by DynExpCore::GetParams() to lock the mutex of the project parameter instance.
Definition: DynExpCore.h:145
auto ConnectHardwareAdapters(CommonResourceManagerBase::FunctionToCallWhenObjectStartedType FunctionToCallWhenHardwareAdapterConnecting=nullptr)
Connects all hardware adapters contained in HardwareAdapterMgr asynchronously calling ResourceManager...
Definition: DynExpCore.h:204
void ShutdownProject()
Terminates all running instruments and modules.
Definition: DynExpCore.cpp:184
bool AllInstrumentsInitialized() const
Checks whether all instruments contained in InstrumentMgr have been initialized successfully.
Definition: DynExpCore.cpp:169
auto & GetHardwareAdapterLib() const noexcept
Getter for the hardware adapter library.
Definition: DynExpCore.h:263
auto & GetModuleManager() noexcept
Getter for the module manager.
Definition: DynExpCore.h:293
void OpenProject(std::string_view Filename)
Loads a DynExp project from an XML project file.
Definition: DynExpCore.cpp:114
bool IsProjectOpened(const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const noexcept
Locks Params and determines whether a project has been openend from a project file.
Definition: DynExpCore.h:354
bool LoadedProjectFromCommandlineParams
This flag will be set to true if DynExpCore has been initialized with a path to a project file to loa...
Definition: DynExpCore.h:460
bool OpenProjectSafe(const std::string &Filename) noexcept
Calls OpenProject() and performs error handling for that function.
Definition: DynExpCore.cpp:313
HardwareAdapterManager HardwareAdapterMgr
Hardware adapter manager owning all instantiated hardware adapters.
Definition: DynExpCore.h:451
const HardwareAdapterLibraryVectorType HardwareAdapterLib
Hardware adapter library vector containing information about all hardware adapters DynExp knows.
Definition: DynExpCore.h:441
auto & GetHardwareAdapterManager() noexcept
Getter for the hardware adapter manager.
Definition: DynExpCore.h:281
void EditProjectSettings(QWidget *const DialogParent)
Opens a settings dialog (ParamsConfigDialog) to let the user configure the parameter values of the cu...
Definition: DynExpCore.cpp:156
void RestoreWindowStatesFromParams(QMainWindow &MainWindow, QDialog &CircuitDiagramDlg, QSplitter &HSplitter, QSplitter &VSplitter, bool OnlyMainWindow=false)
Sets module and main windows' positions, sizes and styles according to parameters stored in the curre...
Definition: DynExpCore.cpp:231
QThread WorkerThread
One worker thread runs the Qt event queues for all objects derived from Util::QWorker (e....
Definition: DynExpCore.h:484
auto & GetInstrumentManager() noexcept
Getter for the instrument manager.
Definition: DynExpCore.h:287
void Reset(bool Force=false)
Resets the currently loaded project removing all resources from the resource managers....
Definition: DynExpCore.cpp:58
bool HasLoadedProjectFromCommandlineParams() noexcept
Indicates whether a DynExp project has been loaded from a path specified as a command line argument w...
Definition: DynExpCore.cpp:271
ModuleManager ModuleMgr
Module manager owning all instantiated modules.
Definition: DynExpCore.h:453
const InstrumentLibraryVectorType InstrumentLib
Instrument library vector containing information about all instruments DynExp knows.
Definition: DynExpCore.h:442
const auto GetOwnerThreadID() const noexcept
Getter for the thread id of the thread which constructed (and owns) this DynExpCore instance.
Definition: DynExpCore.h:376
auto & GetModuleLib() const noexcept
Getter for the module library.
Definition: DynExpCore.h:275
void MoveQWorkerToWorkerThread(Util::QWorker &Worker, ItemIDType ID) const
Moves a Util::QWorker instance to WorkerThread to run its Qt event queue there. This method is thread...
Definition: DynExpCore.cpp:290
void SetDataSaveDirectory(const std::filesystem::path &Directory, const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault)
Sets a path where modules might save recorded data to. Used by Util::PromptSaveFilePathModule() to st...
Definition: DynExpCore.cpp:308
const ModuleLibraryVectorType ModuleLib
Module library vector containing information about all modules DynExp knows.
Definition: DynExpCore.h:443
auto GetLastDataSaveDirectory(const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
Locks Params and returns the path to a directory where modules can save data.
Definition: DynExpCore.h:347
auto GetProjectFilename(const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
Locks Params and returns the current DynExp project's filename.
Definition: DynExpCore.h:340
auto & GetHardwareAdapterManager() const noexcept
Getter for the hardware adapter manager.
Definition: DynExpCore.h:295
void RunModules(CommonResourceManagerBase::FunctionToCallWhenObjectStartedType FunctionToCallWhenModuleStarted=nullptr)
Runs all modules contained in ModuleMgr with RunnableObjectParams::StartupType::OnCreation startup se...
Definition: DynExpCore.cpp:179
ParamsConstTypeSyncPtrType GetParams(const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
Locks the mutex of the parameter class instance Params assigned to the current project and returns a ...
Definition: DynExpCore.cpp:285
void UpdateParamsFromWindowStates(const QMainWindow &MainWindow, const QDialog &CircuitDiagramDlg, QSplitter &HSplitter, QSplitter &VSplitter)
Retrieves the module and main windows' positions, sizes and styles from the windows and updates the p...
Definition: DynExpCore.cpp:361
InstrumentManager InstrumentMgr
Instrument manager owning all instantiated instruments.
Definition: DynExpCore.h:452
const std::thread::id OwnerThreadID
The ID is set by DynExpCore::DynExpCore() to the id of the thread which constructed the DynExpCore in...
Definition: DynExpCore.h:472
void RunInstruments(CommonResourceManagerBase::FunctionToCallWhenObjectStartedType FunctionToCallWhenInstrumentStarted=nullptr)
Runs all instruments contained in InstrumentMgr with RunnableObjectParams::StartupType::OnCreation st...
Definition: DynExpCore.cpp:174
ItemIDType MakeItem(const LibraryEntry< HardwareAdapterPtrType > &LibEntry, ParamsBasePtrType &&Params)
Creates a DynExp::HardwareAdapterBase instance from a LibraryEntry.
Definition: DynExpCore.cpp:256
bool AllHardwareAdaptersConnected() const
Checks whether all hardware adapters contained in HardwareAdapterMgr have been connected successfully...
Definition: DynExpCore.cpp:164
Util::SynchronizedPointer< const ProjectParams > ParamsConstTypeSyncPtrType
Alias for the return type of DynExpCore::GetParams() const. Parameters wrapped into Util::Synchronize...
Definition: DynExpCore.h:139
auto & GetInstrumentManager() const noexcept
Getter for the instrument manager.
Definition: DynExpCore.h:296
void ResetFailedItems(QWidget &ParentWindow)
Calls Object::Reset() and Object::ClearWarning() on all owned DynExp::Object instances which are in a...
Definition: DynExpCore.cpp:190
std::string GetDataSaveDirectory(const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
Recalls a path where modules might save recorded data to. Used by Util::PromptSaveFilePathModule() to...
Definition: DynExpCore.cpp:298
std::filesystem::path ToAbsolutePath(const std::filesystem::path &Path) const
Transforms the path Path into an absolute path relative to ProjectParams::ProjectFilename.
Definition: DynExpCore.cpp:279
auto & GetInstrumentLib() const noexcept
Getter for the instrument library.
Definition: DynExpCore.h:269
Util::SynchronizedPointer< ProjectParams > ParamsTypeSyncPtrType
Alias for the return type of DynExpCore::GetParams(). Parameters wrapped into Util::SynchronizedPoint...
Definition: DynExpCore.h:133
void Shutdown()
Terminates DynExpCore::WorkerThread and waits until the thread has ended.
Definition: DynExpCore.cpp:50
Resource manager for HardwareAdapterBase resources deriving from a specialized ResourceManagerBase cl...
Definition: Managers.h:631
Resource manager for InstrumentBase resources deriving from a specialized ResourceManagerBase class.
Definition: Managers.h:657
Resource manager for ModuleBase resources deriving from a specialized ResourceManagerBase class.
Definition: Managers.h:700
Abstract base class for object parameter classes. Each class derived from class Object must be accomp...
Definition: Object.h:326
const DynExpCore & Core
Reference to DynExp's core.
Definition: Object.h:1780
Defines a parameter class with parameters common to all DynExp projects.
Definition: DynExpCore.h:25
std::filesystem::path ProjectFilename
Path to project file. Only non-empty if project has been saved or if it was loaded from file.
Definition: DynExpCore.h:105
Param< ParamsConfigDialog::TextType > Authors
Author of project file.
Definition: DynExpCore.h:61
ListParam< int > VSplitterWidgetHeights
Widths of areas split vertically by the main splitter widget in the main window.
Definition: DynExpCore.h:92
StoreWindowStatesType
Indicates whether to apply window states (like position and size) from the project files.
Definition: DynExpCore.h:33
Param< StoreWindowStatesType > StoreWindowStates
Indicates whether to apply window states (like position and size) from the project files.
Definition: DynExpCore.h:68
WindowStyleParamsExtension CircuitWindowStyleParams
Window states of the circuit diagram window (CircuitDiagram). WindowStyleParamsExtension::WindowDocki...
Definition: DynExpCore.h:82
static Util::TextValueListType< StoreWindowStatesType > AvlblStoreWindowStatesTypeStrList()
Assigns labels to the entries of StoreWindowStatesType.
Definition: DynExpCore.cpp:8
std::filesystem::path LastDataSaveDirectory
Path to directory where modules saved their data most recently.
Definition: DynExpCore.h:110
const char * GetParamClassTag() const noexcept override final
This function is intended to be overridden once in each derived class returning the name of the respe...
Definition: DynExpCore.h:59
Param< ParamsConfigDialog::TextType > Version
Version of project file.
Definition: DynExpCore.h:62
ListParam< int > HSplitterWidgetWidths
Widths of areas split horizontally by the main splitter widget in the main window.
Definition: DynExpCore.h:87
void ConfigureParamsImpl(dispatch_tag< ParamsBase >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition: DynExpCore.cpp:18
WindowStyleParamsExtension MainWindowStyleParams
Window states of the main window. WindowStyleParamsExtension::WindowDockingState member is ignored.
Definition: DynExpCore.h:75
Param< ParamsConfigDialog::TextType > Comment
Comment to project file.
Definition: DynExpCore.h:63
ProjectParams(const DynExpCore &Core)
Constructs the parameters for a ProjectParams instance. The ID argument passed to ParamsBase::ParamsB...
Definition: DynExpCore.h:54
void Startup(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
Definition: Managers.h:250
Bundles several parameters to describe a UI window's style. Use in parameter classes.
Definition: Module.h:1234
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:309
Interface to delete copy constructor and copy assignment operator and thus make derived classes non-c...
Definition: Util.h:23
Implements a QObject belonging to a hardware adapter (derived from DynExp::HardwareAdapterBase) that ...
Definition: QtUtil.h:327
Pointer to lock a class derived from ISynchronizedPointerLockable for synchronizing between threads....
Definition: Util.h:170
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::vector< LibraryEntry< InstrumentPtrType > > InstrumentLibraryVectorType
Alias for the vector type containing entries of a instrument library.
Definition: Libraries.h:225
std::vector< LibraryEntry< HardwareAdapterPtrType > > HardwareAdapterLibraryVectorType
Alias for the vector type containing entries of a hardware adapter library.
Definition: Libraries.h:204
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.
std::vector< LibraryEntry< ModulePtrType > > ModuleLibraryVectorType
Alias for the vector type containing entries of a module library.
Definition: Libraries.h:246
EventLogger & EventLog()
This function holds a static EventLogger instance and returns a reference to it. DynExp uses only one...
Definition: Util.cpp:509
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Definition: QtUtil.h:37
Accumulates include statements to provide a precompiled header.
Represents an entry in the library.
Definition: Libraries.h:75
LibraryObjectFactoryPtrType< ObjectTypeBasePtr > ObjectFactoryPtr
Refer to LibraryEntry::LibraryEntry()
Definition: Libraries.h:92