Skip to content

Commit

Permalink
Merge branch 'set_global'
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed May 20, 2024
2 parents 6a184bf + 2b79c50 commit 9b0bc0a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
10 changes: 10 additions & 0 deletions include/behaviortree_cpp/blackboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ class Blackboard

Blackboard::Ptr parent();

// recursively look for parent Blackboard, until you find the root
Blackboard* rootBlackboard();

const Blackboard* rootBlackboard() const;

private:
mutable std::mutex mutex_;
mutable std::recursive_mutex entry_mutex_;
Expand Down Expand Up @@ -197,6 +202,11 @@ inline void Blackboard::unset(const std::string& key)
template <typename T>
inline void Blackboard::set(const std::string& key, const T& value)
{
if(StartWith(key, '@'))
{
rootBlackboard()->set(key.substr(1, key.size() - 1), value);
return;
}
std::unique_lock lock(mutex_);

// check local storage
Expand Down
2 changes: 1 addition & 1 deletion include/behaviortree_cpp/scripting/operators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using SimpleString = SafeAny::SimpleString;
using expr_ptr = std::shared_ptr<struct ExprBase>;

// extended strin to number that consider enums and booleans
double StringToDouble(const Any& value, const Environment& env)
inline double StringToDouble(const Any& value, const Environment& env)
{
const auto str = value.cast<std::string>();
if(str == "true")
Expand Down
27 changes: 19 additions & 8 deletions src/blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ Blackboard::getEntry(const std::string& key) const
// special syntax: "@" will always refer to the root BB
if(StartWith(key, '@'))
{
if(auto parent = parent_bb_.lock())
{
return parent->getEntry(key);
}
else
{
return getEntry(key.substr(1, key.size() - 1));
}
return rootBlackboard()->getEntry(key.substr(1, key.size() - 1));
}

std::unique_lock<std::mutex> lock(mutex_);
Expand Down Expand Up @@ -318,4 +311,22 @@ Blackboard::Entry& Blackboard::Entry::operator=(const Entry& other)
return *this;
}

Blackboard* BT::Blackboard::rootBlackboard()
{
auto bb = static_cast<const Blackboard&>(*this).rootBlackboard();
return const_cast<Blackboard*>(bb);
}

const Blackboard* BT::Blackboard::rootBlackboard() const
{
const Blackboard* bb = this;
Blackboard::Ptr prev = parent_bb_.lock();
while(prev)
{
bb = prev.get();
prev = bb->parent_bb_.lock();
}
return bb;
}

} // namespace BT

0 comments on commit 9b0bc0a

Please sign in to comment.