From 04a514eecc0e02e38f8c104084f43a85f57fabd2 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Tue, 19 Dec 2023 11:47:47 +0100 Subject: [PATCH] alternative to #719 --- include/behaviortree_cpp_v3/basic_types.h | 8 ++++++-- src/xml_parsing.cpp | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/behaviortree_cpp_v3/basic_types.h b/include/behaviortree_cpp_v3/basic_types.h index edfa7aa4b..2ec051cf1 100644 --- a/include/behaviortree_cpp_v3/basic_types.h +++ b/include/behaviortree_cpp_v3/basic_types.h @@ -216,7 +216,11 @@ using Optional = nonstd::expected; * */ using Result = Optional; -const std::unordered_set ReservedPortNames = {"ID", "name", "_description"}; +inline bool IsReservedPortname(StringView name) +{ + return name == "ID" || name == "name" || name == "_description"; +} + class PortInfo { @@ -271,7 +275,7 @@ std::pair CreatePort(PortDirection direction, StringView StringView description = {}) { auto sname = static_cast(name); - if (ReservedPortNames.count(sname) != 0) + if (IsReservedPortname(sname)) { throw std::runtime_error("A port can not use a reserved name. See ReservedPortNames"); } diff --git a/src/xml_parsing.cpp b/src/xml_parsing.cpp index da3aa9d17..d6a8b8d4a 100644 --- a/src/xml_parsing.cpp +++ b/src/xml_parsing.cpp @@ -523,7 +523,7 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element, for (const XMLAttribute* att = element->FirstAttribute(); att; att = att->Next()) { const std::string attribute_name = att->Name(); - if (ReservedPortNames.count(attribute_name) == 0) + if (!IsReservedPortname(attribute_name)) { port_remap[attribute_name] = att->Value(); } @@ -699,7 +699,7 @@ void BT::XMLParser::Pimpl::recursivelyCreateTree(const std::string& tree_ID, for (const XMLAttribute* attr = element->FirstAttribute(); attr != nullptr; attr = attr->Next()) { - if (ReservedPortNames.count(attr->Name()) == 0) + if (!IsReservedPortname(attr->Name())) { new_bb->addSubtreeRemapping(attr->Name(), attr->Value()); } @@ -720,7 +720,7 @@ void BT::XMLParser::Pimpl::recursivelyCreateTree(const std::string& tree_ID, const char* attr_name = attr->Name(); const char* attr_value = attr->Value(); - if (ReservedPortNames.count(attr->Name()) != 0) + if (IsReservedPortname(attr->Name())) { continue; } @@ -779,7 +779,7 @@ void XMLParser::Pimpl::getPortsRecursively(const XMLElement* element, { const char* attr_name = attr->Name(); const char* attr_value = attr->Value(); - if (ReservedPortNames.count(attr_name) == 0 && + if (!IsReservedPortname(attr_name) && TreeNode::isBlackboardPointer(attr_value)) { auto port_name = TreeNode::stripBlackboardPointer(attr_value);