@@ -49,12 +49,15 @@ class Blackboard
49
49
{
50
50
std::unique_lock<std::mutex> lock (mutex_);
51
51
// search first if this port was remapped
52
- if ( auto parent = parent_bb_. lock ())
52
+ if (!internal_to_external_. empty ())
53
53
{
54
- auto remapping_it = internal_to_external_.find (key);
55
- if (remapping_it != internal_to_external_.end ())
54
+ if (auto parent = parent_bb_.lock ())
56
55
{
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
+ }
58
61
}
59
62
}
60
63
auto it = storage_.find (key);
@@ -63,18 +66,9 @@ class Blackboard
63
66
64
67
Any* getAny (const std::string& key)
65
68
{
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));
78
72
}
79
73
80
74
/* * Return true if the entry with the given key was found.
@@ -117,14 +111,17 @@ class Blackboard
117
111
118
112
// search first if this port was remapped.
119
113
// 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 ())
122
115
{
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 ())
125
118
{
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
+ }
128
125
}
129
126
}
130
127
@@ -196,7 +193,7 @@ class Blackboard
196
193
}
197
194
}
198
195
199
- void setPortInfo (std::string key, const PortInfo& info);
196
+ void setPortInfo (const std::string & key, const PortInfo& info);
200
197
201
198
const PortInfo* portInfo (const std::string& key);
202
199
@@ -210,7 +207,6 @@ class Blackboard
210
207
{
211
208
std::unique_lock<std::mutex> lock (mutex_);
212
209
storage_.clear ();
213
- internal_to_external_.clear ();
214
210
}
215
211
216
212
// Lock this mutex before using get() and getAny() and unlock it while you have
@@ -239,6 +235,7 @@ class Blackboard
239
235
std::unordered_map<std::string, Entry> storage_;
240
236
std::weak_ptr<Blackboard> parent_bb_;
241
237
std::unordered_map<std::string, std::string> internal_to_external_;
238
+
242
239
};
243
240
244
241
} // namespace BT
0 commit comments