COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Protected Member Functions | Related Symbols | List of all members
comms::field::Optional< TField, TOptions > Class Template Reference

#include "comms/field/Optional.h"

Detailed Description

template<typename TField, typename... TOptions>
class comms::field::Optional< TField, TOptions >

Adaptor class to any other field, that makes the field optional.

When field is optional, it may either exist or not. The behaviour of length(), read() and write() operations depends on the current field's mode.

Template Parameters
TFieldProper type of the field that needs to be optional.
TOptionsZero or more options that modify/refine default behaviour of the field.
Supported options are:
Inheritance diagram for comms::field::Optional< TField, TOptions >:
comms::Field< TOptions >

Public Types

using CommsTag = typename BaseImpl::CommsTag
 Tag indicating type of the field.
 
using Endian = typename BaseImpl::Endian
 Endian used for serialisation.
 
using Field = TField
 Type of the field.
 
using FieldType = typename ParsedOptions::FieldType
 Type of actual extending field specified via comms::option::def::FieldType.
 
using Mode = OptionalMode
 Mode of the field.
 
using ParsedOptions = details::OptionsParser< TOptions... >
 All the options provided to this class bundled into struct.
 
using ValueType = Field
 Value type of this field, equal to Field.
 
using VersionType = typename BaseImpl::VersionType
 Version type.
 

Public Member Functions

 Optional ()=default
 Default constructor.
 
 Optional (const Field &fieldSrc)
 Construct the field.
 
 Optional (const Optional &)=default
 Copy constructor.
 
 Optional (Field &&fieldSrc)
 Construct the field.
 
 Optional (Optional &&)=default
 Move constructor.
 
 ~Optional () noexcept=default
 Destructor.
 
bool canWrite () const
 Check of whether the field has a consistent value for writing.
 
bool doesExist () const
 Check whether mode is equivalent to Mode::Exists.
 
Fieldfield ()
 Get an access to the wrapped field object.
 
const Fieldfield () const
 Get an access to the wrapped field object.
 
Mode getMode () const
 Get current optional mode.
 
const ValueTypegetValue () const
 Get value.
 
VersionType getVersion () const
 Get version of the field.
 
bool isMissing () const
 Check whether mode is equivalent to Mode::Missing.
 
bool isTentative () const
 Check whether mode is equivalent to Mode::Tentative.
 
std::size_t length () const
 Get length required to serialise the current field value.
 
Optionaloperator= (const Optional &)=default
 Copy assignment.
 
Optionaloperator= (Optional &&)=default
 Move assignment.
 
template<typename TIter >
ErrorStatus read (TIter &iter, std::size_t len)
 Read field value from input data sequence.
 
template<typename TIter >
void readNoStatus (TIter &iter)
 Read field value from input data sequence without error check and status report.
 
bool refresh ()
 Refresh the field's value.
 
void setExists ()
 Set mode to Mode::Exists.
 
void setMissing ()
 Set mode to Mode::Missing.
 
void setMode (Mode val)
 Get optional mode.
 
void setTentative ()
 Set mode to Mode::Tentative.
 
template<typename U >
void setValue (U &&val)
 Set value.
 
bool setVersion (VersionType version)
 Default implementation of version update.
 
bool valid () const
 Check validity of the field value.
 
ValueTypevalue ()
 Get an access to the wrapped field object.
 
const ValueTypevalue () const
 Get an access to the wrapped field object.
 
template<typename TIter >
ErrorStatus write (TIter &iter, std::size_t len) const
 Write current field value to output data sequence.
 
template<typename TIter >
void writeNoStatus (TIter &iter) const
 Write current field value to output data sequence without error check and status report.
 

Static Public Member Functions

static constexpr bool canWrite ()
 Default check of whether the field has a consistent value for writing.
 
static constexpr bool hasEmptySerialization ()
 Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
 
static constexpr bool hasFailOnInvalid ()
 Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
 
static constexpr bool hasFieldType ()
 Compile time inquiry of whether comms::option::def::FieldType option has been used.
 
static constexpr bool hasIgnoreInvalid ()
 Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
 
static constexpr bool hasNonDefaultRefresh ()
 Compile time check if this class has non-default refresh functionality.
 
static constexpr bool hasReadNoStatus ()
 Compile time check of whether the field has proper readNoStatus() member function.
 
static constexpr bool hasVarLength ()
 Default check of whether the field has variable length definition via comms::option::def::VarLength option.
 
static constexpr bool hasWriteNoStatus ()
 Compile time check of whether the field has proper writeNoStatus() member function.
 
static constexpr bool isVersionDependent ()
 Compile time check if this class is version dependent.
 
static constexpr std::size_t maxLength ()
 Get maximal length that is required to serialise field of this type.
 
static constexpr std::size_t minLength ()
 Get minimal length that is required to serialise field of this type.
 
static constexpr bool valid ()
 Default validity check.
 

Static Protected Member Functions

template<typename T , typename TIter >
static T readData (TIter &iter)
 Read data from input buffer.
 
template<typename T , std::size_t TSize, typename TIter >
static T readData (TIter &iter)
 Read partial data from input buffer.
 
template<typename T , typename TIter >
static void writeData (T value, TIter &iter)
 Write data into the output buffer.
 
template<std::size_t TSize, typename T , typename TIter >
static void writeData (T value, TIter &iter)
 Write partial data into the output buffer.
 

Related Symbols

(Note that these are not member symbols.)

template<typename T >
constexpr bool isOptional ()
 Compile time check function of whether a provided type is any variant of comms::field::Optional.
 
template<typename TField , typename... TOptions>
bool operator!= (const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
 Non-equality comparison operator.
 
template<typename TField , typename... TOptions>
bool operator< (const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
 Equivalence comparison operator.
 
template<typename TField , typename... TOptions>
bool operator<= (const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
 Equivalence comparison operator.
 
template<typename TField , typename... TOptions>
bool operator== (const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
 Equality comparison operator.
 
template<typename TField , typename... TOptions>
bool operator> (const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
 Equivalence comparison operator.
 
template<typename TField , typename... TOptions>
bool operator>= (const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
 Equivalence comparison operator.
 
template<typename TField , typename... TOptions>
const Optional< TField, TOptions... > & toFieldBase (const Optional< TField, TOptions... > &field)
 Upcast type of the field definition to its parent comms::field::Optional type in order to have access to its internal types.
 
template<typename TField , typename... TOptions>
Optional< TField, TOptions... > & toFieldBase (Optional< TField, TOptions... > &field)
 Upcast type of the field definition to its parent comms::field::Optional type in order to have access to its internal types.
 

Member Typedef Documentation

◆ FieldType

template<typename TField , typename... TOptions>
using comms::field::Optional< TField, TOptions >::FieldType = typename ParsedOptions::FieldType

Type of actual extending field specified via comms::option::def::FieldType.

void if comms::option::def::FieldType hasn't been applied.

◆ Mode

template<typename TField , typename... TOptions>
using comms::field::Optional< TField, TOptions >::Mode = OptionalMode

Mode of the field.

See also
OptionalMode

Constructor & Destructor Documentation

◆ Optional() [1/3]

template<typename TField , typename... TOptions>
comms::field::Optional< TField, TOptions >::Optional ( )
default

Default constructor.

The mode it is created in is OptionalMode::Tentative.

◆ Optional() [2/3]

template<typename TField , typename... TOptions>
comms::field::Optional< TField, TOptions >::Optional ( const Field fieldSrc)
explicit

Construct the field.

Parameters
[in]fieldSrcField to be copied from during construction.

◆ Optional() [3/3]

template<typename TField , typename... TOptions>
comms::field::Optional< TField, TOptions >::Optional ( Field &&  fieldSrc)
explicit

Construct the field.

Parameters
[in]fieldSrcField to be moved from during construction.

Member Function Documentation

◆ canWrite()

template<typename... TOptions>
static constexpr bool comms::Field< TOptions >::canWrite ( )
staticconstexprinherited

Default check of whether the field has a consistent value for writing.

Returns
Always true.

◆ doesExist()

template<typename TField , typename... TOptions>
bool comms::field::Optional< TField, TOptions >::doesExist ( ) const

Check whether mode is equivalent to Mode::Exists.

Convenience wrapper for getMode(), equivalent to

return getMode() == Mode::Exists;
Mode getMode() const
Get current optional mode.
Definition Optional.h:224

◆ getValue()

template<typename TField , typename... TOptions>
const ValueType & comms::field::Optional< TField, TOptions >::getValue ( ) const

Get value.

Implemented by calling value(), but can be overriden in the derived class

◆ getVersion()

template<typename TField , typename... TOptions>
VersionType comms::field::Optional< TField, TOptions >::getVersion ( ) const

Get version of the field.

Exists only if comms::option::def::VersionStorage option has been provided.

◆ hasVarLength()

template<typename... TOptions>
static constexpr bool comms::Field< TOptions >::hasVarLength ( )
staticconstexprinherited

Default check of whether the field has variable length definition via comms::option::def::VarLength option.

Returns
Always false.

◆ isMissing()

template<typename TField , typename... TOptions>
bool comms::field::Optional< TField, TOptions >::isMissing ( ) const

Check whether mode is equivalent to Mode::Missing.

Convenience wrapper for getMode(), equivalent to

return getMode() == Mode::Missing;

◆ isTentative()

template<typename TField , typename... TOptions>
bool comms::field::Optional< TField, TOptions >::isTentative ( ) const

Check whether mode is equivalent to Mode::Tentative.

Convenience wrapper for getMode(), equivalent to

return getMode() == Mode::Tentative;

◆ length()

template<typename TField , typename... TOptions>
std::size_t comms::field::Optional< TField, TOptions >::length ( ) const

Get length required to serialise the current field value.

Returns
If current mode is OptionalMode::Exists, then the function returns whatever length() member function of the wrapped field returns. Otherwise (for both OptionalMode::Missing and OptionalMode::Tentative) 0 is returned.

◆ maxLength()

template<typename TField , typename... TOptions>
static constexpr std::size_t comms::field::Optional< TField, TOptions >::maxLength ( )
staticconstexpr

Get maximal length that is required to serialise field of this type.

Returns
Same as Field::maxLength()

◆ minLength()

template<typename TField , typename... TOptions>
static constexpr std::size_t comms::field::Optional< TField, TOptions >::minLength ( )
staticconstexpr

Get minimal length that is required to serialise field of this type.

Returns
Same as Field::minLength()

◆ read()

template<typename TField , typename... TOptions>
template<typename TIter >
ErrorStatus comms::field::Optional< TField, TOptions >::read ( TIter &  iter,
std::size_t  len 
)

Read field value from input data sequence.

If field is marked as missing (mode is OptionalMode::Missing), function returns comms::ErrorStatus::Success without advancing iterator.
If field is marked as existing (mode is OptionalMode::Exists) the read() member function of the wrapped field object is invoked.
If field is marked to be tentative (mode is OptionalMode::Tentative), the call redirected to wrapped field's read() member function if value of the "len" parameter is greater than 0, i.e. there are still bytes available for reading, and field itself is marked as existing.
Otherwise, field is marked as missing and comms::ErrorStatus::Success is returned.

Parameters
[in,out]iterIterator to read the data.
[in]lenNumber of bytes available for reading.
Returns
Status of read operation.
Postcondition
Iterator is advanced.

◆ readData() [1/2]

template<typename... TOptions>
template<typename T , typename TIter >
static T comms::Field< TOptions >::readData ( TIter &  iter)
staticprotectedinherited

Read data from input buffer.

Use this function to read data from the intput buffer maintained by the caller. The endianness of the data will be as specified in options of the class.

Template Parameters
TReturn type
TIterType of input iterator
Parameters
[in,out]iterInput iterator.
Returns
The integral type value.
Precondition
TSize <= sizeof(T)
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.
Note
Thread safety: Safe for distinct stream buffers, unsafe otherwise.

◆ readData() [2/2]

template<typename... TOptions>
template<typename T , std::size_t TSize, typename TIter >
static T comms::Field< TOptions >::readData ( TIter &  iter)
staticprotectedinherited

Read partial data from input buffer.

Use this function to read data from the intput buffer maintained by the caller. The endianness of the data will be as specified in options of the class.

Template Parameters
TReturn type
TSizenumber of bytes to read
TIterType of input iterator
Parameters
[in,out]iterInput iterator.
Returns
The integral type value.
Precondition
TSize <= sizeof(T)
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The internal pointer of the stream buffer is advanced.
Note
Thread safety: Safe for distinct stream buffers, unsafe otherwise.

◆ readNoStatus()

template<typename TField , typename... TOptions>
template<typename TIter >
void comms::field::Optional< TField, TOptions >::readNoStatus ( TIter &  iter)

Read field value from input data sequence without error check and status report.

Similar to read(), but doesn't perform any correctness checks and doesn't report any failures.

Parameters
[in,out]iterIterator to read the data.
Postcondition
Iterator is advanced.

◆ refresh()

template<typename TField , typename... TOptions>
bool comms::field::Optional< TField, TOptions >::refresh ( )

Refresh the field's value.

Will invoke the refresh() member function of the contained field, only if it is marked as "exists", otherwise false will be returned.

Returns
true if the value has been updated, false otherwise

◆ setExists()

template<typename TField , typename... TOptions>
void comms::field::Optional< TField, TOptions >::setExists ( )

Set mode to Mode::Exists.

Convenience wrapper for setMode(), equivalent to

setMode(Mode::Exists);
void setMode(Mode val)
Get optional mode.
Definition Optional.h:230

◆ setMissing()

template<typename TField , typename... TOptions>
void comms::field::Optional< TField, TOptions >::setMissing ( )

Set mode to Mode::Missing.

Convenience wrapper for setMode(), equivalent to

setMode(Mode::Missing);

◆ setTentative()

template<typename TField , typename... TOptions>
void comms::field::Optional< TField, TOptions >::setTentative ( )

Set mode to Mode::Tentative.

Convenience wrapper for setMode(), equivalent to

setMode(Mode::Tentative);

◆ setValue()

template<typename TField , typename... TOptions>
template<typename U >
void comms::field::Optional< TField, TOptions >::setValue ( U &&  val)

Set value.

Implemented as re-assigning to value(), but can be overriden in the derived class.

◆ setVersion()

template<typename TField , typename... TOptions>
bool comms::field::Optional< TField, TOptions >::setVersion ( VersionType  version)

Default implementation of version update.

Returns
true in case the field contents have changed, false otherwise

◆ valid() [1/2]

template<typename... TOptions>
static constexpr bool comms::Field< TOptions >::valid ( )
staticconstexprinherited

Default validity check.

Always returns true, can be overriden by the derived class

Returns
Always true

◆ valid() [2/2]

template<typename TField , typename... TOptions>
bool comms::field::Optional< TField, TOptions >::valid ( ) const

Check validity of the field value.

Returns
If field is marked to be missing (mode is OptionalMode::Missing), "true" is returned, otherwise valid() member function of the wrapped field is called.

◆ write()

template<typename TField , typename... TOptions>
template<typename TIter >
ErrorStatus comms::field::Optional< TField, TOptions >::write ( TIter &  iter,
std::size_t  len 
) const

Write current field value to output data sequence.

If field is marked as missing (mode is OptionalMode::Missing) or tentative (mode is OptionalMode::Tentative) the function returns comms::ErrorStatus::Success without advancing iterator.
If field is marked as existing (mode is OptionalMode::Exists) the write() member function of the wrapped field object is invoked.

Parameters
[in,out]iterIterator to write the data.
[in]lenMaximal number of bytes that can be written.
Returns
Status of write operation.
Postcondition
Iterator is advanced.

◆ writeData() [1/2]

template<typename... TOptions>
template<typename T , typename TIter >
static void comms::Field< TOptions >::writeData ( value,
TIter &  iter 
)
staticprotectedinherited

Write data into the output buffer.

Use this function to write data to the the buffer maintained by the caller. The endianness of the data will be as specified in the options provided to the class.

Template Parameters
TType of the value to write. Must be integral.
Typeof output iterator
Parameters
[in]valueIntegral type value to be written.
[in,out]iterOutput iterator.
Precondition
The iterator must be valid and can be successfully dereferenced and incremented at least sizeof(T) times.
Postcondition
The iterator is advanced.
Note
Thread safety: Safe for distinct buffers, unsafe otherwise.

◆ writeData() [2/2]

template<typename... TOptions>
template<std::size_t TSize, typename T , typename TIter >
static void comms::Field< TOptions >::writeData ( value,
TIter &  iter 
)
staticprotectedinherited

Write partial data into the output buffer.

Use this function to write partial data to the buffer maintained by the caller. The endianness of the data will be as specified the class options.

Template Parameters
TSizeLength of the value in bytes known in compile time.
TType of the value to write. Must be integral.
TIterType of output iterator
Parameters
[in]valueIntegral type value to be written.
[in,out]iterOutput iterator.
Precondition
TSize <= sizeof(T)
The iterator must be valid and can be successfully dereferenced and incremented at least TSize times.
Postcondition
The iterator is advanced.
Note
Thread safety: Safe for distinct buffers, unsafe otherwise.

◆ writeNoStatus()

template<typename TField , typename... TOptions>
template<typename TIter >
void comms::field::Optional< TField, TOptions >::writeNoStatus ( TIter &  iter) const

Write current field value to output data sequence without error check and status report.

Similar to write(), but doesn't perform any correctness checks and doesn't report any failures.

Parameters
[in,out]iterIterator to write the data.
Postcondition
Iterator is advanced.

Friends And Related Symbol Documentation

◆ isOptional()

template<typename T >
constexpr bool isOptional ( )
related

Compile time check function of whether a provided type is any variant of comms::field::Optional.

Template Parameters
TAny type.
Returns
true in case provided type is any variant of Optional

◆ operator!=()

template<typename TField , typename... TOptions>
bool operator!= ( const Optional< TField, TOptions... > &  field1,
const Optional< TField, TOptions... > &  field2 
)
related

Non-equality comparison operator.

Parameters
[in]field1First field.
[in]field2Second field.
Returns
Result of the non-equality comparison of the contained fields.

◆ operator<()

template<typename TField , typename... TOptions>
bool operator< ( const Optional< TField, TOptions... > &  field1,
const Optional< TField, TOptions... > &  field2 
)
related

Equivalence comparison operator.

Parameters
[in]field1First field.
[in]field2Second field.
Returns
Result of the equivalence comparison of the contained fields.

◆ operator<=()

template<typename TField , typename... TOptions>
bool operator<= ( const Optional< TField, TOptions... > &  field1,
const Optional< TField, TOptions... > &  field2 
)
related

Equivalence comparison operator.

Parameters
[in]field1First field.
[in]field2Second field.
Returns
Result of the equivalence comparison of the contained fields.

◆ operator==()

template<typename TField , typename... TOptions>
bool operator== ( const Optional< TField, TOptions... > &  field1,
const Optional< TField, TOptions... > &  field2 
)
related

Equality comparison operator.

Parameters
[in]field1First field.
[in]field2Second field.
Returns
Result of the equality comparison of the contained fields.

◆ operator>()

template<typename TField , typename... TOptions>
bool operator> ( const Optional< TField, TOptions... > &  field1,
const Optional< TField, TOptions... > &  field2 
)
related

Equivalence comparison operator.

Parameters
[in]field1First field.
[in]field2Second field.
Returns
Result of the equivalence comparison of the contained fields.

◆ operator>=()

template<typename TField , typename... TOptions>
bool operator>= ( const Optional< TField, TOptions... > &  field1,
const Optional< TField, TOptions... > &  field2 
)
related

Equivalence comparison operator.

Parameters
[in]field1First field.
[in]field2Second field.
Returns
Result of the equivalence comparison of the contained fields.

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