Motion Master
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
mm::comm::base::Parameter Class Reference

Represents a device parameter identified by index and subindex. More...

#include <base.h>

Public Types

using Address = std::pair< uint16_t, uint8_t >
 Alias for a pair of uint16_t and uint8_t representing a parameter address. More...
 

Public Member Functions

ParameterValue getValue () const
 Retrieves the value of the parameter based on its data type. More...
 
template<typename T >
getValue () const
 Retrieves the parameter value from the ParameterValue variant as the specified type. More...
 
template<typename T >
std::optional< T > tryGetValue () const
 Attempts to retrieve the parameter value from the ParameterValue variant as the specified type. More...
 
template<typename T >
getIntValue ()
 Converts the internal byte vector to an integer of type T. More...
 
void setValue (const ParameterValue &value)
 Sets the internal raw data representation from a given value. More...
 
template<typename T >
void setValue (const T &value)
 Sets the parameter value using a strongly-typed input. More...
 
template<typename T >
bool trySetValue (const T &value)
 Attempts to set the parameter value with a strongly-typed input. More...
 
bool hasAllRead () const
 Checks if all read access flags are set in the access field. More...
 
bool hasAllWrite () const
 Checks if all write access flags are set in the access field. More...
 
bool operator< (const Parameter &other) const
 Compares two Parameter objects for ordering. More...
 
bool operator> (const Parameter &other) const
 Compares two Parameter objects for reverse ordering. More...
 
bool operator== (const Parameter &other) const
 Checks if two Parameter objects are equal. More...
 

Static Public Member Functions

static void to_json (nlohmann::json &j, const Parameter &p)
 Serializes a Parameter object to JSON. More...
 
static void from_json (const nlohmann::json &j, Parameter &p)
 Deserializes a Parameter object from JSON. More...
 

Public Attributes

std::string group
 Name of the group this parameter belongs to. Applicable only to ARRAY and RECORD objects. More...
 
std::string name
 Name of the parameter. More...
 
std::uint16_t index
 Index of the parameter. More...
 
std::uint8_t subindex
 Subindex of the parameter. More...
 
std::uint16_t bitLength
 Bit length of the parameter. More...
 
int byteLength
 Byte length of the parameter. More...
 
ObjectDataType dataType
 The data type of the parameter. More...
 
ObjectCode code
 The object code for the parameter. More...
 
ObjectFlags flags
 The object flags for the parameter. More...
 
ObjectFlags access
 The access flags for the parameter. More...
 
PdoMappingStatus pdoMappingStatus
 PDO mapping status of this parameter. More...
 
std::vector< std::uint8_t > data
 Holds raw data as a vector of uint8_t elements. More...
 

Detailed Description

Represents a device parameter identified by index and subindex.

The Parameter class models an object from a device's object dictionary, uniquely identified by a 16-bit index and an 8-bit subindex. These parameters typically appear in communication profiles such as CANopen or other embedded protocols that use structured configuration and runtime data.

Each Parameter holds metadata (name, data type, access rights, etc.) and stores its actual value as a byte array. The value can be safely interpreted and manipulated through variant-based and templated getter/setter functions, ensuring proper type handling.

The class also provides comparison operators for sorting or lookup based on index/subindex and supports JSON serialization for configuration export/import.

See also
mm::core::ParameterValue, mm::core::ObjectDataType, mm::core::ObjectCode, mm::core::ObjectFlags

Member Typedef Documentation

◆ Address

using mm::comm::base::Parameter::Address = std::pair<uint16_t, uint8_t>

Alias for a pair of uint16_t and uint8_t representing a parameter address.

This alias simplifies using a std::pair<uint16_t, uint8_t> to identify a specific device parameter, where the first element (uint16_t) is the index and the second element (uint8_t) is the subindex.

Member Function Documentation

◆ from_json()

void mm::comm::base::Parameter::from_json ( const nlohmann::json &  j,
Parameter p 
)
static

Deserializes a Parameter object from JSON.

Parses the JSON object and populates the fields of the given Parameter instance. This assumes the JSON structure matches the format produced by to_json.

Parameters
jThe JSON object to deserialize.
pThe Parameter instance to populate.

◆ getIntValue()

template<typename T >
T mm::comm::base::Parameter::getIntValue ( )
inline

Converts the internal byte vector to an integer of type T.

This function interprets the stored data vector as an integer of type T, starting at offset 0, assuming little-endian byte order.

Template Parameters
TThe integral type to convert to (e.g., int32_t, uint16_t).
Returns
T The integer value converted from the internal byte vector.
Note
The conversion uses little-endian byte order.

◆ getValue() [1/2]

ParameterValue mm::comm::base::Parameter::getValue ( ) const

Retrieves the value of the parameter based on its data type.

This function extracts the raw data from the parameter's internal storage (data), converts it to the appropriate type based on the dataType, and returns it as a ParameterValue (which is a std::variant). The supported types include various integer, floating-point, and string types. For string types (VISIBLE_STRING, OCTET_STRING, UNICODE_STRING), the function ensures the string is null-terminated.

Returns
ParameterValue The value of the parameter as a ParameterValue (std::variant), which can be one of the following types:
  • bool for BOOLEAN
  • std::int8_t for INTEGER8
  • std::int16_t for INTEGER16
  • std::int32_t for INTEGER24 and INTEGER32
  • std::int64_t for INTEGER64
  • std::uint8_t for UNSIGNED8, PDO_MAPPING, IDENTITY, COMMAND_PAR, and RECORD
  • std::uint16_t for UNSIGNED16
  • std::uint32_t for UNSIGNED24 and UNSIGNED32
  • std::uint64_t for UNSIGNED64
  • float for REAL32
  • double for REAL64
  • std::string for VISIBLE_STRING, OCTET_STRING, and UNICODE_STRING
Exceptions
std::runtime_errorIf the dataType is not supported or is unknown.

◆ getValue() [2/2]

template<typename T >
T mm::comm::base::Parameter::getValue ( ) const
inline

Retrieves the parameter value from the ParameterValue variant as the specified type.

This function attempts to extract the stored parameter value from the ParameterValue variant, and if successful, it returns the value as the specified type T. If the type T does not match the type stored in the variant, a std::bad_variant_access exception is thrown.

Template Parameters
TThe type to retrieve from the ParameterValue. This type must match the type stored in the variant.
Returns
T The value of the parameter, cast to the type T.
Exceptions
std::bad_variant_accessIf the requested type T does not match the type stored in the ParameterValue.

◆ hasAllRead()

bool mm::comm::base::Parameter::hasAllRead ( ) const

Checks if all read access flags are set in the access field.

This function tests whether the access member has all bits corresponding to ObjectFlags::ALL_RD set, indicating full read access permissions.

Returns
true if all read access flags are set; false otherwise.

◆ hasAllWrite()

bool mm::comm::base::Parameter::hasAllWrite ( ) const

Checks if all write access flags are set in the access field.

This function tests whether the access member has all bits corresponding to ObjectFlags::ALL_WR set, indicating full write access permissions.

Returns
true if all write access flags are set; false otherwise.

◆ operator<()

bool mm::comm::base::Parameter::operator< ( const Parameter other) const
inline

Compares two Parameter objects for ordering.

The comparison is first done by the index property. If the indices are equal, the comparison is then done by the subindex property.

Parameters
otherThe other Parameter object to compare with.
Returns
True if the current object is less than the other object, false otherwise.

◆ operator==()

bool mm::comm::base::Parameter::operator== ( const Parameter other) const
inline

Checks if two Parameter objects are equal.

The equality check is performed by comparing the index and subindex properties. Both properties must be equal for the objects to be considered equal.

Parameters
otherThe other Parameter object to compare with.
Returns
True if both the index and subindex are equal, false otherwise.

◆ operator>()

bool mm::comm::base::Parameter::operator> ( const Parameter other) const
inline

Compares two Parameter objects for reverse ordering.

The comparison is first done by the index property. If the indices are equal, the comparison is then done by the subindex property.

Parameters
otherThe other Parameter object to compare with.
Returns
True if the current object is greater than the other object, false otherwise.

◆ setValue() [1/2]

void mm::comm::base::Parameter::setValue ( const ParameterValue value)

Sets the internal raw data representation from a given value.

This function converts the provided value into a byte representation and stores it in the internal data vector, based on the current dataType. It supports all standard types defined in ObjectDataType.

If the value is a vector of bytes (std::vector<std::uint8_t>), it is directly copied into the internal buffer. Otherwise, the value is cast to the expected type based on dataType, converted to bytes, and stored.

For string types (VISIBLE_STRING, OCTET_STRING, UNICODE_STRING), the string content is copied and null-terminated if not already.

Parameters
valueThe value to store, wrapped in a ParameterValue variant.
Exceptions
std::bad_variant_accessIf the value's type does not match the expected type.
std::runtime_errorIf the data type is unsupported.

◆ setValue() [2/2]

template<typename T >
void mm::comm::base::Parameter::setValue ( const T &  value)
inline

Sets the parameter value using a strongly-typed input.

This templated overload constructs a ParameterValue variant from the provided typed value and delegates to the main setValue function for byte-level storage based on the current ObjectDataType.

Template Parameters
TThe type of the input value. Must be compatible with ParameterValue.
Parameters
valueThe value to set.
Exceptions
std::bad_variant_accessIf the type T is incompatible with the expected data type.
std::runtime_errorIf the data type is unsupported during conversion.

◆ to_json()

void mm::comm::base::Parameter::to_json ( nlohmann::json &  j,
const Parameter p 
)
static

Serializes a Parameter object to JSON.

Converts the given Parameter object into a JSON representation, storing its fields such as name, index, data type, etc. Enums are cast to uint16_t to ensure compatibility with JSON output.

Parameters
jThe JSON object to store serialized data.
pThe Parameter instance to serialize.

◆ tryGetValue()

template<typename T >
std::optional< T > mm::comm::base::Parameter::tryGetValue ( ) const
inline

Attempts to retrieve the parameter value from the ParameterValue variant as the specified type.

This function tries to extract the stored parameter value from the ParameterValue variant and returns it as the specified type T in an std::optional<T>. If the type T does not match the type stored in the variant, the function returns std::nullopt.

Template Parameters
TThe type to retrieve from the ParameterValue. This type must match the type stored in the variant.
Returns
std::optional<T> An optional containing the value of type T if found; otherwise, std::nullopt.

◆ trySetValue()

template<typename T >
bool mm::comm::base::Parameter::trySetValue ( const T &  value)
inline

Attempts to set the parameter value with a strongly-typed input.

This templated method checks whether the type of the input value matches the expected type based on the current ObjectDataType. If compatible, it sets the value; otherwise, it fails silently.

Template Parameters
TThe type of the input value to attempt setting.
Parameters
valueThe value to attempt to assign to the parameter.
Returns
true if the type matches the expected data type and the value was set; false otherwise.
Note
Supports raw byte input via std::vector<std::uint8_t> as a fallback.

Member Data Documentation

◆ access

ObjectFlags mm::comm::base::Parameter::access

The access flags for the parameter.

◆ bitLength

std::uint16_t mm::comm::base::Parameter::bitLength

Bit length of the parameter.

◆ byteLength

int mm::comm::base::Parameter::byteLength

Byte length of the parameter.

◆ code

ObjectCode mm::comm::base::Parameter::code

The object code for the parameter.

◆ data

std::vector<std::uint8_t> mm::comm::base::Parameter::data

Holds raw data as a vector of uint8_t elements.

◆ dataType

ObjectDataType mm::comm::base::Parameter::dataType

The data type of the parameter.

◆ flags

ObjectFlags mm::comm::base::Parameter::flags

The object flags for the parameter.

◆ group

std::string mm::comm::base::Parameter::group

Name of the group this parameter belongs to. Applicable only to ARRAY and RECORD objects.

◆ index

std::uint16_t mm::comm::base::Parameter::index

Index of the parameter.

◆ name

std::string mm::comm::base::Parameter::name

Name of the parameter.

◆ pdoMappingStatus

PdoMappingStatus mm::comm::base::Parameter::pdoMappingStatus

PDO mapping status of this parameter.

Represents whether the parameter is mapped in a Process Data Object (PDO), and if so, whether it is mapped as a receive (RX) or transmit (TX) PDO.

◆ subindex

std::uint8_t mm::comm::base::Parameter::subindex

Subindex of the parameter.


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