BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
BT::PolymorphicCastRegistry Class Reference

Registry for polymorphic shared_ptr cast relationships. More...

#include <polymorphic_cast_registry.hpp>

Public Types

using CastFunction = std::function< linb::any(const linb::any &)>
 

Public Member Functions

 PolymorphicCastRegistry (const PolymorphicCastRegistry &)=delete
 
PolymorphicCastRegistryoperator= (const PolymorphicCastRegistry &)=delete
 
 PolymorphicCastRegistry (PolymorphicCastRegistry &&)=delete
 
PolymorphicCastRegistryoperator= (PolymorphicCastRegistry &&)=delete
 
template<typename Derived , typename Base >
void registerCast ()
 Register a Derived -> Base inheritance relationship.
 
bool isConvertible (std::type_index from_type, std::type_index to_type) const
 Check if from_type can be converted to to_type.
 
bool canUpcast (std::type_index from_type, std::type_index to_type) const
 Check if from_type can be UPCAST to to_type (not downcast).
 
nonstd::expected< linb::any, std::string > tryCast (const linb::any &from, std::type_index from_type, std::type_index to_type) const
 Attempt to cast the value to the target type.
 
std::set< std::type_index > getBaseTypes (std::type_index type) const
 Get all registered base types for a given type.
 
void clear ()
 Clear all registrations (mainly for testing).
 

Detailed Description

Registry for polymorphic shared_ptr cast relationships.

This enables passing shared_ptr<Derived> to ports expecting shared_ptr<Base> without breaking ABI compatibility. Users register inheritance relationships at runtime, and the registry handles upcasting/downcasting transparently.

This class is typically owned by BehaviorTreeFactory and passed to Blackboard during tree creation. This avoids global state and makes testing easier.

Usage with BehaviorTreeFactory: BehaviorTreeFactory factory; factory.registerPolymorphicCast<Cat, Animal>(); factory.registerPolymorphicCast<Sphynx, Cat>(); auto tree = factory.createTreeFromText(xml);

Member Function Documentation

◆ canUpcast()

bool BT::PolymorphicCastRegistry::canUpcast ( std::type_index  from_type,
std::type_index  to_type 
) const
inline

Check if from_type can be UPCAST to to_type (not downcast).

This is stricter than isConvertible - only allows going from derived to base, not the reverse.

◆ isConvertible()

bool BT::PolymorphicCastRegistry::isConvertible ( std::type_index  from_type,
std::type_index  to_type 
) const
inline

Check if from_type can be converted to to_type.

Returns true if:

  • from_type == to_type
  • from_type is a registered derived type of to_type (upcast)
  • to_type is a registered derived type of from_type (downcast)

◆ registerCast()

template<typename Derived , typename Base >
void BT::PolymorphicCastRegistry::registerCast ( )
inline

Register a Derived -> Base inheritance relationship.

This enables:

  • Upcasting: shared_ptr<Derived> can be retrieved as shared_ptr<Base>
  • Downcasting: shared_ptr<Base> can be retrieved as shared_ptr<Derived> (via dynamic_pointer_cast, may return nullptr if types don't match)
Template Parameters
DerivedThe derived class (must inherit from Base)
BaseThe base class (must be polymorphic - have virtual functions)

◆ tryCast()

nonstd::expected< linb::any, std::string > BT::PolymorphicCastRegistry::tryCast ( const linb::any &  from,
std::type_index  from_type,
std::type_index  to_type 
) const
inline

Attempt to cast the value to the target type.

Parameters
fromThe source any containing a shared_ptr
from_typeThe type_index of the stored type
to_typeThe target type_index
Returns
The casted any on success, or an error string on failure

The documentation for this class was generated from the following file: