Motion Master
|
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 > | |
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... | |
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.
|
static |
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.
ParameterValue
(std::variant), which can be one of the following types:bool
for BOOLEANstd::int8_t
for INTEGER8std::int16_t
for INTEGER16std::int32_t
for INTEGER24 and INTEGER32std::int64_t
for INTEGER64std::uint8_t
for UNSIGNED8, PDO_MAPPING, IDENTITY, COMMAND_PAR, and RECORDstd::uint16_t
for UNSIGNED16std::uint32_t
for UNSIGNED24 and UNSIGNED32std::uint64_t
for UNSIGNED64float
for REAL32double
for REAL64std::string
for VISIBLE_STRING, OCTET_STRING, and UNICODE_STRINGstd::runtime_error | If the dataType is not supported or is unknown. |
|
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.
T | The type to retrieve from the ParameterValue . This type must match the type stored in the variant. |
T
.std::bad_variant_access | If the requested type T does not match the type stored in the ParameterValue . |
|
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.
other | The other Parameter object to compare with. |
|
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.
other | The other Parameter object to compare with. |
index
and subindex
are equal, false otherwise.
|
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.
other | The other Parameter object to compare with. |
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.
value | The value to store, wrapped in a ParameterValue variant. |
std::bad_variant_access | If the value's type does not match the expected type. |
std::runtime_error | If the data type is unsupported. |
|
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.
T | The type of the input value. Must be compatible with ParameterValue. |
value | The value to set. |
std::bad_variant_access | If the type T is incompatible with the expected data type. |
std::runtime_error | If the data type is unsupported during conversion. |
|
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.
j | The JSON object to store serialized data. |
p | The Parameter instance to serialize. |
|
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
.
T | The type to retrieve from the ParameterValue . This type must match the type stored in the variant. |
T
if found; otherwise, std::nullopt
.
|
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.
T | The type of the input value to attempt setting. |
value | The value to attempt to assign to the parameter. |
common::ObjectFlags common::Parameter::access |
The access flags for the parameter, defined by common::ObjectFlags
.
std::uint16_t common::Parameter::bitLength |
Bit length of the parameter.
int common::Parameter::byteLength |
Byte length of the parameter.
common::ObjectCode common::Parameter::code |
The object code for the parameter, defined by common::ObjectCode
.
std::vector<std::uint8_t> common::Parameter::data |
Holds raw data as a vector of uint8_t elements.
common::ObjectDataType common::Parameter::dataType |
The data type of the parameter, defined by common::ObjectDataType
.
common::ObjectFlags common::Parameter::flags |
The object flags for the parameter, defined by common::ObjectFlags
.
std::uint16_t common::Parameter::index |
Index of the parameter.
std::string common::Parameter::name |
Name of the parameter.
std::uint8_t common::Parameter::subindex |
Subindex of the parameter.