22 template <
typename Po
interType>
32 template <
typename ResourcePo
interT>
85 template <
typename Po
interType>
119 template <
typename T>
122 using MapType = std::unordered_map<ItemIDType, ResourceType>;
145 const bool Empty() const noexcept {
return Map.empty(); }
207 template <
typename T = Po
interType, ShareResourceEnablerType<T> = 0>
213 template <
typename T = Po
interType, ShareResourceEnablerType<T> = 0>
226 auto cend() const noexcept {
return Map.cend(); }
231 template <
typename ElementType>
241 template <
typename DerivedType>
319 template <
typename LibraryVectorT>
345 template <
typename ElementType>
393 template <
typename Po
interType>
396 return LookUpResource(ID)->second.ResourcePointer.get();
399 template <
typename Po
interType>
402 return LookUpResource(ID)->second.ResourcePointer.get();
405 template <
typename Po
interType>
408 auto Res = Map.extract(ID);
412 "Resource with ID " +
Util::ToStr(ID) +
" cannot be extracted from a resource manager since it cannot be found.");
414 return std::move(Res.mapped().ResourcePointer);
417 template <
typename Po
interType>
420 GetResource(ID)->BlockIfUnused(
Timeout);
427 auto Resource = ExtractResource(ID);
430 template <
typename Po
interType>
433 LookUpResource(ID)->second.TreeWidgetItem.reset();
436 template <
typename Po
interType>
441 auto const TreeWidgetItem = LookUpResource(ID)->second.TreeWidgetItem.get();
445 TreeWidgetItem->setSelected(
true);
452 template <
typename Po
interType>
453 template <typename T, ResourceManagerBase<PointerType>::ShareResourceEnablerType<T>>
456 return std::static_pointer_cast<const typename PointerType::element_type>(LookUpResource(ID)->second.ResourcePointer);
459 template <
typename Po
interType>
460 template <typename T, ResourceManagerBase<PointerType>::ShareResourceEnablerType<T>>
463 return LookUpResource(ID)->second.ResourcePointer;
466 template <
typename Po
interType>
467 template <
typename ElementType>
470 return InsertResource(std::forward<ElementType>(Element), GetNextID());
473 template <
typename Po
interType>
474 template <
typename DerivedType>
479 for (
const auto& ResourceEntry : Map)
480 if (
dynamic_cast<DerivedType*
>(ResourceEntry.second.ResourcePointer.get()))
481 FoundIDs.push_back(ResourceEntry.first);
486 template <
typename Po
interType>
489 std::for_each(cbegin(), cend(), [](
const auto& i) {
490 i.second.ResourcePointer->ClearWarning();
494 template <
typename Po
interType>
499 std::for_each(cbegin(), cend(), [&FoundIDs, OnlyResourcesBeingInUse](
const auto& i) {
500 if (i.second.ResourcePointer->GetException() && (!OnlyResourcesBeingInUse || !i.second.ResourcePointer->IsUnused()))
501 FoundIDs.push_back(i.first);
507 template <
typename Po
interType>
510 std::for_each(cbegin(), cend(), [](
const auto& i) {
511 if (i.second.ResourcePointer->GetException())
512 i.second.ResourcePointer->Reset();
516 template <
typename Po
interType>
525 template <
typename Po
interType>
528 auto XMLNode = MakeXMLConfigHeadNode(Document);
530 std::for_each(cbegin(), cend(), [&XMLNode, &Document](
const auto& i) {
531 QDomElement ItemNode = Document.createElement(
"Item");
532 ItemNode.setAttribute(QString(
"Name"), QString::fromStdString(i.second.ResourcePointer->GetName()));
533 ItemNode.setAttribute(QString(
"Category"), QString::fromStdString(i.second.ResourcePointer->GetCategory()));
534 ItemNode.setAttribute(QString(
"ID"),
static_cast<qulonglong
>(i.first));
537 QDomElement ParamsNode = Document.createElement(
"Params");
538 ParamsNode.appendChild(i.second.ResourcePointer->GetParams()->ConfigToXML(Document));
539 ItemNode.appendChild(ParamsNode);
541 XMLNode.appendChild(ItemNode);
547 template <
typename Po
interType>
550 std::for_each(cbegin(), cend(), [](
const auto& i) { i.second.TreeWidgetItem.reset(); });
553 template <
typename Po
interType>
554 template <
typename LibraryVectorT>
557 auto ItemNodeList = XMLNode.elementsByTagName(
"Item");
558 for (
int i = 0; i < ItemNodeList.length(); ++i)
560 auto ItemNode = ItemNodeList.item(i).toElement();
561 if (ItemNode.isNull())
563 "Error parsing the specified project file. An item node contains invalid data.");
567 auto ItemID = Util::GetTFromDOMAttribute<ItemIDType>(ItemNode,
"ID");
571 auto Params = LibEntry.ConfigFactoryPtr()->MakeConfigFromXML(ItemID, Core, ParamsNode);
572 auto Item = LibEntry.ObjectFactoryPtr(GetOwnerThreadID(Core), std::move(Params));
574 InsertResource(std::move(Item), ItemID);
578 template <
typename Po
interType>
581 auto Iter = Map.find(ID);
583 if (Iter == Map.cend())
589 template <
typename Po
interType>
590 template <
typename ElementType>
595 "Resource cannot be inserted into a resource manager since 0 is not a valid ID.");
597 auto Result = Map.try_emplace(ID,
ResourceType(std::forward<ElementType>(Element)));
600 "Resource cannot be inserted into a resource manager since the chosen ID already exists.");
607 template <
typename Po
interType>
610 if (CurrentID < ConsumedID)
611 CurrentID = ConsumedID;
613 if (CurrentID == ConsumedID)
617 template <
typename Po
interType>
620 if (CurrentID == std::numeric_limits<ItemIDType>::max())
Implementation of DynExp hardware adapter objects.
Implementation of DynExp instrument objects.
Implementation of DynExp module objects.
Common base class for all derived ResourceManagerBase classes. Logical const-ness: Only const functio...
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...
~CommonResourceManagerBase()=default
CommonResourceManagerBase()=default
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...
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Resource manager for HardwareAdapterBase resources deriving from a specialized ResourceManagerBase cl...
~HardwareAdapterManager()=default
virtual QDomElement MakeXMLConfigHeadNode(QDomDocument &Document) const override
Creates and returns the XML root node of the (single) resource manager instance of the derived manage...
virtual void StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const override
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
bool AllConnected() const
Determines whether all hardware adapters managed by this resource manager are connected....
HardwareAdapterManager()=default
Resource manager for InstrumentBase resources deriving from a specialized ResourceManagerBase class.
virtual QDomElement MakeXMLConfigHeadNode(QDomDocument &Document) const override
Creates and returns the XML root node of the (single) resource manager instance of the derived manage...
virtual void PrepareResetChild() override
Prepares all resources stored in this resource manager for their deletion by e.g. asking them to term...
bool AllInitialized() const
Determines whether all instruments managed by this resource manager are initialized....
virtual void ShutdownChild() const override
Calls ShutdownChild() to terminate and clean up all resources stored in this resource manager.
virtual void StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const override
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
~InstrumentManager()=default
void TerminateAll() const
Calls RunnableObject::Terminate() on all instruments managed by this resource manager....
InstrumentManager()=default
size_t GetNumRunningInstruments() const
Counts the instruments managed by this resource manager which are running (as determined by RunnableO...
Abstract base class of link parameters (to be saved in project files) describing relations between mu...
Resource manager for ModuleBase resources deriving from a specialized ResourceManagerBase class.
void RestoreWindowStatesFromParams() const
Calls ModuleBase::RestoreWindowStatesFromParams() on all modules managed by this resource manager....
virtual void StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const override
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
virtual QDomElement MakeXMLConfigHeadNode(QDomDocument &Document) const override
Creates and returns the XML root node of the (single) resource manager instance of the derived manage...
void TerminateAll() const
Calls RunnableObject::Terminate() on all modules managed by this resource manager....
void UpdateParamsFromWindowStates() const
Calls ModuleBase::UpdateParamsFromWindowStates() on all modules managed by this resource manager....
virtual void PrepareResetChild() override
Prepares all resources stored in this resource manager for their deletion by e.g. asking them to term...
virtual void ShutdownChild() const override
Calls ShutdownChild() to terminate and clean up all resources stored in this resource manager.
size_t GetNumRunningModules() const
Counts the modules managed by this resource manager which are running (as determined by RunnableObjec...
Base class for all DynExp Objects like hardware adapters (DynExp::HardwareAdapterBase),...
Allow exclusive access to some of ResourceManagerBase's private methods to LinkBase.
auto ShareResourceAsNonConst(ItemIDType ID) const
Copies and returns the shared pointer pointing to (and owning) the resource identified by ID....
constexpr LinkBaseOnlyType(ResourceManagerBase &Parent) noexcept
Construcs an instance - one for each ResourceManagerBase instance.
ResourceManagerBase & Parent
Owning ResourceManagerBase instance.
Typed resource manager base class deriving from class CommonResourceManagerBase.
const auto GetNumResources() const noexcept
Determines the amount of resources stored in this resource manager.
void Reset()
Resets the resource manager by calling ResetChild(), by removing all resources from Map and by resett...
virtual void ResetChild()
Resets the resource manager by calling ResetChild(), by removing all resources from Map and by resett...
void ClearResourcesWarnings() const
Calls Object::ClearWarning() on all resources stored in this resource manager.
void DeleteAllTreeWidgetItems()
Deletes the Resource::TreeWidgetItem of all resources stored in this resource manager.
void ResetFailedResources() const
Calls Object::Reset() on all resources stored in this resource manager which are in an error state (d...
PointerType ExtractResource(ItemIDType ID)
Releases ownership of the owned resource with ID and returns it.
auto cend() const noexcept
Returns an iterator behind the last resource stored in the resource manager.
std::unordered_map< ItemIDType, ResourceType > MapType
Type of a map mapping DynExp object IDs (ItemIDType) to DynExp resources (ResourceType)
ItemIDType InsertResource(ElementType &&Element)
Inserts a resource into the resource manager.
PointerType::element_type * GetResource(ItemIDType ID)
Retrieves a resource specified by its ID from the resource manager.
void RemoveResource(ItemIDType ID, const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout)
Removes the resource identified by ID from the resource manager and deletes it.
void FocusTreeWidgetItem(ItemIDType ID)
Selects the Resource::TreeWidgetItem of the requested resource.
LinkBaseOnlyType LinkBaseOnly
const PointerType::element_type * GetResource(ItemIDType ID) const
Retrieves a resource specified by its ID from the resource manager.
auto LookUpResource(ItemIDType ID) const
Retrieves a resource specified by its ID from the resource manager.
void MakeEntriesFromXML(const QDomElement &XMLNode, const LibraryVectorT &Library, const DynExpCore &Core)
Creates resources and takes ownership of them according to an XML tree from a DynExp project XML file...
ItemIDType InsertResource(ElementType &&Element, const ItemIDType ID)
Inserts a resource into the resource manager.
ItemIDType CurrentID
ID, the next resource added to the manager will receive. After initialization and reset (Reset()),...
auto ShareResource(ItemIDType ID)
Copies and returns the shared pointer pointing to (and owning) the resource identified by ID....
virtual void ShutdownChild() const
Calls ShutdownChild() to terminate and clean up all resources stored in this resource manager.
void Shutdown() const
Calls ShutdownChild() to terminate and clean up all resources stored in this resource manager.
virtual void StartupChild(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const =0
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
auto ShareResource(ItemIDType ID) const
Copies and returns the shared pointer pointing to (and owning) the resource identified by ID....
auto cbegin() const noexcept
Returns an iterator to the first resource stored in the resource manager.
~ResourceManagerBase()=default
ItemIDType GetNextID() const noexcept
Getter for the ID to assign to the next resource to be inserted into the resource manager.
const bool Empty() const noexcept
Determines whether this resource manager is empty.
void PrepareReset()
Prepares all resources stored in this resource manager for their deletion by e.g. asking them to term...
virtual QDomElement MakeXMLConfigHeadNode(QDomDocument &Document) const =0
Creates and returns the XML root node of the (single) resource manager instance of the derived manage...
virtual void PrepareResetChild()
Prepares all resources stored in this resource manager for their deletion by e.g. asking them to term...
ItemIDListType GetFailedResourceIDs(bool OnlyResourcesBeingInUse=false) const
Builds and returns a list of the IDs of all resources stored in this resource manager which are in an...
ItemIDListType Filter() const
Builds and returns a list of the IDs of all resources stored in this resource manager which can be ca...
std::enable_if_t< std::is_same_v< std::shared_ptr< typename T::element_type >, PointerType >, int > ShareResourceEnablerType
SFINAE alias to define methods assuming shared resource ownership only if PointerType is a std::share...
void DeleteTreeWidgetItem(ItemIDType ID)
Deletes the Resource::TreeWidgetItem of the requested resource.
void Startup(FunctionToCallWhenObjectStartedType FunctionToCallWhenObjectStarted) const
Starts all resources owned by the resource manager. For HardwareAdapterBase instances,...
void IncrementID()
Increments CurrentID.
void RaiseID(const ItemIDType ConsumedID)
If CurrentID is less or equal to ConsumedID, sets CurrentID to ConsumedID + 1. If CurrentID is greate...
QDomElement EntryConfigsToXML(QDomDocument &Document) const
Creates and returns the XML tree containing the configuration of each resource owned by this resource...
MapType Map
Map storing all resources owned by this resource manager.
static constexpr std::chrono::milliseconds DefaultTimeout
Duration which is used as a default timeout within all methods of this class if no different duration...
An invalid argument like a null pointer has been passed to a function.
Data to operate on is invalid for a specific purpose. This indicates a corrupted data structure or fu...
An operation cannot be performed currently since the related object is in an invalid state like an er...
Thrown when a requested ressource does not exist.
Thrown when a numeric operation would result in an overflow (e.g. due to incompatible data types)
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::vector< ItemIDType > ItemIDListType
Type of a list of IDs belonging to objects managed by DynExp.
size_t ItemIDType
ID type of objects/items managed by DynExp.
const LibraryVectorT::value_type & FindInLibraryVector(const LibraryVectorT &LibraryVector, const std::string &Category, const std::string &Name)
Finds an entry in a library vector by category and name.
std::string ToStr(const T &Value, int Precision=-1)
Converts a (numeric) value of type T to a std::string using operator<< of std::stringstream.
QDomElement GetSingleChildDOMElement(const QDomElement &Parent, const QString &ChildTagName)
Behaves like GetSingleChildDOMNode() but returns the node converted to a DOM element.
std::string GetStringFromDOMAttribute(const QDomElement &Element, const QString &AttributeName)
Behaves like GetDOMAttribute() but returns the text from the attribute.
Accumulates include statements to provide a precompiled header.
Defines a DynExp resource, which mainly owns a DynExp::Object instance wrapping a pointer to it.
Resource(ResourcePointerT &&ResourcePointer)
Constructs a Resource instance.
std::unique_ptr< QTreeWidgetItem > TreeWidgetItem
For visualization of the resource and its state in DynExp's main window (DynExpManager)....
PointerType ResourcePointer
Pointer to the DynExp::Object instance owned by this resource.