Skip to content

Commit bb11f7d

Browse files
committed
Enable blackboard matching for types sharing a common base class
1 parent a422dd8 commit bb11f7d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 17 additions & 1 deletion
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
{

include/behaviortree_cpp/blackboard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,11 @@ inline void Blackboard::set(const std::string& key, const T& value)
257257

258258
std::type_index previous_type = entry.info.type();
259259

260+
// allow matching if any is of the same base
261+
const auto current_type = TypeInfo::Create<T>().type();
262+
260263
// check type mismatch
261-
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type())
264+
if(previous_type != current_type && previous_type != new_value.type())
262265
{
263266
bool mismatching = true;
264267
if(std::is_constructible<StringView, T>::value)

0 commit comments

Comments
 (0)