Skip to content

Commit 7b88aea

Browse files
committed
minor fixes in blackboard
1 parent aa57c5a commit 7b88aea

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

include/behaviortree_cpp/blackboard.h

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ class Blackboard
4949
{
5050
std::unique_lock<std::mutex> lock(mutex_);
5151
// search first if this port was remapped
52-
if (auto parent = parent_bb_.lock())
52+
if(!internal_to_external_.empty())
5353
{
54-
auto remapping_it = internal_to_external_.find(key);
55-
if (remapping_it != internal_to_external_.end())
54+
if (auto parent = parent_bb_.lock())
5655
{
57-
return parent->getAny(remapping_it->second);
56+
auto remapping_it = internal_to_external_.find(key);
57+
if (remapping_it != internal_to_external_.end())
58+
{
59+
return parent->getAny(remapping_it->second);
60+
}
5861
}
5962
}
6063
auto it = storage_.find(key);
@@ -63,18 +66,9 @@ class Blackboard
6366

6467
Any* getAny(const std::string& key)
6568
{
66-
std::unique_lock<std::mutex> lock(mutex_);
67-
// search first if this port was remapped
68-
if (auto parent = parent_bb_.lock())
69-
{
70-
auto remapping_it = internal_to_external_.find(key);
71-
if (remapping_it != internal_to_external_.end())
72-
{
73-
return parent->getAny(remapping_it->second);
74-
}
75-
}
76-
auto it = storage_.find(key);
77-
return (it == storage_.end()) ? nullptr : &(it->second.value);
69+
// "Avoid Duplication in const and Non-const Member Function,"
70+
// on p. 23, in Item 3 "Use const whenever possible," in Effective C++, 3d ed
71+
return const_cast<Any*>( static_cast<const Blackboard &>(*this).getAny(key));
7872
}
7973

8074
/** Return true if the entry with the given key was found.
@@ -117,14 +111,17 @@ class Blackboard
117111

118112
// search first if this port was remapped.
119113
// Change the parent_bb_ in that case
120-
auto remapping_it = internal_to_external_.find(key);
121-
if (remapping_it != internal_to_external_.end())
114+
if(!internal_to_external_.empty())
122115
{
123-
const auto& remapped_key = remapping_it->second;
124-
if (auto parent = parent_bb_.lock())
116+
auto remapping_it = internal_to_external_.find(key);
117+
if (remapping_it != internal_to_external_.end())
125118
{
126-
parent->set(remapped_key, value);
127-
return;
119+
const auto& remapped_key = remapping_it->second;
120+
if (auto parent = parent_bb_.lock())
121+
{
122+
parent->set(remapped_key, value);
123+
return;
124+
}
128125
}
129126
}
130127

@@ -196,7 +193,7 @@ class Blackboard
196193
}
197194
}
198195

199-
void setPortInfo(std::string key, const PortInfo& info);
196+
void setPortInfo(const std::string &key, const PortInfo& info);
200197

201198
const PortInfo* portInfo(const std::string& key);
202199

@@ -210,7 +207,6 @@ class Blackboard
210207
{
211208
std::unique_lock<std::mutex> lock(mutex_);
212209
storage_.clear();
213-
internal_to_external_.clear();
214210
}
215211

216212
// Lock this mutex before using get() and getAny() and unlock it while you have
@@ -239,6 +235,7 @@ class Blackboard
239235
std::unordered_map<std::string, Entry> storage_;
240236
std::weak_ptr<Blackboard> parent_bb_;
241237
std::unordered_map<std::string, std::string> internal_to_external_;
238+
242239
};
243240

244241
} // namespace BT

src/blackboard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BT
44
{
5-
void Blackboard::setPortInfo(std::string key, const PortInfo& info)
5+
void Blackboard::setPortInfo(const std::string& key, const PortInfo& info)
66
{
77
std::unique_lock<std::mutex> lock(mutex_);
88

0 commit comments

Comments
 (0)