Skip to content

Commit f90f822

Browse files
committed
Extend TypeInfo to store and expose base class chain
- Modified TypeInfo::Create<T>() to compute and store the full base type chain using compute_base_chain and to_type_index_vector. - Added a new constructor to TypeInfo to accept a base_chain vector. - Introduced baseChain() accessor and isConvertibleFrom() helper. - Enables runtime introspection and safe type conversions across the base chain.
1 parent d313bec commit f90f822

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,13 @@ class TypeInfo
364364
"specialization "
365365
"must be "
366366
"polymorphic");
367-
return TypeInfo{ typeid(std::shared_ptr<RootBase>),
368-
GetAnyFromStringFunctor<std::shared_ptr<RootBase>>() };
367+
368+
using Chain = compute_base_chain<Elem>;
369+
auto base_chain = to_type_index_vector(Chain{});
370+
371+
return TypeInfo(typeid(std::shared_ptr<RootBase>),
372+
GetAnyFromStringFunctor<std::shared_ptr<RootBase>>(),
373+
std::move(base_chain));
369374
}
370375
}
371376
return TypeInfo{ typeid(T), GetAnyFromStringFunctor<T>() };
@@ -378,6 +383,14 @@ class TypeInfo
378383
: type_info_(type_info), converter_(conv), type_str_(BT::demangle(type_info))
379384
{}
380385

386+
TypeInfo(std::type_index type_info, StringConverter conv,
387+
std::vector<std::type_index>&& base_chain)
388+
: type_info_(type_info)
389+
, converter_(conv)
390+
, type_str_(BT::demangle(type_info))
391+
, base_chain_(std::move(base_chain))
392+
{}
393+
381394
[[nodiscard]] const std::type_index& type() const;
382395

383396
[[nodiscard]] const std::string& typeName() const;
@@ -403,10 +416,21 @@ class TypeInfo
403416
return converter_;
404417
}
405418

419+
[[nodiscard]] bool isConvertibleFrom(const std::type_index& other) const
420+
{
421+
return std::find(base_chain_.begin(), base_chain_.end(), other) != base_chain_.end();
422+
}
423+
424+
[[nodiscard]] const std::vector<std::type_index>& baseChain() const
425+
{
426+
return base_chain_;
427+
}
428+
406429
private:
407430
std::type_index type_info_;
408431
StringConverter converter_;
409432
std::string type_str_;
433+
std::vector<std::type_index> base_chain_;
410434
};
411435

412436
class PortInfo : public TypeInfo

0 commit comments

Comments
 (0)