DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
DynExp::InstrumentBase Class Referenceabstract

Base class for instruments. Instruments comprise virtual devices (meta instruments) and physial devices (instruments). While meta instruments are used by modules (ModuleBase) as an abstraction layer, physical instruments derive from meta instruments and make usually use of one hardware adapter (HardwareAdapterBase) to communicate with the underlying hardware. More...

#include <Instrument.h>

+ Inheritance diagram for DynExp::InstrumentBase:

Classes

class  InstrumenThreadOnlyType
 Allow exclusive access to some of InstrumentBase's private methods to the instrument thread InstrumentThreadMain(). More...
 

Public Types

enum class  TaskHandlingContinuationType { Continue , Terminate , Defer }
 Indicates how an instrument should proceed after handling a task. More...
 
using ParamsType = InstrumentParamsBase
 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. "
 
using ConfigType = InstrumentConfiguratorBase
 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.
 
using InstrumentDataType = InstrumentDataBase
 Type of the data class belonging to this InstrumentBase type. Declare this alias in every derived class with the respective data class accompanying the derived InstrumentBase.
 
using InstrumentDataTypeSyncPtrType = Util::SynchronizedPointer< InstrumentDataType >
 Alias for the return type of InstrumentBase::GetInstrumentData(). Data class instances wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way.
 
using InstrumentDataTypeSyncPtrConstType = Util::SynchronizedPointer< const InstrumentDataType >
 Alias for the return type of InstrumentBase::GetInstrumentData() const. Data class instances wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way.
 
gRPC aliases

Redefine in derived meta instrument classes to use them in DynExpInstr::gRPCInstrument.

using InitTaskType = InitTaskBase
 Defines a task for initializing an instrument within an instrument inheritance hierarchy. Each instrument (indirectly) derived from class InstrumentBase must be accompanied by an initialization task class derived from InitTaskBase. Even if the task does not do anything, at least it has to call InitTaskBase::InitFuncImpl() of the derived instrument's initialization task class.
 
using ExitTaskType = ExitTaskBase
 Defines a task for deinitializing an instrument within an instrument inheritance hierarchy. Each instrument (indirectly) derived from class InstrumentBase must be accompanied by a deinitialization task class derived from ExitTaskBase. Even if the task does not do anything, at least it has to call ExitTaskBase::ExitFuncImpl() of the derived instrument's deinitialization task class.
 
using UpdateTaskType = UpdateTaskBase
 Defines a task for updating an instrument within an instrument inheritance hierarchy. Each instrument (indirectly) derived from class InstrumentBase must be accompanied by an update task class derived from UpdateTaskBase. Even if the task does not do anything, at least it has to call UpdateTaskBase::UpdateFuncImpl() of the derived instrument's update task class.
 
- Public Types inherited from DynExp::RunnableObject
using ParamsType = RunnableObjectParams
 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. "
 
using ConfigType = RunnableObjectConfigurator
 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.
 
- 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.
 
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.
 
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.
 
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.
 
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.
 

Public Member Functions

 InstrumentBase (const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
 Constructs an instrument instance.
 
virtual ~InstrumentBase ()=0
 
virtual std::string GetCategory () const override
 Returns the category of this Object type.
 
template<typename DerivedInstrT , typename... TaskFuncArgTs, typename... ArgTs>
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 synchronously. This means that AsSyncTask() blocks until the task has been executed or aborted. This is achieved by passing a callback function to the task which in turn sets a flag after the task execution. The thread calling AsSyncTask() blocks (via std::this_thread::yield()) until this flag is set.
 
- Public Member Functions inherited from DynExp::RunnableObject
 RunnableObject (const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
 Constructs an Object instance.
 
virtual ~RunnableObject ()=0
 
void SetPaused (bool Pause, std::string Description="")
 Pauses or resumes a RunnableObject instance. Its thread stays running, but the instance does not perform any action while it is paused.
 
bool IsRunning () const noexcept
 Returns Running.
 
bool IsPaused () const noexcept
 Returns Paused.
 
bool IsExiting () const noexcept
 Returns ShouldExit.
 
auto GetStartupType () const noexcept
 Returns Startup.
 
auto GetReasonWhyPaused () const
 Returns ReasonWhyPaused.
 
bool Run (QWidget *ParentWidget=nullptr)
 Starts the RunnableObject instance's thread and ensures that all Object instances linked to this instance via ObjectLink parameters are in a ready state. Calls RunChild() which is to be overridden by derived classes to start a thread and to pass this thread back by calling StoreThread().
 
bool RunIfRunAutomatic ()
 Calls Run() if RunnableObjectParams::Startup is set to RunnableObjectParams::Automatic.
 
bool RunIfRunOnCreation ()
 Calls Run() if RunnableObjectParams::Startup is set to RunnableObjectParams::OnCreation.
 
void Terminate (bool Force=false, const std::chrono::milliseconds Timeout=TerminateTimeoutDefault)
 Notifies the RunnableObject instance's thread to terminate and waits until it has ended. Calls TerminateImpl().
 
- 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.
 
std::string GetCategoryAndName () const
 Builds a string from an Object's category and name to allow the user to identify an Object's type.
 
void EnsureReadyState (bool IsAutomaticStartup)
 Ensures that this Object instance is ready by possibly starting its worker thread or by opening connections to hardware devices.
 
void CheckLinkedObjectStates () const
 Checks whether Object instances this instance uses are in a ready state. Override CheckLinkedObjectStatesChild() to implement this behavior.
 
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.
 
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.
 
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.
 
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.
 
auto GetObjectName (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
 Returns the name of this Object instance.
 
bool IsSharedUsageEnabled (const std::chrono::milliseconds Timeout=GetParamsTimeoutDefault) const
 Returns whether shared usage has been enabled for this Object instance. Refer to ParamsBase::UsageType.
 
void SetWarning (std::string Description, int ErrorCode) const
 Setter for Object::Warning. Sets the warning by a description and an error code.
 
void SetWarning (const Util::Exception &e) const
 Setter for Object::Warning. Sets the warning by retrieving the warning data from an exception e.
 
void ClearWarning () const
 Resets Object::Warning.
 
auto GetWarning () const
 Returns Object::Warning in a thread-safe way by copying its internal data.
 
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.
 
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).
 
auto GetUseCount (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
 Counts the registered useres in a thread-safe way.
 
bool IsUnused (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout) const
 Returns whether this Object instance is used by other instances.
 
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.
 
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.
 
virtual std::string GetName () const =0
 Returns the name of this Object type.
 

Static Public Member Functions

static constexpr auto Category () noexcept
 Every derived class has to redefine this function.
 
- 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.
 

Public Attributes

InstrumenThreadOnlyType InstrumentThreadOnly
 Allow exclusive access to some of InstrumentBase's private methods to the instrument thread InstrumentThreadMain().
 
- Public Attributes inherited from DynExp::RunnableObject
RunnableInstanceOnlyType RunnableInstanceOnly
 Allow exclusive access to some of RunnableObject's private methods to class RunnableInstance.
 
- Public Attributes inherited from DynExp::Object
LinkedObjectWrapperOnlyType LinkedObjectWrapperOnly
 Allow exclusive access to some of Object's private methods to any LinkedObjectWrapper<T>.
 

Static Public Attributes

static constexpr auto GetInstrumentDataTimeoutDefault = std::chrono::milliseconds(1000)
 Determines the default timeout for GetInstrumentData() to lock the mutex synchronizing the instrument's data InstrumentData.
 
- Static Public Attributes inherited from DynExp::RunnableObject
static constexpr auto ShortTimeoutDefault = std::chrono::milliseconds(50)
 Default timeout e.g. used as a default for calls to InstrumentBase::GetInstrumentData or ModuleBase::GetModuleData.
 
static constexpr auto TerminateTimeoutDefault = std::chrono::milliseconds(3000)
 Default timeout used as a default for calls to RunnableObject::Terminate.
 
- 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.
 

Protected Member Functions

template<typename TaskT , typename... ArgTs>
void MakeAndEnqueueTask (ArgTs &&...Args) const
 Calls MakeTask() to construct a new task and subsequently enqueues the task into the instrument's task queue. Logical const-ness: this is a const member function to allow pointers to const InstrumentBase inserting tasks into the instrument's task queue. These kind of pointers are e.g. returned by RunnableInstance::GetOwner() which can be called by tasks' TaskBase::RunChild() functions. For const InstrumentBase*, it is possible to insert tasks into the task queue, but not to change the InstrumentBase object itself (e.g. calling Object::Reset()).
 
- Protected Member Functions inherited from DynExp::RunnableObject
void Init ()
 Initializes member variables in case of a reset.
 
std::promise< void > MakeThreadExitedPromise ()
 Helper function to be used by overridden RunChild() functions in derived classes to (re)initialize the ThreadExitedSignal future and to provide the derived class a promise to signal the RunnableObject instance that its thread has terminated.
 
void MakeThread (ThreadFuncType ThreadFunc, std::unique_ptr< RunnableInstance > &&InstancePtr)
 Creates and runs the thread of the RunnableObject instance. Call this function in the derived class overriding RunChild() and nowhere else!
 
bool IsCallFromRunnableThread () const
 Checks whether Thread's id matches the id of the calling thread. This is thread-safe if the function is called by the RunnableObject instance's thread since Terminate() joins the threads before changing the Thread member. It is also thread-safe if the function is called by the thread owning the the RunnableObject instance since Run() and Terminate() can only be called by this thread. Only Run() and Terminate() (indirectly) modify Thread.
 
void EnsureCallFromRunnableThread () const
 Asserts that the call to this function is performed from the RunnableObject instance's thread by calling IsCallFromRunnableThread().
 
void SetReasonWhyPaused (std::string Description)
 Sets the reason why this RunnableObject instance has been paused.
 
void SetReasonWhyPaused (const Util::Exception &e)
 Sets the reason why this RunnableObject instance has been paused.
 
void ClearReasonWhyPaused ()
 Removes the reason why this RunnableObject instance has been paused (since it is resumed).
 
- 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.
 
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).
 
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).
 
auto LockUserList (const std::chrono::milliseconds Timeout=Util::ILockable::DefaultTimeout)
 Locks the user list for thread-safe manipulation.
 
void DeregisterAllUnsafe ()
 Deregisters all users and notifies them that they need to check the states of their used linked objects.
 
auto GetUseCountUnsafe ()
 
auto GetUserNamesStringUnsafe () const
 
bool IsUnusedUnsafe ()
 Returns whether this Object instance is used by other instances (not thread-safe).
 

Static Protected Member Functions

static auto GetExceptionUnsafe (const InstrumentDataTypeSyncPtrConstType &InstrumentDataPtr)
 Getter for InstrumentDataBase::InstrumentException. If InstrumentDataBase::InstrumentException is nullptr and InstrumentDataBase::HasException is still true, returns a pointer to a Util::Exception instance.
 

Private Member Functions

void ResetImpl (dispatch_tag< RunnableObject >) 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.
 
virtual void ResetImpl (dispatch_tag< InstrumentBase >)=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.
 
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.
 
bool IsReadyChild () const override final
 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).
 
Private functions for logical const-ness

Logical const-ness: refer to MakeAndEnqueueTask().

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 task functions.
 
Instrument thread only

These functions must be called by instrument thread only.

TaskHandlingContinuationType HandleTask (InstrumentInstance &Instance)
 Executes and removes the next pending task from the instrument's task queue.
 
void UpdateDataInternal ()
 Inserts an update task (UpdateTaskBase) into the instrument's task queue. Override UpdateAdditionalData() to adjust behavior.
 
void SetException (std::exception_ptr Exception) noexcept
 Sets this instrument instance to an error state and tries to store the exception responsible for the error state in InstrumentData. If InstrumentData cannot be locked, still atomically sets an error flag.
 
void OnError ()
 Derived classes can perform critical shutdown actions after an error has occurred. Override OnErrorChild() to adjust behavior.
 
Main thread only

These functions must not be called by instrument thread.

void RunChild () override final
 Refer to Run().
 
void NotifyChild () override final
 Notify derived classes that some state has changed (e.g. the termination of Thread is requested) and that the child's event/task queue should run now.
 
void TerminateChild (const std::chrono::milliseconds Timeout) override final
 Signals derived classes that terminating the RunnableObject instance's thread is about to be requested. Derived classes might now enqueue respective exit tasks/events into their task/event queues. Refer to TerminateUnsafe().
 
void OnPrepareExit ()
 This function enables derived classes to enqueue tasks to be executed directly before the final exit task (ExitTaskBase). Override OnPrepareExitChild() to adjust behavior.
 

Private Attributes

const std::unique_ptr< InstrumentDataTypeInstrumentData
 Instrument data belonging to this InstrumentBase instance.
 
std::atomic< bool > Initialized = false
 Determines whether the init task (InitTaskBase) has run.
 

Thread-safe public functions

Methods can be called from any thread.

using InstrumentDataGetterType = Util::CallableMemberWrapper< InstrumentBase, InstrumentDataTypeSyncPtrType(InstrumentBase::*)(const std::chrono::milliseconds)>
 Invoking an instance of this alias is supposed to call InstrumentBase::GetInstrumentData() of the instance the Util::CallableMemberWrapper has been constructed with.
 
InstrumentDataTypeSyncPtrType GetInstrumentData (const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault)
 Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase instance and returns a pointer to the locked InstrumentData. Instrument data should not be locked by having called this function while subsequently calling a derived instrument's method which also makes use of the instrument's data by locking it. If this happens (e.g. in a module thread), the instrument data's mutex is locked recursively. In principle, this does no harm since Util::ISynchronizedPointerLockable supports that. But, it is not considered good practice.
 
InstrumentDataTypeSyncPtrConstType GetInstrumentData (const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault) const
 Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase instance and returns a pointer to the locked InstrumentData. Instrument data should not be locked by having called this function while subsequently calling a derived instrument's method which also makes use of the instrument's data by locking it. If this happens (e.g. in a module thread), the instrument data's mutex is locked recursively. In principle, this does no harm since Util::ISynchronizedPointerLockable supports that. But, it is not considered good practice.
 
void UpdateData () const
 Enqueues an update task (instance of class UpdateTaskBase).
 
void EnqueueArriveAtLatchTask (std::latch &Latch) const
 Enqueues a task which arrives at a latch when executed (instance of class ArriveAtLatchTask).
 
bool IsInitialized () const
 Getter for Initialized.
 

Override

Override by derived classes.

virtual std::chrono::milliseconds GetTaskQueueDelay () const
 Specifies in which time intervals the instrument's task queue runs to handle pending tasks.
 
virtual void OnErrorChild () const
 Derived classes can perform critical shutdown actions after an error has occurred. Override OnErrorChild() to adjust behavior.
 
virtual void OnPrepareExitChild () const
 This function enables derived classes to enqueue tasks to be executed directly before the final exit task (ExitTaskBase). Override OnPrepareExitChild() to adjust behavior.
 
virtual bool HandleAdditionalTask ()
 Determines whether task handling should continue.
 
virtual bool UpdateAdditionalData ()
 Determines whether to enqueue update tasks (UpdateTaskBase).
 
virtual std::unique_ptr< InitTaskBaseMakeInitTask () const
 Factory function for an init task (InitTaskBase). Override to define the desired initialization task in derived classes only if the respective task really does something and if it has no pure virtual function.
 
virtual std::unique_ptr< ExitTaskBaseMakeExitTask () const
 Factory function for an exit task (ExitTaskBase). Override to define the desired deinitialization task in derived classes only if the respective task really does something and if it has no pure virtual function.
 
virtual std::unique_ptr< UpdateTaskBaseMakeUpdateTask () const
 Factory function for an update task (UpdateTaskBase). Override to define the desired update task in derived classes only if the respective task really does something and if it has no pure virtual function.
 

Additional Inherited Members

- Protected Types inherited from DynExp::RunnableObject
using ThreadFuncType = std::function< int(std::unique_ptr< RunnableInstance > &&, RunnableObject *)>
 Type of the thread function executed by RunnableObject instances.
 

Detailed Description

Base class for instruments. Instruments comprise virtual devices (meta instruments) and physial devices (instruments). While meta instruments are used by modules (ModuleBase) as an abstraction layer, physical instruments derive from meta instruments and make usually use of one hardware adapter (HardwareAdapterBase) to communicate with the underlying hardware.

Definition at line 479 of file Instrument.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 525 of file Instrument.h.

◆ ExitTaskType

Defines a task for deinitializing an instrument within an instrument inheritance hierarchy. Each instrument (indirectly) derived from class InstrumentBase must be accompanied by a deinitialization task class derived from ExitTaskBase. Even if the task does not do anything, at least it has to call ExitTaskBase::ExitFuncImpl() of the derived instrument's deinitialization task class.

Definition at line 551 of file Instrument.h.

◆ InitTaskType

Defines a task for initializing an instrument within an instrument inheritance hierarchy. Each instrument (indirectly) derived from class InstrumentBase must be accompanied by an initialization task class derived from InitTaskBase. Even if the task does not do anything, at least it has to call InitTaskBase::InitFuncImpl() of the derived instrument's initialization task class.

"

Definition at line 550 of file Instrument.h.

◆ InstrumentDataGetterType

Invoking an instance of this alias is supposed to call InstrumentBase::GetInstrumentData() of the instance the Util::CallableMemberWrapper has been constructed with.

Definition at line 620 of file Instrument.h.

◆ InstrumentDataType

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

Definition at line 531 of file Instrument.h.

◆ InstrumentDataTypeSyncPtrConstType

Alias for the return type of InstrumentBase::GetInstrumentData() const. Data class instances wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way.

Definition at line 543 of file Instrument.h.

◆ InstrumentDataTypeSyncPtrType

Alias for the return type of InstrumentBase::GetInstrumentData(). Data class instances wrapped into Util::SynchronizedPointer can be accessed in a thread-safe way.

Definition at line 537 of file Instrument.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 524 of file Instrument.h.

◆ UpdateTaskType

Defines a task for updating an instrument within an instrument inheritance hierarchy. Each instrument (indirectly) derived from class InstrumentBase must be accompanied by an update task class derived from UpdateTaskBase. Even if the task does not do anything, at least it has to call UpdateTaskBase::UpdateFuncImpl() of the derived instrument's update task class.

Definition at line 552 of file Instrument.h.

Member Enumeration Documentation

◆ TaskHandlingContinuationType

Indicates how an instrument should proceed after handling a task.

Enumerator
Continue 

Task handling should continue, the instrument does not terminate.

Terminate 

Task handling should not continue, the instrument should terminate.

Defer 

Task handling should continue. However, a deferred task currently blocks the instrument queue. Hence, do not enqueue update tasks.

Definition at line 509 of file Instrument.h.

Constructor & Destructor Documentation

◆ InstrumentBase()

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

Constructs an instrument 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.
Util::InvalidArgExceptionis thrown if InstrumentParamsBase::InstrumentData of Params is nullptr.

Definition at line 234 of file Instrument.cpp.

◆ ~InstrumentBase()

DynExp::InstrumentBase::~InstrumentBase ( )
pure virtual

Definition at line 242 of file Instrument.cpp.

Member Function Documentation

◆ AsSyncTask()

template<typename DerivedInstrT , typename... TaskFuncArgTs, typename... ArgTs>
ExceptionContainer DynExp::InstrumentBase::AsSyncTask ( void(DerivedInstrT::*)(TaskFuncArgTs...) const  TaskFunc,
ArgTs &&...  Args 
) const

Calls a (derived) instrument's function which inserts a task into the instrument's task queue synchronously. This means that AsSyncTask() blocks until the task has been executed or aborted. This is achieved by passing a callback function to the task which in turn sets a flag after the task execution. The thread calling AsSyncTask() blocks (via std::this_thread::yield()) until this flag is set.

Template Parameters
DerivedInstrTInstrument type (derived from class InstrumentBase).
...TaskFuncArgTsTypes the task inserting function expects as arguments.
...ArgTsTypes of the arguments passed to AsSyncTask().
Parameters
TaskFuncMember function pointer to (derived) instrument's task-inserting function.
...ArgsArguments to be perfectly forwarded to the task function.
Returns
Return the exception possibly thrown by the task.

Definition at line 1222 of file Instrument.h.

◆ Category()

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

Every derived class has to redefine this function.

Returns
Returns the category of this instrument type.

Definition at line 559 of file Instrument.h.

◆ EnqueueArriveAtLatchTask()

void DynExp::InstrumentBase::EnqueueArriveAtLatchTask ( std::latch &  Latch) const

Enqueues a task which arrives at a latch when executed (instance of class ArriveAtLatchTask).

Parameters
LatchLatch to arrive at.

Definition at line 271 of file Instrument.cpp.

◆ GetCategory()

◆ GetExceptionChild()

std::exception_ptr DynExp::InstrumentBase::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 457 of file Instrument.cpp.

◆ GetExceptionUnsafe()

static auto DynExp::InstrumentBase::GetExceptionUnsafe ( const InstrumentDataTypeSyncPtrConstType InstrumentDataPtr)
inlinestaticprotected

Getter for InstrumentDataBase::InstrumentException. If InstrumentDataBase::InstrumentException is nullptr and InstrumentDataBase::HasException is still true, returns a pointer to a Util::Exception instance.

Returns
Returns the exception being responsible for the instrument's current state.
Parameters
InstrumentDataPtrReference to a pointer to the locked instrument data

Definition at line 660 of file Instrument.h.

◆ GetInstrumentData() [1/2]

InstrumentBase::InstrumentDataTypeSyncPtrType DynExp::InstrumentBase::GetInstrumentData ( const std::chrono::milliseconds  Timeout = GetInstrumentDataTimeoutDefault)

Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase instance and returns a pointer to the locked InstrumentData. Instrument data should not be locked by having called this function while subsequently calling a derived instrument's method which also makes use of the instrument's data by locking it. If this happens (e.g. in a module thread), the instrument data's mutex is locked recursively. In principle, this does no harm since Util::ISynchronizedPointerLockable supports that. But, it is not considered good practice.

Parameters
TimeoutTime to wait for locking the mutex of InstrumentData.
Returns
Returns a pointer to InstrumentDataType (non-const) to allow access all of its members.

Definition at line 246 of file Instrument.cpp.

◆ GetInstrumentData() [2/2]

InstrumentBase::InstrumentDataTypeSyncPtrConstType DynExp::InstrumentBase::GetInstrumentData ( const std::chrono::milliseconds  Timeout = GetInstrumentDataTimeoutDefault) const

Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase instance and returns a pointer to the locked InstrumentData. Instrument data should not be locked by having called this function while subsequently calling a derived instrument's method which also makes use of the instrument's data by locking it. If this happens (e.g. in a module thread), the instrument data's mutex is locked recursively. In principle, this does no harm since Util::ISynchronizedPointerLockable supports that. But, it is not considered good practice.

Parameters
TimeoutTime to wait for locking the mutex of InstrumentData.
Returns
Returns a pointer to const InstrumentDataType, since users of this InstrumentBase instance are not allowed to access non-const members of InstrumentDataType. These are only to be accessed by the main thread!

Definition at line 251 of file Instrument.cpp.

◆ GetNonConstInstrumentData()

InstrumentBase::InstrumentDataTypeSyncPtrType DynExp::InstrumentBase::GetNonConstInstrumentData ( const std::chrono::milliseconds  Timeout = GetInstrumentDataTimeoutDefault) const
private

Always allows InstrumentBase to obtain a non-const pointer to the instrument's data - even in const task functions.

Parameters
TimeoutTime to wait for locking the mutex of InstrumentData.
Returns
Returns a pointer to InstrumentDataType (non-const) to allow access all of its members.

Definition at line 256 of file Instrument.cpp.

◆ GetTaskQueueDelay()

virtual std::chrono::milliseconds DynExp::InstrumentBase::GetTaskQueueDelay ( ) const
inlinevirtual

Specifies in which time intervals the instrument's task queue runs to handle pending tasks.

Returns
Delay time in between task queue executions. Return std::chrono::milliseconds::max() to make the instrument thread only wake up (to handle tasks and to update data) when a task is enqueued. Return 0 to make the task queue delay as small as as possible.

Reimplemented in DynExpInstr::NenionLeakvalveF3, DynExpInstr::NP_Conex_CC, DynExpInstr::RS_SMB100B, DynExpInstr::RS_SMC100A, DynExpInstr::SwabianInstrumentsPulseStreamer, DynExpInstr::ZI_MFLI, DynExpInstr::Camera, DynExpInstr::Laser, DynExpInstr::LockinAmplifier, DynExpInstr::Spectrometer, DynExpInstr::PositionerStage, and DynExpInstr::TimeTagger.

Definition at line 584 of file Instrument.h.

◆ HandleAdditionalTask()

virtual bool DynExp::InstrumentBase::HandleAdditionalTask ( )
inlineprivatevirtual

Determines whether task handling should continue.

Returns
Returns false if task handling (the instrument) should stop, true otherwise.

Definition at line 767 of file Instrument.h.

◆ HandleTask()

InstrumentBase::TaskHandlingContinuationType DynExp::InstrumentBase::HandleTask ( InstrumentInstance Instance)
private

Executes and removes the next pending task from the instrument's task queue.

Parameters
InstanceHandle to the instrument thread's data
Returns
Returns how the instrument should proceed after handling a task at front of the task queue. Always indicates InstrumentBase::TaskHandlingContinuationType::Continue if there is no task to be handled.

Definition at line 278 of file Instrument.cpp.

◆ IsInitialized()

bool DynExp::InstrumentBase::IsInitialized ( ) const
inline

Getter for Initialized.

Returns
Returns true when the instrument is initialized and ready to handle tasks, false othweise.

Definition at line 638 of file Instrument.h.

◆ IsReadyChild()

bool DynExp::InstrumentBase::IsReadyChild ( ) const
finaloverrideprivatevirtual

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).

Returns
Returns true if this Object instance is ready, false otherwise.

Implements DynExp::Object.

Definition at line 467 of file Instrument.cpp.

◆ MakeAndEnqueueTask()

template<typename TaskT , typename... ArgTs>
void DynExp::InstrumentBase::MakeAndEnqueueTask ( ArgTs &&...  Args) const
inlineprotected

Calls MakeTask() to construct a new task and subsequently enqueues the task into the instrument's task queue. Logical const-ness: this is a const member function to allow pointers to const InstrumentBase inserting tasks into the instrument's task queue. These kind of pointers are e.g. returned by RunnableInstance::GetOwner() which can be called by tasks' TaskBase::RunChild() functions. For const InstrumentBase*, it is possible to insert tasks into the task queue, but not to change the InstrumentBase object itself (e.g. calling Object::Reset()).

Template Parameters
TaskTType of a task derived from class TaskBase
...ArgTsTypes of the arguments to forward to the task's constructor
Parameters
...ArgsArguments to forward to the task's constructor

Definition at line 675 of file Instrument.h.

◆ MakeExitTask()

virtual std::unique_ptr< ExitTaskBase > DynExp::InstrumentBase::MakeExitTask ( ) const
inlineprivatevirtual

Factory function for an exit task (ExitTaskBase). Override to define the desired deinitialization task in derived classes only if the respective task really does something and if it has no pure virtual function.

Returns
Pointer to the task (transferring ownership) or nullptr if no exit task is required. A task can be created with MakeTask().

Reimplemented in DynExpInstr::DummyAnalogIn, DynExpInstr::DummyAnalogOut, DynExpInstr::DummyDataStreamInstrument, DynExpInstr::DummyDigitalIn, DynExpInstr::DummyDigitalOut, DynExpInstr::NenionLeakvalveF3, DynExpInstr::NetworkLaser, DynExpInstr::NetworkSpectrometer, DynExpInstr::NIDAQAnalogIn, DynExpInstr::NIDAQAnalogOut, DynExpInstr::NIDAQDigitalIn, DynExpInstr::NIDAQDigitalOut, DynExpInstr::NP_Conex_CC, DynExpInstr::PI_C_862, DynExpInstr::PVCam, DynExpInstr::QutoolsQuTAG, DynExpInstr::RS_SMB100B, DynExpInstr::RS_SMC100A, DynExpInstr::SmarAct, DynExpInstr::SwabianInstrumentsPulseStreamer, DynExpInstr::WidefieldLocalization, DynExpInstr::ZI_MFLI, DynExpInstr::AnalogIn, DynExpInstr::AnalogOut, DynExpInstr::DataStreamInstrument, DynExpInstr::DigitalIn, DynExpInstr::DigitalOut, DynExpInstr::FunctionGenerator, DynExpInstr::gRPCInstrument< DataStreamInstrument, 0, gRPCStubs... >, DynExpInstr::gRPCInstrument< DynExp::InstrumentBase, 0, DynExpProto::WidefieldLocalization::WidefieldLocalization >, DynExpInstr::gRPCInstrument< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser >, DynExpInstr::gRPCInstrument< Spectrometer, 0, DynExpProto::NetworkSpectrometer::NetworkSpectrometer >, DynExpInstr::InputPort, DynExpInstr::LockinAmplifier, DynExpInstr::OutputPort, and DynExpInstr::TimeTagger.

Definition at line 792 of file Instrument.h.

◆ MakeInitTask()

virtual std::unique_ptr< InitTaskBase > DynExp::InstrumentBase::MakeInitTask ( ) const
inlineprivatevirtual

Factory function for an init task (InitTaskBase). Override to define the desired initialization task in derived classes only if the respective task really does something and if it has no pure virtual function.

Returns
Pointer to the task (transferring ownership) or nullptr if no init task is required. A task can be created with MakeTask().

Reimplemented in DynExpInstr::DummyAnalogIn, DynExpInstr::DummyAnalogOut, DynExpInstr::DummyCamera, DynExpInstr::DummyDataStreamInstrument, DynExpInstr::DummyDigitalIn, DynExpInstr::DummyDigitalOut, DynExpInstr::NenionLeakvalveF3, DynExpInstr::NetworkLaser, DynExpInstr::NetworkSpectrometer, DynExpInstr::NIDAQAnalogIn, DynExpInstr::NIDAQAnalogOut, DynExpInstr::NIDAQDigitalIn, DynExpInstr::NIDAQDigitalOut, DynExpInstr::NP_Conex_CC, DynExpInstr::PI_C_862, DynExpInstr::PVCam, DynExpInstr::QutoolsQuTAG, DynExpInstr::RS_SMB100B, DynExpInstr::RS_SMC100A, DynExpInstr::SmarAct, DynExpInstr::SwabianInstrumentsPulseStreamer, DynExpInstr::WidefieldLocalization, DynExpInstr::ZI_MFLI, DynExpInstr::AnalogIn, DynExpInstr::AnalogOut, DynExpInstr::DataStreamInstrument, DynExpInstr::DigitalIn, DynExpInstr::DigitalOut, DynExpInstr::FunctionGenerator, DynExpInstr::gRPCInstrument< DataStreamInstrument, 0, gRPCStubs... >, DynExpInstr::gRPCInstrument< DynExp::InstrumentBase, 0, DynExpProto::WidefieldLocalization::WidefieldLocalization >, DynExpInstr::gRPCInstrument< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser >, DynExpInstr::gRPCInstrument< Spectrometer, 0, DynExpProto::NetworkSpectrometer::NetworkSpectrometer >, DynExpInstr::InputPort, DynExpInstr::LockinAmplifier, DynExpInstr::OutputPort, and DynExpInstr::TimeTagger.

Definition at line 783 of file Instrument.h.

◆ MakeUpdateTask()

virtual std::unique_ptr< UpdateTaskBase > DynExp::InstrumentBase::MakeUpdateTask ( ) const
inlineprivatevirtual

Factory function for an update task (UpdateTaskBase). Override to define the desired update task in derived classes only if the respective task really does something and if it has no pure virtual function.

Returns
Pointer to the task (transferring ownership) or nullptr if no update task is required. A task can be created with MakeTask().

Reimplemented in DynExpInstr::DummyAnalogIn, DynExpInstr::DummyAnalogOut, DynExpInstr::DummyCamera, DynExpInstr::DummyDataStreamInstrument, DynExpInstr::DummyDigitalIn, DynExpInstr::DummyDigitalOut, DynExpInstr::NenionLeakvalveF3, DynExpInstr::NetworkLaser, DynExpInstr::NetworkSpectrometer, DynExpInstr::NIDAQAnalogIn, DynExpInstr::NIDAQAnalogOut, DynExpInstr::NIDAQDigitalIn, DynExpInstr::NIDAQDigitalOut, DynExpInstr::NP_Conex_CC, DynExpInstr::PI_C_862, DynExpInstr::PVCam, DynExpInstr::QutoolsQuTAG, DynExpInstr::RS_SMB100B, DynExpInstr::RS_SMC100A, DynExpInstr::SmarAct, DynExpInstr::SwabianInstrumentsPulseStreamer, DynExpInstr::WidefieldLocalization, DynExpInstr::ZI_MFLI, DynExpInstr::AnalogIn, DynExpInstr::AnalogOut, DynExpInstr::DataStreamInstrument, DynExpInstr::DigitalIn, DynExpInstr::DigitalOut, DynExpInstr::FunctionGenerator, DynExpInstr::gRPCInstrument< DataStreamInstrument, 0, gRPCStubs... >, DynExpInstr::gRPCInstrument< DynExp::InstrumentBase, 0, DynExpProto::WidefieldLocalization::WidefieldLocalization >, DynExpInstr::gRPCInstrument< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser >, DynExpInstr::gRPCInstrument< Spectrometer, 0, DynExpProto::NetworkSpectrometer::NetworkSpectrometer >, DynExpInstr::InputPort, DynExpInstr::LockinAmplifier, DynExpInstr::OutputPort, and DynExpInstr::TimeTagger.

Definition at line 801 of file Instrument.h.

◆ NotifyChild()

void DynExp::InstrumentBase::NotifyChild ( )
finaloverrideprivatevirtual

Notify derived classes that some state has changed (e.g. the termination of Thread is requested) and that the child's event/task queue should run now.

Reimplemented from DynExp::RunnableObject.

Definition at line 388 of file Instrument.cpp.

◆ OnError()

void DynExp::InstrumentBase::OnError ( )
private

Derived classes can perform critical shutdown actions after an error has occurred. Override OnErrorChild() to adjust behavior.

Definition at line 338 of file Instrument.cpp.

◆ OnErrorChild()

virtual void DynExp::InstrumentBase::OnErrorChild ( ) const
inlineprivatevirtual

Derived classes can perform critical shutdown actions after an error has occurred. Override OnErrorChild() to adjust behavior.

Reimplemented in DynExpInstr::NenionLeakvalveF3, DynExpInstr::NIDAQAnalogIn, DynExpInstr::NIDAQAnalogOut, DynExpInstr::NIDAQDigitalIn, DynExpInstr::NIDAQDigitalOut, DynExpInstr::NP_Conex_CC, DynExpInstr::PI_C_862, and DynExpInstr::SmarAct.

Definition at line 760 of file Instrument.h.

◆ OnPrepareExit()

void DynExp::InstrumentBase::OnPrepareExit ( )
private

This function enables derived classes to enqueue tasks to be executed directly before the final exit task (ExitTaskBase). Override OnPrepareExitChild() to adjust behavior.

Definition at line 433 of file Instrument.cpp.

◆ OnPrepareExitChild()

virtual void DynExp::InstrumentBase::OnPrepareExitChild ( ) const
inlineprivatevirtual

This function enables derived classes to enqueue tasks to be executed directly before the final exit task (ExitTaskBase). Override OnPrepareExitChild() to adjust behavior.

Reimplemented in DynExpInstr::AnalogOut, and DynExpInstr::DigitalOut.

Definition at line 761 of file Instrument.h.

◆ ResetImpl() [1/2]

virtual void DynExp::InstrumentBase::ResetImpl ( dispatch_tag< InstrumentBase )
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 DynExpInstr::InterModuleCommunicator, and DynExpInstr::gRPCInstrument< DynExp::InstrumentBase, 0, DynExpProto::WidefieldLocalization::WidefieldLocalization >.

◆ ResetImpl() [2/2]

void DynExp::InstrumentBase::ResetImpl ( dispatch_tag< RunnableObject )
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::RunnableObject.

Definition at line 365 of file Instrument.cpp.

◆ RunChild()

void DynExp::InstrumentBase::RunChild ( )
finaloverrideprivatevirtual

Refer to Run().

Implements DynExp::RunnableObject.

Definition at line 375 of file Instrument.cpp.

◆ SetException()

void DynExp::InstrumentBase::SetException ( std::exception_ptr  Exception)
privatenoexcept

Sets this instrument instance to an error state and tries to store the exception responsible for the error state in InstrumentData. If InstrumentData cannot be locked, still atomically sets an error flag.

Parameters
ExceptionException to store.

Definition at line 324 of file Instrument.cpp.

◆ TerminateChild()

void DynExp::InstrumentBase::TerminateChild ( const std::chrono::milliseconds  Timeout)
finaloverrideprivatevirtual

Signals derived classes that terminating the RunnableObject instance's thread is about to be requested. Derived classes might now enqueue respective exit tasks/events into their task/event queues. Refer to TerminateUnsafe().

Parameters
TimeoutTime to wait until the RunnableObject's thread has ended.

Reimplemented from DynExp::RunnableObject.

Definition at line 395 of file Instrument.cpp.

◆ UpdateAdditionalData()

virtual bool DynExp::InstrumentBase::UpdateAdditionalData ( )
inlineprivatevirtual

Determines whether to enqueue update tasks (UpdateTaskBase).

Returns
Returns true if update tasks should be appended to the instrument's task queue periodically after handling all pending tasks, false otherwise.

Definition at line 774 of file Instrument.h.

◆ UpdateData()

void DynExp::InstrumentBase::UpdateData ( ) const

Enqueues an update task (instance of class UpdateTaskBase).

Definition at line 261 of file Instrument.cpp.

◆ UpdateDataInternal()

void DynExp::InstrumentBase::UpdateDataInternal ( )
private

Inserts an update task (UpdateTaskBase) into the instrument's task queue. Override UpdateAdditionalData() to adjust behavior.

Definition at line 314 of file Instrument.cpp.

Member Data Documentation

◆ GetInstrumentDataTimeoutDefault

constexpr auto DynExp::InstrumentBase::GetInstrumentDataTimeoutDefault = std::chrono::milliseconds(1000)
staticconstexpr

Determines the default timeout for GetInstrumentData() to lock the mutex synchronizing the instrument's data InstrumentData.

Definition at line 591 of file Instrument.h.

◆ Initialized

std::atomic<bool> DynExp::InstrumentBase::Initialized = false
private

Determines whether the init task (InitTaskBase) has run.

Definition at line 805 of file Instrument.h.

◆ InstrumentData

const std::unique_ptr<InstrumentDataType> DynExp::InstrumentBase::InstrumentData
private

Instrument data belonging to this InstrumentBase instance.

Definition at line 804 of file Instrument.h.

◆ InstrumentThreadOnly

InstrumenThreadOnlyType DynExp::InstrumentBase::InstrumentThreadOnly

Allow exclusive access to some of InstrumentBase's private methods to the instrument thread InstrumentThreadMain().

Definition at line 572 of file Instrument.h.


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