Skip to content

Commit f2bdb22

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

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

Diff for: include/behaviortree_cpp/basic_types.h

+21-5
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

@@ -415,7 +430,8 @@ class PortInfo : public TypeInfo
415430
default_value_str_ = BT::toStr(default_value);
416431
}
417432
catch(LogicError&)
418-
{}
433+
{
434+
}
419435
}
420436

421437
[[nodiscard]] const std::string& description() const;
@@ -452,7 +468,8 @@ template <typename T = AnyTypeAllowed>
452468
}
453469
else
454470
{
455-
out = { sname, PortInfo(direction, typeid(T), GetAnyFromStringFunctor<T>()) };
471+
auto type_info = TypeInfo::Create<T>();
472+
out = { sname, PortInfo(direction, type_info.type(), type_info.converter()) };
456473
}
457474
if(!description.empty())
458475
{
@@ -501,7 +518,6 @@ BidirectionalPort(StringView name, StringView description = {})
501518

502519
namespace details
503520
{
504-
505521
template <typename T = AnyTypeAllowed, typename DefaultT = T>
506522
[[nodiscard]] inline std::pair<std::string, PortInfo>
507523
PortWithDefault(PortDirection direction, StringView name, const DefaultT& default_value,
@@ -614,14 +630,14 @@ struct has_static_method_metadata<
614630

615631
template <typename T>
616632
[[nodiscard]] inline PortsList
617-
getProvidedPorts(enable_if<has_static_method_providedPorts<T>> = nullptr)
633+
getProvidedPorts(enable_if<has_static_method_providedPorts<T>> = nullptr)
618634
{
619635
return T::providedPorts();
620636
}
621637

622638
template <typename T>
623639
[[nodiscard]] inline PortsList
624-
getProvidedPorts(enable_if_not<has_static_method_providedPorts<T>> = nullptr)
640+
getProvidedPorts(enable_if_not<has_static_method_providedPorts<T>> = nullptr)
625641
{
626642
return {};
627643
}

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)