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

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

#include <common.h>

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...
 
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 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 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...
 
common::ObjectDataType dataType
 The data type of the parameter, defined by common::ObjectDataType. More...
 
common::ObjectCode code
 The object code for the parameter, defined by common::ObjectCode. More...
 
common::ObjectFlags flags
 The object flags for the parameter, defined by common::ObjectFlags. More...
 
common::ObjectFlags access
 The access flags for the parameter, defined by common::ObjectFlags. 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
ParameterValue, common::ObjectDataType, common::ObjectCode, common::ObjectFlags

Member Function Documentation

◆ from_json()

void common::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.

◆ getValue() [1/2]

ParameterValue common::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 common::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.

◆ operator<()

bool common::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 common::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 common::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 common::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 common::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 common::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 > common::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 common::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

common::ObjectFlags common::Parameter::access

The access flags for the parameter, defined by common::ObjectFlags.

◆ bitLength

std::uint16_t common::Parameter::bitLength

Bit length of the parameter.

◆ byteLength

int common::Parameter::byteLength

Byte length of the parameter.

◆ code

common::ObjectCode common::Parameter::code

The object code for the parameter, defined by common::ObjectCode.

◆ data

std::vector<std::uint8_t> common::Parameter::data

Holds raw data as a vector of uint8_t elements.

◆ dataType

common::ObjectDataType common::Parameter::dataType

The data type of the parameter, defined by common::ObjectDataType.

◆ flags

common::ObjectFlags common::Parameter::flags

The object flags for the parameter, defined by common::ObjectFlags.

◆ index

std::uint16_t common::Parameter::index

Index of the parameter.

◆ name

std::string common::Parameter::name

Name of the parameter.

◆ subindex

std::uint8_t common::Parameter::subindex

Subindex of the parameter.


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