15 class HardwareAdapterBase;
16 class SerialCommunicationHardwareAdapter;
17 class QSerialCommunicationHardwareAdapter;
29 template <
typename HardwareAdapterT>
32 return std::make_shared<typename HardwareAdapterT::ConfigType>();
42 template <
typename HardwareAdapterT>
45 return std::make_shared<HardwareAdapterT>(OwnerThreadID, std::move(Params));
62 virtual const char*
GetParamClassTag() const noexcept
override {
return "HardwareAdapterParamsBase"; }
109 constexpr
static auto Category() noexcept {
return ""; }
165 void ResetImpl(dispatch_tag<Object>)
override final;
200 const std::source_location Location = std::source_location::current()) noexcept
201 : Exception(std::move(Description),
Util::
ErrorType::Error, ErrorCode, Location)
225 virtual const char*
GetParamClassTag() const noexcept
override {
return "SerialCommunicationHardwareAdapterParams"; }
237 "Characters which terminate a single line",
true, LineEndingType::LF };
276 constexpr
static auto Name() noexcept {
return "< any >"; }
278 constexpr
static auto Category() noexcept {
return "Communication"; }
310 virtual std::string
GetName()
const override {
return Name(); }
328 std::string ReadLine()
const;
334 std::string ReadAll()
const;
344 std::string WaitForLine(
unsigned int NumTries = 10, std::chrono::milliseconds DelayBetweenTries = std::chrono::milliseconds(10))
const;
407 template <
typename T>
429 void InsertIntoBuffer(
const std::string& String)
const;
434 void ResetImpl(dispatch_tag<HardwareAdapterBase>)
override final;
447 void ReadIntoBuffer()
const;
452 void ClearReadBuffer()
const;
459 bool CheckOverflow()
const;
474 virtual std::string
Read()
const = 0;
480 virtual void Write(
const std::string& String)
const = 0;
493 std::atomic<SerialCommunicationHardwareAdapterParams::LineEndingType> LineEnding = SerialCommunicationHardwareAdapterParams::LineEndingType::LF;
503 case SerialCommunicationHardwareAdapterParams::LineEndingType::LF:
return {
'\n',
'\0' };
504 case SerialCommunicationHardwareAdapterParams::LineEndingType::CRLF:
return {
'\r',
'\n' };
505 case SerialCommunicationHardwareAdapterParams::LineEndingType::CR:
return {
'\r',
'\0' };
506 default:
return {
'\0',
'\0' };
514 if (LineEnding == SerialCommunicationHardwareAdapterParams::LineEndingType::CRLF)
534 virtual const char*
GetParamClassTag() const noexcept
override {
return "QSerialCommunicationHardwareAdapterParams"; }
578 void Write(
const QString String) { WriteChild(String); }
586 void SetCommunicationChannelOpened() const noexcept;
592 void SetCommunicationChannelClosed() const noexcept;
598 void DataRead(const std::
string& String) const;
606 template <typename ExceptionType>
607 void SetException(const ExceptionType& Exception) const;
610 virtual
void OpenChild() = 0;
611 virtual
void CloseChild() = 0;
612 virtual
void ResetChild() = 0;
613 virtual
void ClearChild() = 0;
614 virtual
void FlushChild() = 0;
615 virtual
void ReadChild() = 0;
616 virtual
void WriteChild(const QString& String) = 0;
617 virtual
void Write_endl_Child() = 0;
650 void DataRead(
const std::string& String)
const { Parent.DataRead(String); }
655 template <
typename ExceptionType>
656 void SetException(
const ExceptionType& Exception)
const { Parent.SetException(Exception); }
664 using QWorkerPtrType = std::unique_ptr<QSerialCommunicationHardwareAdapterWorker>;
692 void ResetImpl(dispatch_tag<SerialCommunicationHardwareAdapter>)
override final;
709 void SetCommunicationChannelOpened() const noexcept { CommunicationChannelOpened =
true; }
716 void DataRead(
const std::string& String)
const;
724 template <
typename ExceptionType>
731 auto ExceptionPtr = std::make_exception_ptr(Exception);
733 PendingException = ExceptionPtr;
747 void ClearChild() const override final;
748 void FlushChild() const override final;
749 std::
string Read() const override final;
750 void Write(const std::
string& String) const override final;
751 void Write_endl() const override final;
759 void ThrowPendingException() const;
771 void ClearSig() const;
772 void FlushSig() const;
773 void ReadSig() const;
774 void WriteSig(const QString String) const;
775 void Write_endl_Sig() const;
780 mutable std::atomic<
bool> CommunicationChannelOpened;
781 mutable std::exception_ptr PendingException;
785 template <typename ExceptionType>
788 auto Owner = std::dynamic_pointer_cast<const QSerialCommunicationHardwareAdapter>(GetOwner());
792 Owner->QSerialCommunicationHardwareAdapterWorkerOnly.SetException(Exception);
Implementation of DynExp objects as the base for derived resources and implementation of the object p...
The configurator classes have the task to generate parameter objects (refer to DynExp::ParamsBase) of...
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Defines the base class for a hardware adapter object. Hardware adapters describe interfaces/connectio...
HardwareAdapterBase(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a hardware adapter instance.
void EnsureReadyStateChild([[maybe_unused]] bool IsAutomaticStartup) override final
Ensures that this Object instance is ready by possibly starting its worker thread or by opening conne...
void ThrowException(std::exception_ptr Exception) const
Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped exce...
constexpr static auto Category() noexcept
Every derived class has to redefine this function.
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.
virtual bool IsConnectedChild() const noexcept=0
Determines the connection status of the hardware interface.
virtual ~HardwareAdapterBase()=0
bool IsConnected() const noexcept
Determines the connection status of the hardware interface.
void SetExceptionUnsafe(std::exception_ptr Exception) const
Stores Exception in LastException.
static constexpr auto ShortTimeoutDefault
Default timeout e.g. used as a default for calls to Object::GetException().
virtual std::string GetCategory() const override
Returns the category of this Object type.
virtual void EnsureReadyStateChild()=0
Ensures that this Object instance is ready by possibly starting its worker thread or by opening conne...
std::exception_ptr LastException
Stores the most recent exception caused by a hardware operation.
static constexpr auto HardwareOperationTimeout
Default timeout used to lock the mutex provided by the base class Util::ILockable to synchronize acce...
void ThrowExceptionUnsafe(std::exception_ptr Exception) const
Stores Exception in LastException, wraps it in a Util::ForwardedException and throws the wrapped exce...
auto GetExceptionUnsafe() const
Getter for LastException.
void ResetImpl(dispatch_tag< Object >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
void ResetException() const
Sets LastException to nullptr in a thread-safe way.
virtual void ResetImpl(dispatch_tag< HardwareAdapterBase >)=0
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
Configurator class for HardwareAdapterBase.
virtual ~HardwareAdapterConfiguratorBase()=0
HardwareAdapterConfiguratorBase()=default
Parameter class for HardwareAdapterBase.
void ConfigureParamsImpl(dispatch_tag< ParamsBase >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
DummyParam Dummy
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
HardwareAdapterParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a HardwareAdapterBase instance.
virtual void ConfigureParamsImpl(dispatch_tag< HardwareAdapterParamsBase >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
virtual ~HardwareAdapterParamsBase()=0
Base class for all DynExp Objects like hardware adapters (DynExp::HardwareAdapterBase),...
virtual bool IsReadyChild() const =0
Returns wheter this Object instance is ready (e.g. it is running or connected to a hardware device) a...
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Refer to ParamsBase::dispatch_tag.
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
Abstract base class for object parameter classes. Each class derived from class Object must be accomp...
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
const DynExpCore & Core
Reference to DynExp's core.
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Configurator class for QSerialCommunicationHardwareAdapter.
QSerialCommunicationHardwareAdapterConfigurator()=default
Parameter class for QSerialCommunicationHardwareAdapter.
virtual void ConfigureParamsImpl(dispatch_tag< QSerialCommunicationHardwareAdapterParams >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
void ConfigureParamsImpl(dispatch_tag< SerialCommunicationHardwareAdapterParams >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
QSerialCommunicationHardwareAdapterParams(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a QSerialCommunicationHardwareAdapter instance.
Qt worker which performs actual serial communication hardware operations in a separate thread in orde...
void Read()
Reads from the communication connection's hardware interface.
void Clear()
Clears the communication connection's buffers and state.
void Write_endl()
Writes end of the line character(s) to the communication connection's hardware interface.
void Flush()
Flushes the communication connection's buffers.
void Close()
Closes the communication connection.
void Write(const QString String)
Writes String to the communication connection's hardware interface.
QSerialCommunicationHardwareAdapterWorker()=default
void Open()
Opens the communication connection.
void Reset()
Resets the worker and the communication connection.
Allow exclusive access to some of QSerialCommunicationHardwareAdapter's private methods to QSerialCom...
void SetException(const ExceptionType &Exception) const
Sets Exception as PendingException and stores it by calling HardwareAdapterBase::SetExceptionUnsafe()...
QSerialCommunicationHardwareAdapter & Parent
Owning QSerialCommunicationHardwareAdapter instance.
void DataRead(const std::string &String) const
Passes data to the hardware adapter which has been received by the worker instance.
void SetCommunicationChannelClosed() const noexcept
Sets CommunicationChannelOpened to false.
constexpr QSerialCommunicationHardwareAdapterWorkerOnlyType(QSerialCommunicationHardwareAdapter &Parent) noexcept
Construcs an instance - one for each QSerialCommunicationHardwareAdapter instance.
void SetCommunicationChannelOpened() const noexcept
Sets CommunicationChannelOpened to true.
SerialCommunicationHardwareAdapter is based on a Qt communication object (wrapped by QSerialCommunica...
virtual void InitWorker()
Tells the worker instance to perform initialization steps, e.g. by emitting a Qt signal which is rece...
void SetException(const ExceptionType &Exception) const noexcept
Sets Exception as PendingException and stores it by calling HardwareAdapterBase::SetExceptionUnsafe()...
QSerialCommunicationHardwareAdapterWorkerOnlyType QSerialCommunicationHardwareAdapterWorkerOnly
Allow exclusive access to some of QSerialCommunicationHardwareAdapter's private methods to QSerialCom...
std::unique_ptr< QSerialCommunicationHardwareAdapterWorker > QWorkerPtrType
Pointer-type owning the related QSerialCommunicationHardwareAdapterWorker instance.
virtual void ResetImpl(dispatch_tag< QSerialCommunicationHardwareAdapter >)
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
void SetCommunicationChannelClosed() const noexcept
Sets CommunicationChannelOpened to false.
virtual QWorkerPtrType MakeWorker()=0
Abstract factory function for a worker instance derived from QSerialCommunicationHardwareAdapterWorke...
Defines an exception caused by a serial communication operation of a hardware adapter.
SerialCommunicationException(std::string Description, const int ErrorCode, const std::source_location Location=std::source_location::current()) noexcept
Constructs a SerialCommunicationException instance.
Configurator class for SerialCommunicationHardwareAdapter.
SerialCommunicationHardwareAdapterConfigurator()=default
Parameter class for SerialCommunicationHardwareAdapter.
LineEndingType
Possible line endings sent after writing a line and used to determine the end of a line while reading...
void ConfigureParamsImpl(dispatch_tag< HardwareAdapterParamsBase >) override final
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
virtual void ConfigureParamsImpl(dispatch_tag< SerialCommunicationHardwareAdapterParams >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
SerialCommunicationHardwareAdapterParams(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a SerialCommunicationHardwareAdapter instance.
Defines a hardware adapter for serial communication. Logical const-ness: see declaration of class Dyn...
constexpr static unsigned int GetLineEndingLength(SerialCommunicationHardwareAdapterParams::LineEndingType LineEnding) noexcept
Determines the amount of characters required to express a line ending type defined by SerialCommunica...
auto & operator<<(const T &Value) const
Writes the input parameter calling Write(). Also writes endl at the end automatically.
virtual void Write_endl() const =0
Writes end of the line characters to the underlying hardware interface.
virtual void FlushChild() const
Flushes the underlying hardware interface.
virtual void Write(const std::string &String) const =0
Writes a string to the underlying hardware interface.
std::string LineEndingString
String corresponding to LineEnding. Refer to LineEndingToChar()
virtual void ClearChild() const
Clears internal buffers of the underlying hardware interface.
constexpr static auto GetMaxBufferSize() noexcept
Defines the maximal size of the hardware adapter's (read) buffer.
constexpr static std::array< char, 2 > LineEndingToChar(SerialCommunicationHardwareAdapterParams::LineEndingType LineEnding) noexcept
Converts SerialCommunicationHardwareAdapterParams::LineEndingType to two characters being used as the...
virtual void ResetImpl(dispatch_tag< SerialCommunicationHardwareAdapter >)
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual std::string Read() const =0
Reads a string from the underlying hardware interface.
virtual std::string GetName() const override
Returns the name of this Object type.
virtual std::string GetCategory() const override
Returns the category of this Object type.
auto GetLineEnding() const
Getter for LineEnding. Thread-safe since LineEnding only changes when Object::Reset() is called (itse...
constexpr static auto Name() noexcept
Every derived class has to redefine this function.
std::stringstream ReadBuffer
Buffer storing data read from the underlying physical hardware.
constexpr static auto Category() noexcept
Every derived class has to redefine this function.
Type denoting the end of a line when piped to operator<<(endl)
Interface to allow synchronizing the access to derived classes between different threads by providing...
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.
Implements a QObject belonging to a hardware adapter (derived from DynExp::HardwareAdapterBase) that ...
DynExp's hardware namespace contains the implementation of DynExp hardware adapters which extend DynE...
@ None
Unspecified/arbitrary waveform.
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
HardwareAdapterPtrType MakeHardwareAdapter(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Factory function to generate a hardware adapter of a specific type.
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
size_t ItemIDType
ID type of objects/items managed by DynExp.
ConfiguratorBasePtrType MakeHardwareAdapterConfig()
Factory function to generate a configurator for a specific hardware adapter type.
std::shared_ptr< ConfiguratorBase > ConfiguratorBasePtrType
Alias for a pointer to the configurator base class ConfiguratorBase.
std::shared_ptr< HardwareAdapterBase > HardwareAdapterPtrType
Pointer type to store a hardware adapter (DynExp::HardwareAdapterBase) with.
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
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.
std::ostream & operator<<(std::ostream &stream, const Exception &e)
Writes a DynExp exception in a user-readable way to a stream.
ErrorType
DynExp's error types
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Accumulates include statements to provide a precompiled header.