Skip to content

Commit a425b07

Browse files
committed
change blackboard entry
1 parent d6c2884 commit a425b07

File tree

7 files changed

+91
-75
lines changed

7 files changed

+91
-75
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,27 @@ using Result = Expected<std::monostate>;
255255
[[nodiscard]]
256256
bool IsAllowedPortName(StringView str);
257257

258-
class PortInfo
258+
struct AnyTypeAllowed
259+
{};
260+
261+
class TypeInfo
259262
{
260263
public:
261-
struct AnyTypeAllowed
262-
{
263-
};
264264

265-
PortInfo(PortDirection direction = PortDirection::INOUT) :
266-
type_(direction), type_info_(typeid(AnyTypeAllowed)),
265+
template <typename T>
266+
static TypeInfo Create() {
267+
return TypeInfo{typeid(T), GetAnyFromStringFunctor<T>()};
268+
}
269+
270+
TypeInfo(): type_info_(typeid(AnyTypeAllowed)),
267271
type_str_("AnyTypeAllowed")
268272
{}
269273

270-
PortInfo(PortDirection direction, std::type_index type_info, StringConverter conv) :
271-
type_(direction), type_info_(type_info), converter_(conv),
274+
TypeInfo(std::type_index type_info, StringConverter conv) :
275+
type_info_(type_info), converter_(conv),
272276
type_str_(BT::demangle(type_info))
273277
{}
274278

275-
[[nodiscard]] PortDirection direction() const;
276-
277279
[[nodiscard]] const std::type_index& type() const;
278280

279281
[[nodiscard]] const std::string& typeName() const;
@@ -289,6 +291,38 @@ class PortInfo
289291
return {};
290292
}
291293

294+
[[nodiscard]] bool isStronglyTyped() const
295+
{
296+
return type_info_ != typeid(AnyTypeAllowed);
297+
}
298+
299+
[[nodiscard]] const StringConverter& converter() const
300+
{
301+
return converter_;
302+
}
303+
304+
private:
305+
306+
std::type_index type_info_;
307+
StringConverter converter_;
308+
std::string type_str_;
309+
};
310+
311+
312+
class PortInfo: public TypeInfo
313+
{
314+
public:
315+
316+
PortInfo(PortDirection direction = PortDirection::INOUT) :
317+
TypeInfo(), direction_(direction)
318+
{}
319+
320+
PortInfo(PortDirection direction, std::type_index type_info, StringConverter conv) :
321+
TypeInfo(type_info, conv), direction_(direction)
322+
{}
323+
324+
[[nodiscard]] PortDirection direction() const;
325+
292326
void setDescription(StringView description);
293327

294328
template <typename T>
@@ -306,27 +340,14 @@ class PortInfo
306340

307341
[[nodiscard]] const std::string& defaultValueString() const;
308342

309-
[[nodiscard]] bool isStronglyTyped() const
310-
{
311-
return type_info_ != typeid(AnyTypeAllowed);
312-
}
313-
314-
[[nodiscard]] const StringConverter& converter() const
315-
{
316-
return converter_;
317-
}
318-
319343
private:
320-
PortDirection type_;
321-
std::type_index type_info_;
322-
StringConverter converter_;
344+
PortDirection direction_;
323345
std::string description_;
324346
Any default_value_;
325347
std::string default_value_str_;
326-
std::string type_str_;
327348
};
328349

329-
template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
350+
template <typename T = AnyTypeAllowed> [[nodiscard]]
330351
std::pair<std::string, PortInfo> CreatePort(PortDirection direction,
331352
StringView name,
332353
StringView description = {})
@@ -357,28 +378,28 @@ std::pair<std::string, PortInfo> CreatePort(PortDirection direction,
357378
}
358379

359380
//----------
360-
template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
381+
template <typename T = AnyTypeAllowed> [[nodiscard]]
361382
inline std::pair<std::string, PortInfo> InputPort(StringView name,
362383
StringView description = {})
363384
{
364385
return CreatePort<T>(PortDirection::INPUT, name, description);
365386
}
366387

367-
template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
388+
template <typename T = AnyTypeAllowed> [[nodiscard]]
368389
inline std::pair<std::string, PortInfo> OutputPort(StringView name,
369390
StringView description = {})
370391
{
371392
return CreatePort<T>(PortDirection::OUTPUT, name, description);
372393
}
373394

374-
template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
395+
template <typename T = AnyTypeAllowed> [[nodiscard]]
375396
inline std::pair<std::string, PortInfo> BidirectionalPort(StringView name,
376397
StringView description = {})
377398
{
378399
return CreatePort<T>(PortDirection::INOUT, name, description);
379400
}
380401
//----------
381-
template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
402+
template <typename T = AnyTypeAllowed> [[nodiscard]]
382403
inline std::pair<std::string, PortInfo> InputPort(StringView name, const T& default_value,
383404
StringView description)
384405
{
@@ -387,7 +408,7 @@ inline std::pair<std::string, PortInfo> InputPort(StringView name, const T& defa
387408
return out;
388409
}
389410

390-
template <typename T = PortInfo::AnyTypeAllowed> [[nodiscard]]
411+
template <typename T = AnyTypeAllowed> [[nodiscard]]
391412
inline std::pair<std::string, PortInfo> BidirectionalPort(StringView name,
392413
const T& default_value,
393414
StringView description)

include/behaviortree_cpp/blackboard.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ class Blackboard
4040
struct Entry
4141
{
4242
Any value;
43-
PortInfo port_info;
43+
TypeInfo info;
44+
StringConverter string_converter;
4445
mutable std::mutex entry_mutex;
45-
46-
Entry(const PortInfo& info) : port_info(info)
47-
{}
48-
49-
Entry(Any&& other_any, const PortInfo& info) :
50-
value(std::move(other_any)), port_info(info)
46+
47+
Entry(const TypeInfo& _info) : info(_info)
5148
{}
5249
};
5350

@@ -149,21 +146,19 @@ class Blackboard
149146
std::scoped_lock lock(entry.entry_mutex);
150147

151148
Any& previous_any = entry.value;
152-
const PortInfo& port_info = entry.port_info;
153149

154150
Any new_value(value);
155151

156152
// special case: entry exists but it is not strongly typed... yet
157-
if (!port_info.isStronglyTyped())
153+
if (!entry.info.isStronglyTyped())
158154
{
159155
// Use the new type to create a new entry that is strongly typed.
160-
entry.port_info =
161-
PortInfo(port_info.direction(), new_value.type(), port_info.converter());
156+
entry.info = TypeInfo::Create<T>();
162157
previous_any = std::move(new_value);
163158
return;
164159
}
165160

166-
std::type_index previous_type = port_info.type();
161+
std::type_index previous_type = entry.info.type();
167162

168163
// check type mismatch
169164
if (previous_type != std::type_index(typeid(T)) &&
@@ -172,7 +167,7 @@ class Blackboard
172167
bool mismatching = true;
173168
if (std::is_constructible<StringView, T>::value)
174169
{
175-
Any any_from_string = port_info.parseString(value);
170+
Any any_from_string = entry.info.parseString(value);
176171
if (any_from_string.empty() == false)
177172
{
178173
mismatching = false;
@@ -204,8 +199,8 @@ class Blackboard
204199
new_value.copyInto(previous_any);
205200
}
206201
}
207-
208-
[[nodiscard]] const PortInfo* portInfo(const std::string& key);
202+
203+
[[nodiscard]] const TypeInfo* entryInfo(const std::string& key);
209204

210205
void addSubtreeRemapping(StringView internal, StringView external);
211206

@@ -217,17 +212,17 @@ class Blackboard
217212

218213
[[deprecated("Use getAnyLocked to access safely an Entry")]]
219214
std::recursive_mutex& entryMutex() const;
220-
221-
void createEntry(const std::string& key, const PortInfo& info);
215+
216+
void createEntry(const std::string& key, const TypeInfo& info);
222217

223218
private:
224219
mutable std::mutex mutex_;
225220
mutable std::recursive_mutex entry_mutex_;
226221
std::unordered_map<std::string, std::shared_ptr<Entry>> storage_;
227222
std::weak_ptr<Blackboard> parent_bb_;
228223
std::unordered_map<std::string, std::string> internal_to_external_;
229-
230-
std::shared_ptr<Entry> createEntryImpl(const std::string &key, const PortInfo& info);
224+
225+
std::shared_ptr<Entry> createEntryImpl(const std::string &key, const TypeInfo& info);
231226

232227
bool autoremapping_ = false;
233228
};

include/behaviortree_cpp/scripting/operators.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ struct ExprAssignment : ExprBase
504504
{
505505
// the very fist assignment can come from any type.
506506
// In the future, type check will be done by Any::copyInto
507-
if (dst_ptr->empty() && entry->port_info.type() == typeid(PortInfo::AnyTypeAllowed))
507+
if (dst_ptr->empty() && entry->info.type() == typeid(AnyTypeAllowed))
508508
{
509509
*dst_ptr = value;
510510
}
@@ -513,8 +513,8 @@ struct ExprAssignment : ExprBase
513513
// special case: string to other type.
514514
// Check if we can use the StringConverter
515515
auto const str = value.cast<std::string>();
516-
const auto& port_info = env.vars->portInfo(key);
517-
if (auto converter = port_info->converter())
516+
const auto& entry_info = env.vars->entryInfo(key);
517+
if (auto converter = entry_info->converter())
518518
{
519519
*dst_ptr = converter(str);
520520
}

src/basic_types.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,20 @@ std::vector<StringView> splitString(const StringView& strToSplit, char delimeter
327327

328328
PortDirection PortInfo::direction() const
329329
{
330-
return type_;
330+
return direction_;
331331
}
332332

333-
const std::type_index& PortInfo::type() const
333+
const std::type_index& TypeInfo::type() const
334334
{
335335
return type_info_;
336336
}
337337

338-
const std::string &PortInfo::typeName() const
338+
const std::string &TypeInfo::typeName() const
339339
{
340340
return type_str_;
341341
}
342342

343-
Any PortInfo::parseString(const char* str) const
343+
Any TypeInfo::parseString(const char* str) const
344344
{
345345
if (converter_)
346346
{
@@ -349,7 +349,7 @@ Any PortInfo::parseString(const char* str) const
349349
return {};
350350
}
351351

352-
Any PortInfo::parseString(const std::string& str) const
352+
Any TypeInfo::parseString(const std::string& str) const
353353
{
354354
if (converter_)
355355
{

src/blackboard.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ std::shared_ptr<Blackboard::Entry> Blackboard::getEntry(const std::string &key)
9797
}
9898

9999

100-
const PortInfo* Blackboard::portInfo(const std::string& key)
100+
const TypeInfo* Blackboard::entryInfo(const std::string& key)
101101
{
102102
std::unique_lock<std::mutex> lock(mutex_);
103103

104104
auto it = storage_.find(key);
105-
return (it == storage_.end()) ? nullptr : &(it->second->port_info);
105+
return (it == storage_.end()) ? nullptr : &(it->second->info);
106106
}
107107

108108
void Blackboard::addSubtreeRemapping(StringView internal, StringView external)
@@ -115,7 +115,7 @@ void Blackboard::debugMessage() const
115115
{
116116
for (const auto& [key, entry] : storage_)
117117
{
118-
auto port_type = entry->port_info.type();
118+
auto port_type = entry->info.type();
119119
if (port_type == typeid(void))
120120
{
121121
port_type = entry->value.type();
@@ -158,13 +158,13 @@ std::recursive_mutex &Blackboard::entryMutex() const
158158
return entry_mutex_;
159159
}
160160

161-
void Blackboard::createEntry(const std::string &key, const PortInfo &info)
161+
void Blackboard::createEntry(const std::string &key, const TypeInfo &info)
162162
{
163163
createEntryImpl(key, info);
164164
}
165165

166166
std::shared_ptr<Blackboard::Entry>
167-
Blackboard::createEntryImpl(const std::string& key, const PortInfo& info)
167+
Blackboard::createEntryImpl(const std::string& key, const TypeInfo& info)
168168
{
169169
std::unique_lock<std::mutex> lock(mutex_);
170170
// This function might be called recursively, when we do remapping, because we move
@@ -174,7 +174,7 @@ Blackboard::createEntryImpl(const std::string& key, const PortInfo& info)
174174
auto storage_it = storage_.find(key);
175175
if(storage_it != storage_.end())
176176
{
177-
const auto& prev_info = storage_it->second->port_info;
177+
const auto& prev_info = storage_it->second->info;
178178
if (prev_info.type() != info.type() &&
179179
prev_info.isStronglyTyped() &&
180180
info.isStronglyTyped())

src/xml_parsing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ TreeNode::Ptr XMLParser::PImpl::createNodeFromXML(const XMLElement* element,
631631
const auto port_key = static_cast<std::string>(param_res.value());
632632

633633
// if the entry already exists, check that the type is the same
634-
if (auto prev_info = blackboard->portInfo(port_key))
634+
if (auto prev_info = blackboard->entryInfo(port_key))
635635
{
636636
// Check consistency of types.
637637
bool const port_type_mismatch = (prev_info->isStronglyTyped() &&

tests/gtest_factory.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,26 @@ TEST(BehaviorTreeFactory, SubTreeWithRemapping)
225225
// Should not throw
226226
tree.tickWhileRunning();
227227

228-
ASSERT_EQ(main_bb->portInfo("talk_hello")->type(), typeid(std::string));
229-
ASSERT_EQ(main_bb->portInfo("talk_bye")->type(), typeid(std::string));
230-
ASSERT_EQ(main_bb->portInfo("talk_out")->type(), typeid(std::string));
228+
ASSERT_EQ(main_bb->entryInfo("talk_hello")->type(), typeid(std::string));
229+
ASSERT_EQ(main_bb->entryInfo("talk_bye")->type(), typeid(std::string));
230+
ASSERT_EQ(main_bb->entryInfo("talk_out")->type(), typeid(std::string));
231231

232-
ASSERT_EQ(talk_bb->portInfo("bye_msg")->type(), typeid(std::string));
233-
ASSERT_EQ(talk_bb->portInfo("hello_msg")->type(), typeid(std::string));
232+
ASSERT_EQ(talk_bb->entryInfo("bye_msg")->type(), typeid(std::string));
233+
ASSERT_EQ(talk_bb->entryInfo("hello_msg")->type(), typeid(std::string));
234234

235235
std::cout << "\n --------------------------------- \n" << std::endl;
236236
main_bb->debugMessage();
237237
std::cout << "\n ----- \n" << std::endl;
238238
talk_bb->debugMessage();
239239
std::cout << "\n --------------------------------- \n" << std::endl;
240240

241-
ASSERT_EQ(main_bb->portInfo("talk_hello")->type(), typeid(std::string));
242-
ASSERT_EQ(main_bb->portInfo("talk_bye")->type(), typeid(std::string));
243-
ASSERT_EQ(main_bb->portInfo("talk_out")->type(), typeid(std::string));
241+
ASSERT_EQ(main_bb->entryInfo("talk_hello")->type(), typeid(std::string));
242+
ASSERT_EQ(main_bb->entryInfo("talk_bye")->type(), typeid(std::string));
243+
ASSERT_EQ(main_bb->entryInfo("talk_out")->type(), typeid(std::string));
244244

245-
ASSERT_EQ(talk_bb->portInfo("bye_msg")->type(), typeid(std::string));
246-
ASSERT_EQ(talk_bb->portInfo("hello_msg")->type(), typeid(std::string));
247-
ASSERT_EQ(talk_bb->portInfo("output")->type(), typeid(std::string));
245+
ASSERT_EQ(talk_bb->entryInfo("bye_msg")->type(), typeid(std::string));
246+
ASSERT_EQ(talk_bb->entryInfo("hello_msg")->type(), typeid(std::string));
247+
ASSERT_EQ(talk_bb->entryInfo("output")->type(), typeid(std::string));
248248

249249
ASSERT_EQ(main_bb->get<std::string>("talk_hello"), "hello");
250250
ASSERT_EQ(main_bb->get<std::string>("talk_bye"), "bye bye");

0 commit comments

Comments
 (0)