Skip to content

Commit 9b0bc0a

Browse files
committed
Merge branch 'set_global'
2 parents 6a184bf + 2b79c50 commit 9b0bc0a

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

include/behaviortree_cpp/blackboard.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ class Blackboard
135135

136136
Blackboard::Ptr parent();
137137

138+
// recursively look for parent Blackboard, until you find the root
139+
Blackboard* rootBlackboard();
140+
141+
const Blackboard* rootBlackboard() const;
142+
138143
private:
139144
mutable std::mutex mutex_;
140145
mutable std::recursive_mutex entry_mutex_;
@@ -197,6 +202,11 @@ inline void Blackboard::unset(const std::string& key)
197202
template <typename T>
198203
inline void Blackboard::set(const std::string& key, const T& value)
199204
{
205+
if(StartWith(key, '@'))
206+
{
207+
rootBlackboard()->set(key.substr(1, key.size() - 1), value);
208+
return;
209+
}
200210
std::unique_lock lock(mutex_);
201211

202212
// check local storage

include/behaviortree_cpp/scripting/operators.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ using SimpleString = SafeAny::SimpleString;
2828
using expr_ptr = std::shared_ptr<struct ExprBase>;
2929

3030
// extended strin to number that consider enums and booleans
31-
double StringToDouble(const Any& value, const Environment& env)
31+
inline double StringToDouble(const Any& value, const Environment& env)
3232
{
3333
const auto str = value.cast<std::string>();
3434
if(str == "true")

src/blackboard.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,7 @@ Blackboard::getEntry(const std::string& key) const
4949
// special syntax: "@" will always refer to the root BB
5050
if(StartWith(key, '@'))
5151
{
52-
if(auto parent = parent_bb_.lock())
53-
{
54-
return parent->getEntry(key);
55-
}
56-
else
57-
{
58-
return getEntry(key.substr(1, key.size() - 1));
59-
}
52+
return rootBlackboard()->getEntry(key.substr(1, key.size() - 1));
6053
}
6154

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

314+
Blackboard* BT::Blackboard::rootBlackboard()
315+
{
316+
auto bb = static_cast<const Blackboard&>(*this).rootBlackboard();
317+
return const_cast<Blackboard*>(bb);
318+
}
319+
320+
const Blackboard* BT::Blackboard::rootBlackboard() const
321+
{
322+
const Blackboard* bb = this;
323+
Blackboard::Ptr prev = parent_bb_.lock();
324+
while(prev)
325+
{
326+
bb = prev.get();
327+
prev = bb->parent_bb_.lock();
328+
}
329+
return bb;
330+
}
331+
321332
} // namespace BT

0 commit comments

Comments
 (0)