From 6967ed910e6f395bdfe01b7f94eadf04480160a5 Mon Sep 17 00:00:00 2001 From: Christoph Froehlich Date: Mon, 8 Jan 2024 22:15:23 +0000 Subject: [PATCH] Use enum instead of std::optional --- .../hardware_interface/hardware_info.hpp | 14 ++++++-- hardware_interface/src/component_parser.cpp | 36 ++++--------------- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/hardware_interface/include/hardware_interface/hardware_info.hpp b/hardware_interface/include/hardware_interface/hardware_info.hpp index 97926030c3f..97abc004382 100644 --- a/hardware_interface/include/hardware_interface/hardware_info.hpp +++ b/hardware_interface/include/hardware_interface/hardware_info.hpp @@ -15,7 +15,6 @@ #ifndef HARDWARE_INTERFACE__HARDWARE_INFO_HPP_ #define HARDWARE_INTERFACE__HARDWARE_INFO_HPP_ -#include #include #include #include @@ -54,6 +53,14 @@ struct MimicJoint double offset = 0.0; }; +/// @brief This enum is used to store the mimic attribute of a joint +enum class MimicAttribute +{ + NOT_SET, + TRUE, + FALSE +}; + /** * This structure stores information about components defined for a specific hardware * in robot's URDF. @@ -65,8 +72,9 @@ struct ComponentInfo /// Type of the component: sensor, joint, or GPIO. std::string type; - /// If the component has a mimic joint tag, for opt-out - std::optional is_mimic{}; + /// Hold the value of the mimic attribute if given, NOT_SET otherwise + MimicAttribute is_mimic = MimicAttribute::NOT_SET; + /** * Name of the command interfaces that can be set, e.g. "position", "velocity", etc. * Used by joints and GPIOs. diff --git a/hardware_interface/src/component_parser.cpp b/hardware_interface/src/component_parser.cpp index 34cfa7813bf..308d03b4dd3 100644 --- a/hardware_interface/src/component_parser.cpp +++ b/hardware_interface/src/component_parser.cpp @@ -102,29 +102,6 @@ std::string get_attribute_value( return element_it->Attribute(attribute_name); } -/// Gets value of the attribute on an XMLelement. -/** - * If parameter is not found, returns specified default value - * - * \param[in] element_it XMLElement iterator to search for the attribute - * \param[in] attribute_name attribute name to search for and return value - * \param[in] default_value When the attribute is not found, this value is returned instead - * \return attribute value - * \throws std::runtime_error if attribute is not found - */ -std::string get_attribute_value_or( - const tinyxml2::XMLElement * element_it, const char * attribute_name, - const std::string default_value) -{ - const tinyxml2::XMLAttribute * attr; - attr = element_it->FindAttribute(attribute_name); - if (!attr) - { - return default_value; - } - return element_it->Attribute(attribute_name); -} - /// Gets value of the attribute on an XMLelement. /** * If attribute is not found throws an error. @@ -345,13 +322,14 @@ ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it { try { - component.is_mimic = - parse_bool(get_attribute_value(component_it, kMimicAttribute, kJointTag)); + component.is_mimic = parse_bool(get_attribute_value(component_it, kMimicAttribute, kJointTag)) + ? MimicAttribute::TRUE + : MimicAttribute::FALSE; } catch (const std::runtime_error & e) { // mimic attribute not set - component.is_mimic = {}; + component.is_mimic = MimicAttribute::NOT_SET; } } @@ -387,7 +365,7 @@ ComponentInfo parse_component_from_xml(const tinyxml2::XMLElement * component_it * and the interface may be an array of a fixed size of the data type. * * \param[in] component_it pointer to the iterator where component - * info should befound + * info should be found * \throws std::runtime_error if a required component attribute or tag is not found. */ ComponentInfo parse_complex_component_from_xml(const tinyxml2::XMLElement * component_it) @@ -703,12 +681,12 @@ std::vector parse_control_resources_from_urdf(const std::string & { throw std::runtime_error("Joint " + joint.name + " not found in URDF"); } - if (!urdf_joint->mimic && joint.is_mimic.value_or(false)) + if (!urdf_joint->mimic && joint.is_mimic == MimicAttribute::TRUE) { throw std::runtime_error( "Joint '" + std::string(joint.name) + "' has no mimic information in the URDF."); } - if (urdf_joint->mimic && joint.is_mimic.value_or(true)) + if (urdf_joint->mimic && joint.is_mimic != MimicAttribute::FALSE) { if (joint.command_interfaces.size() > 0) {