DynExp
Highly flexible laboratory automation for dynamically changing experiments.
DynExp::HardwareAdapterBase Class Referenceabstract

Defines the base class for a hardware adapter object. Hardware adapters describe interfaces/connections to physical hardware. More...

+ Inheritance diagram for DynExp::HardwareAdapterBase:

Public Types

using ParamsType = HardwareAdapterParamsBase
 Type of the parameter class belonging to this Object type. Declare this alias in every derived class with the respective parameter class accompanying the derived Object. More...
 
using ConfigType = HardwareAdapterConfiguratorBase
 Type of the configurator class belonging to this Object type. Declare this alias in every derived class with the respective configurator class accompanying the derived Object. More...
 
- Public Types inherited from DynExp::Object
using ParamsType = ParamsBase
 Type of the parameter class belonging to this Object type. Declare this alias in every derived class with the respective parameter class accompanying the derived Object. More...
 
using ParamsTypeSyncPtrType = Util::SynchronizedPointer< ParamsType >
 Alias for the return type of Object::GetParams(). Parameters wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way. More...
 
using ParamsConstTypeSyncPtrType = Util::SynchronizedPointer< const ParamsType >
 Alias for the return type of Object::GetParams() const. Parameters wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way. More...
 
using ConfigType = ConfiguratorBase
 Type of the configurator class belonging to this Object type. Declare this alias in every derived class with the respective configurator class accompanying the derived Object. More...
 
using ParamsGetterType = Util::CallableMemberWrapper< Object, decltype(&Object::GetParams)>
 Invoking an instance of this alias is supposed to call Object::GetParams() of the instance the Util::CallableMemberWrapper has been constructed with. More...
 

Public Member Functions

 HardwareAdapterBase (const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
 Constructs a hardware adapter instance. More...
 
virtual ~HardwareAdapterBase ()=0
 
virtual std::string GetCategory () const override
 Returns the category of this Object type. More...
 
void ResetException () const
 Sets LastException to nullptr in a thread-safe way. More...
 
bool IsConnected () const noexcept
 Determines the connection status of the hardware interface. More...
 
- Public Member Functions inherited from Util::INonCopyable
 INonCopyable (const INonCopyable &)=delete
 
INonCopyableoperator= (const INonCopyable &)=delete
 
- Public Member Functions inherited from DynExp::Object
ItemIDType GetID () const noexcept
 Returns the ID of this Object instance. Thread-safe since ID is const. More...
 
std::string GetCategoryAndName () const
 Builds a string from an Object's category and name to allow the user to identify an Object's type. More...
 
void EnsureReadyState (bool IsAutomaticStartup)
 Ensures that this Object instance is ready by possibly starting its worker thread or by opening connections to hardware devices. More...
 
void CheckLinkedObjectStates () const
 Checks whether Object instances this instance uses are in a ready state. Override CheckLinkedObjectStatesChild() to implement this behavior. More...
 
void Reset ()
 Resets this Object instance (including all its derived classes) by calling ResetImpl(). A reset implies that the derived classes reload all their parameters from Params and that all LinkedObjectWrapper instances owned by this Object instance become invalidated. More...
 
void BlockIfUnused (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout)
 Blocks this Object instance setting Object::IsBlocked to true. Refer to Object::IsBlocked for the consequences. More...
 
ParamsConstTypeSyncPtrType GetParams (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
 Locks the mutex of the parameter class instance Params assigned to this Object instance and returns a pointer to the locked Params. More...
 
ParamsTypeSyncPtrType GetParams (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault)
 Locks the mutex of the parameter class instance Params assigned to this Object instance and returns a pointer to the locked Params. More...
 
auto GetObjectName (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
 Returns the name of this Object instance. More...
 
bool IsSharedUsageEnabled (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
 Returns whether shared usage has been enabled for this Object instance. Refer to ParamsBase::UsageType. More...
 
void SetWarning (std::string Description, int ErrorCode) const
 Setter for Object::Warning. Sets the warning by a description and an error code. More...
 
void SetWarning (const Util::Exception &e) const
 Setter for Object::Warning. Sets the warning by retrieving the warning data from an exception e. More...
 
void ClearWarning () const
 Resets Object::Warning. More...
 
auto GetWarning () const
 Returns Object::Warning in a thread-safe way by copying its internal data. More...
 
std::exception_ptr GetException (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
 Returns a pointer to the exception which has caused this Object instance to fail. More...
 
bool IsReady () const
 Returns wheter this Object instance is ready (e.g. it is running or connected to a hardware device) and not blocked (refer to Object::IsBlocked). More...
 
auto GetUseCount (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
 Counts the registered useres in a thread-safe way. More...
 
bool IsUnused (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
 Returns whether this Object instance is used by other instances. More...
 
auto GetUserIDs (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
 Returns a list of the IDs of the registered users in a thread-safe way. More...
 
auto GetUserNamesString (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
 Builds a string describing which users are registered containing their object names, categories and type names in a thread-safe way. More...
 
virtual std::string GetName () const =0
 Returns the name of this Object type. More...
 

Static Public Member Functions

constexpr static auto Category () noexcept
 Every derived class has to redefine this function. More...
 
- Static Public Member Functions inherited from DynExp::Object
static std::string CategoryAndNameToStr (const std::string &Category, const std::string &Name)
 Builds a string from an Object's category and name to allow the user to identify an Object's type. More...
 

Static Public Attributes

static constexpr auto ShortTimeoutDefault = std::chrono::milliseconds(10)
 Default timeout e.g. used as a default for calls to Object::GetException(). More...
 
static constexpr auto HardwareOperationTimeout = std::chrono::milliseconds(100)
 Default timeout used to lock the mutex provided by the base class Util::ILockable to synchronize access to the hardware interface in between multiple instrument threads. More...
 
- Static Public Attributes inherited from Util::ILockable
static constexpr std::chrono::milliseconds DefaultTimeout = std::chrono::milliseconds(10)
 Duration which is used as a default timeout within all methods of this class if no different duration is passed to them. More...
 
- Static Public Attributes inherited from DynExp::Object
static constexpr std::chrono::milliseconds GetParamsTimeoutDefault = std::chrono::milliseconds(100)
 Default timeout used by Object::GetParams() to lock the mutex of the parameter instance assigned to this Object instance. More...
 

Protected Member Functions

void ThrowException (std::exception_ptr Exception) const
 Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped excetion by calling Util::ForwardException(). More...
 
Not thread-safe

AcquireLock() has to be called manually before and returned LockType has to be still in scope.

void ThrowExceptionUnsafe (std::exception_ptr Exception) const
 Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped excetion by calling Util::ForwardException(). More...
 
void SetExceptionUnsafe (std::exception_ptr Exception) const
 Stores Exception in LastException. More...
 
auto GetExceptionUnsafe () const
 Getter for LastException. More...
 
- Protected Member Functions inherited from Util::ILockable
 ILockable ()=default
 
 ~ILockable ()=default
 
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. More...
 
- Protected Member Functions inherited from Util::INonCopyable
constexpr INonCopyable ()=default
 
 ~INonCopyable ()=default
 
- Protected Member Functions inherited from DynExp::Object
 Object (const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
 Constructs an Object instance. More...
 
virtual ~Object ()=0
 
void EnsureCallFromOwningThread () const
 Asserts that the call to this function is performed from the thread which constructed this Object instance (the thread with the id stored in Object::OwnerThreadID). More...
 
ParamsTypeSyncPtrType GetNonConstParams (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
 Allows derived Objects to edit their own parameters - even in const task functions (for instruments) or event functions (for modules). More...
 
auto LockUserList (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout)
 Locks the user list for thread-safe manipulation. More...
 
void DeregisterAllUnsafe ()
 Deregisters all users and notifies them that they need to check the states of their used linked objects. More...
 
auto GetUseCountUnsafe ()
 
auto GetUserNamesStringUnsafe () const
 
bool IsUnusedUnsafe ()
 Returns whether this Object instance is used by other instances (not thread-safe). More...
 

Private Member Functions

void ResetImpl (dispatch_tag< Object >) override final
 Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy. More...
 
virtual void ResetImpl (dispatch_tag< HardwareAdapterBase >)=0
 Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy. More...
 
void EnsureReadyStateChild ([[maybe_unused]] bool IsAutomaticStartup) override final
 Ensures that this Object instance is ready by possibly starting its worker thread or by opening connections to hardware devices. More...
 
virtual void EnsureReadyStateChild ()=0
 Ensures that this Object instance is ready by possibly starting its worker thread or by opening connections to hardware devices. More...
 
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. More...
 
Override

Override by derived classes.

virtual bool IsConnectedChild () const noexcept=0
 Determines the connection status of the hardware interface. More...
 

Private Attributes

std::exception_ptr LastException
 Stores the most recent exception caused by a hardware operation. More...
 

Additional Inherited Members

- Public Attributes inherited from DynExp::Object
LinkedObjectWrapperOnlyType LinkedObjectWrapperOnly
 Allow exclusive access to some of Object's private methods to any LinkedObjectWrapper<T>. More...
 
- Protected Types inherited from Util::ILockable
using MutexType = std::timed_mutex
 
using LockType = std::unique_lock< MutexType >
 

Detailed Description

Defines the base class for a hardware adapter object. Hardware adapters describe interfaces/connections to physical hardware.

Definition at line 88 of file HardwareAdapter.h.

Member Typedef Documentation

◆ ConfigType

Type of the configurator class belonging to this Object type. Declare this alias in every derived class with the respective configurator class accompanying the derived Object.

Definition at line 92 of file HardwareAdapter.h.

◆ ParamsType

Type of the parameter class belonging to this Object type. Declare this alias in every derived class with the respective parameter class accompanying the derived Object.

Definition at line 91 of file HardwareAdapter.h.

Constructor & Destructor Documentation

◆ HardwareAdapterBase()

DynExp::HardwareAdapterBase::HardwareAdapterBase ( const std::thread::id  OwnerThreadID,
ParamsBasePtrType &&  Params 
)
inline

Constructs a hardware adapter instance.

Parameters
OwnerThreadIDThread id of the thread owning the Object instance to be constructed.
ParamsParameter class instance to be assigned to the Object instance to be constructed.
Exceptions
Util::InvalidArgExceptionis thrown if OwnerThreadID is an invalid thread id or if Params is nullptr.

Definition at line 115 of file HardwareAdapter.h.

◆ ~HardwareAdapterBase()

DynExp::HardwareAdapterBase::~HardwareAdapterBase ( )
pure virtual

Definition at line 17 of file HardwareAdapter.cpp.

Member Function Documentation

◆ Category()

constexpr static auto DynExp::HardwareAdapterBase::Category ( )
inlinestaticconstexprnoexcept

Every derived class has to redefine this function.

Returns
Returns the category of this hardware adapter type.

Definition at line 109 of file HardwareAdapter.h.

◆ EnsureReadyStateChild() [1/2]

virtual void DynExp::HardwareAdapterBase::EnsureReadyStateChild ( )
privatepure virtual

◆ EnsureReadyStateChild() [2/2]

void DynExp::HardwareAdapterBase::EnsureReadyStateChild ( [[maybe_unused] ] bool  IsAutomaticStartup)
finaloverrideprivate

Ensures that this Object instance is ready by possibly starting its worker thread or by opening connections to hardware devices.

Parameters
IsAutomaticStartupPass true if ensuring other Object instances to be ready have caused a call to this function. Pass false if an action performed by the user has caused the call to this function.
Exceptions
Util::InvalidStateExceptionis thrown if GetException() does not return nullptr.

◆ GetCategory()

◆ GetExceptionChild()

std::exception_ptr DynExp::HardwareAdapterBase::GetExceptionChild ( const std::chrono::milliseconds  Timeout) const
finaloverrideprivatevirtual

Returns a pointer to the exception which has caused this Object instance to fail.

Parameters
TimeoutTime to wait for locking the mutex used to protect the stored exception data.
Returns
Pointer to the stored exception or a default-constructed std::exception_ptr if no exception occurred.

Implements DynExp::Object.

Definition at line 107 of file HardwareAdapter.cpp.

◆ GetExceptionUnsafe()

auto DynExp::HardwareAdapterBase::GetExceptionUnsafe ( ) const
inlineprotected

Getter for LastException.

Returns
Returns LastException.

Definition at line 161 of file HardwareAdapter.h.

◆ IsConnected()

bool DynExp::HardwareAdapterBase::IsConnected ( ) const
inlinenoexcept

Determines the connection status of the hardware interface.

Returns
Returns true when the hardware adapter is connected to the physical hardware device, false otherwise.

Definition at line 132 of file HardwareAdapter.h.

◆ IsConnectedChild()

virtual bool DynExp::HardwareAdapterBase::IsConnectedChild ( ) const
privatepure virtualnoexcept

◆ ResetException()

void DynExp::HardwareAdapterBase::ResetException ( ) const

Sets LastException to nullptr in a thread-safe way.

Definition at line 21 of file HardwareAdapter.cpp.

◆ ResetImpl() [1/2]

virtual void DynExp::HardwareAdapterBase::ResetImpl ( dispatch_tag< HardwareAdapterBase )
privatepure virtual

Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy.

Implemented in DynExp::SerialCommunicationHardwareAdapter.

◆ ResetImpl() [2/2]

void DynExp::HardwareAdapterBase::ResetImpl ( dispatch_tag< Object )
finaloverrideprivatevirtual

Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every derived class gets called - starting from DynExp::Object, descending the inheritance hierarchy.

Implements DynExp::Object.

Definition at line 87 of file HardwareAdapter.cpp.

◆ SetExceptionUnsafe()

void DynExp::HardwareAdapterBase::SetExceptionUnsafe ( std::exception_ptr  Exception) const
protected

Stores Exception in LastException.

Parameters
ExceptionException to store

Definition at line 67 of file HardwareAdapter.cpp.

◆ ThrowException()

void DynExp::HardwareAdapterBase::ThrowException ( std::exception_ptr  Exception) const
protected

Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped excetion by calling Util::ForwardException().

Parameters
ExceptionException to store and rethrow

Definition at line 29 of file HardwareAdapter.cpp.

◆ ThrowExceptionUnsafe()

void DynExp::HardwareAdapterBase::ThrowExceptionUnsafe ( std::exception_ptr  Exception) const
protected

Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped excetion by calling Util::ForwardException().

Parameters
ExceptionException to store and rethrow

Definition at line 40 of file HardwareAdapter.cpp.

Member Data Documentation

◆ HardwareOperationTimeout

constexpr auto DynExp::HardwareAdapterBase::HardwareOperationTimeout = std::chrono::milliseconds(100)
staticconstexpr

Default timeout used to lock the mutex provided by the base class Util::ILockable to synchronize access to the hardware interface in between multiple instrument threads.

Definition at line 103 of file HardwareAdapter.h.

◆ LastException

std::exception_ptr DynExp::HardwareAdapterBase::LastException
mutableprivate

Stores the most recent exception caused by a hardware operation.

Definition at line 184 of file HardwareAdapter.h.

◆ ShortTimeoutDefault

constexpr auto DynExp::HardwareAdapterBase::ShortTimeoutDefault = std::chrono::milliseconds(10)
staticconstexpr

Default timeout e.g. used as a default for calls to Object::GetException().

Definition at line 97 of file HardwareAdapter.h.


The documentation for this class was generated from the following files: