Skip to content

Commit f9c2ab4

Browse files
committed
Enable blackboard matching for types sharing a common base class
1 parent 3021ff0 commit f9c2ab4

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Diff for: include/behaviortree_cpp/basic_types.h

+17-2
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,21 @@ class TypeInfo
350350
template <typename T>
351351
static TypeInfo Create()
352352
{
353+
// store the base class typeid if specialized
354+
if constexpr(is_shared_ptr<T>::value)
355+
{
356+
using Elem = typename T::element_type;
357+
using Base = typename any_cast_base<Elem>::type;
358+
359+
if constexpr(!std::is_same_v<Base, void>)
360+
{
361+
static_assert(std::is_polymorphic_v<Base>, "TypeInfo Base trait specialization "
362+
"must be "
363+
"polymorphic");
364+
return TypeInfo{ typeid(std::shared_ptr<Base>),
365+
GetAnyFromStringFunctor<std::shared_ptr<Base>>() };
366+
}
367+
}
353368
return TypeInfo{ typeid(T), GetAnyFromStringFunctor<T>() };
354369
}
355370

@@ -452,7 +467,8 @@ template <typename T = AnyTypeAllowed>
452467
}
453468
else
454469
{
455-
out = { sname, PortInfo(direction, typeid(T), GetAnyFromStringFunctor<T>()) };
470+
auto type_info = TypeInfo::Create<T>();
471+
out = { sname, PortInfo(direction, type_info.type(), type_info.converter()) };
456472
}
457473
if(!description.empty())
458474
{
@@ -501,7 +517,6 @@ BidirectionalPort(StringView name, StringView description = {})
501517

502518
namespace details
503519
{
504-
505520
template <typename T = AnyTypeAllowed, typename DefaultT = T>
506521
[[nodiscard]] inline std::pair<std::string, PortInfo>
507522
PortWithDefault(PortDirection direction, StringView name, const DefaultT& default_value,

Diff for: include/behaviortree_cpp/blackboard.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace BT
1515
{
16-
1716
/// This type contains a pointer to Any, protected
1817
/// with a locked mutex as long as the object is in scope
1918
using AnyPtrLocked = LockedPtr<Any>;
@@ -257,8 +256,11 @@ inline void Blackboard::set(const std::string& key, const T& value)
257256

258257
std::type_index previous_type = entry.info.type();
259258

259+
// allow matching if any is of the same base
260+
const auto current_type = TypeInfo::Create<T>().type();
261+
260262
// check type mismatch
261-
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type())
263+
if(previous_type != current_type && previous_type != new_value.type())
262264
{
263265
bool mismatching = true;
264266
if(std::is_constructible<StringView, T>::value)

0 commit comments

Comments
 (0)