Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unset key method to blackboard and matching wrapper to TreeNode #7

Draft
wants to merge 2 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion include/behaviortree_cpp_v3/blackboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class Blackboard
template <typename T>
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<std::mutex> lock_entry(entry_mutex_);
std::unique_lock<std::mutex> lock(mutex_);

Expand Down Expand Up @@ -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)), "]");
Expand All @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions include/behaviortree_cpp_v3/tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class TreeNode
template <typename T>
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;
Expand Down Expand Up @@ -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<std::string>(remapped_key));
std::cerr << "\n\n" << key << "\t" << static_cast<std::string>(remapped_key) << "\n\n";
config_.blackboard->unset(key);

return {};
}

// Utility function to fill the list of ports using T::providedPorts();
template <typename T>
inline void assignDefaultRemapping(NodeConfiguration& config)
Expand Down