diff --git a/include/behaviortree_cpp_v3/blackboard.h b/include/behaviortree_cpp_v3/blackboard.h index 12cd71502..adede1b38 100644 --- a/include/behaviortree_cpp_v3/blackboard.h +++ b/include/behaviortree_cpp_v3/blackboard.h @@ -92,6 +92,9 @@ class Blackboard template void set(const std::string& key, const T& value) { + if (key.empty()) + throw LogicError("Blackboard::set with empty key is not allowed"); + std::unique_lock lock_entry(entry_mutex_); std::unique_lock lock(mutex_); @@ -142,7 +145,7 @@ class Blackboard { debugMessage(); - throw LogicError("Blackboard::set() failed: once declared, the type of a port " + throw LogicError("Blackboard::set('", key, "') failed: once declared, the type of a port " "shall not change. Declared type [", BT::demangle(previous_type), "] != current type [", BT::demangle(typeid(T)), "]"); @@ -151,6 +154,23 @@ class Blackboard previous_any = std::move(new_value); } + void unset(const std::string& key) + { + std::unique_lock lock(mutex_); + + // check local storage + auto it = storage_.find(key); + if (it == storage_.end()) + { + // No entry, nothing to do. + return; + } +//it->second->value = Any(); +it->second.reset(); +//std::cout << "\n" << it->second->value.empty() << "\n"; + //storage_.erase(it); + } + const PortInfo* portInfo(const std::string& key); void addSubtreeRemapping(StringView internal, StringView external); diff --git a/include/behaviortree_cpp_v3/tree_node.h b/include/behaviortree_cpp_v3/tree_node.h index d6d203125..d33fd6786 100644 --- a/include/behaviortree_cpp_v3/tree_node.h +++ b/include/behaviortree_cpp_v3/tree_node.h @@ -166,6 +166,8 @@ class TreeNode template Result setOutput(const std::string& key, const T& value); + Result unsetOutput(const std::string& key); + // function provide mostrly for debugging purpose to see the raw value // in the port (no remapping and no conversion to a type) StringView getRawPortValue(const std::string& key) const; @@ -321,6 +323,39 @@ inline Result TreeNode::setOutput(const std::string& key, const T& value) return {}; } +inline Result TreeNode::unsetOutput(const std::string& key) +{ + if (!config_.blackboard) + { + return nonstd::make_unexpected("unsetOutput() failed: trying to access a " + "Blackboard(BB) entry, but BB is invalid"); + } + + auto remap_it = config_.output_ports.find(key); + if (remap_it == config_.output_ports.end()) + { + return nonstd::make_unexpected(StrCat("unsetOutput() failed: " + "NodeConfiguration::output_ports " + "does not " + "contain the key: [", + key, "]")); + } + StringView remapped_key = remap_it->second; + if (remapped_key == "=") + { + remapped_key = key; + } + if (isBlackboardPointer(remapped_key)) + { + remapped_key = stripBlackboardPointer(remapped_key); + } + //config_.blackboard->unset(static_cast(remapped_key)); + std::cerr << "\n\n" << key << "\t" << static_cast(remapped_key) << "\n\n"; +config_.blackboard->unset(key); + + return {}; +} + // Utility function to fill the list of ports using T::providedPorts(); template inline void assignDefaultRemapping(NodeConfiguration& config)