Skip to content

Commit 363b666

Browse files
facontidavideclaude
andcommitted
Add polymorphic shared_ptr port support (Issue #943)
Enable passing shared_ptr<Derived> to ports expecting shared_ptr<Base> while preserving ABI and API compatibility. Implementation: - Add PolymorphicCastRegistry class for runtime cast registration - Store registry in BehaviorTreeFactory via PImpl (ABI-safe) - Propagate registry to blackboards via shared_ptr (tree outlives factory) - Add polymorphic cast fallback in Blackboard::get() and TreeNode::getInputStamped() - Add port type validation with polymorphic awareness in XML parser API: - factory.registerPolymorphicCast<Derived, Base>() to register relationships - Supports transitive upcasts (Sphynx -> Cat -> Animal) - Compile-time port type checking still catches invalid connections Also includes clang-tidy fixes for gtest_parallel.cpp and gtest_factory.cpp. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent facf5a0 commit 363b666

12 files changed

Lines changed: 1183 additions & 18 deletions

‎include/behaviortree_cpp/blackboard.h‎

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "behaviortree_cpp/contrib/json.hpp"
55
#include "behaviortree_cpp/exceptions.h"
66
#include "behaviortree_cpp/utils/locked_reference.hpp"
7+
#include "behaviortree_cpp/utils/polymorphic_cast_registry.hpp"
78
#include "behaviortree_cpp/utils/safe_any.hpp"
89

910
#include <memory>
@@ -149,6 +150,25 @@ class Blackboard
149150

150151
const Blackboard* rootBlackboard() const;
151152

153+
/**
154+
* @brief Set the polymorphic cast registry for this blackboard.
155+
*
156+
* The registry enables polymorphic shared_ptr conversions during get().
157+
* This is typically set automatically when creating trees via BehaviorTreeFactory.
158+
*/
159+
void setPolymorphicCastRegistry(std::shared_ptr<PolymorphicCastRegistry> registry)
160+
{
161+
polymorphic_registry_ = std::move(registry);
162+
}
163+
164+
/**
165+
* @brief Get the polymorphic cast registry (may be null).
166+
*/
167+
[[nodiscard]] const PolymorphicCastRegistry* polymorphicCastRegistry() const
168+
{
169+
return polymorphic_registry_.get();
170+
}
171+
152172
private:
153173
mutable std::mutex storage_mutex_;
154174
mutable std::recursive_mutex entry_mutex_;
@@ -159,6 +179,16 @@ class Blackboard
159179
std::shared_ptr<Entry> createEntryImpl(const std::string& key, const TypeInfo& info);
160180

161181
bool autoremapping_ = false;
182+
183+
// Optional registry for polymorphic shared_ptr conversions
184+
std::shared_ptr<PolymorphicCastRegistry> polymorphic_registry_;
185+
186+
// Helpers to cast Any with polymorphic fallback for shared_ptr types (Issue #943)
187+
template <typename T>
188+
[[nodiscard]] Expected<T> tryCastWithPolymorphicFallback(const Any* any) const;
189+
190+
template <typename T>
191+
[[nodiscard]] T castWithPolymorphicFallback(const Any* any) const;
162192
};
163193

164194
/**
@@ -177,6 +207,43 @@ void ImportBlackboardFromJSON(const nlohmann::json& json, Blackboard& blackboard
177207

178208
//------------------------------------------------------
179209

210+
template <typename T>
211+
inline Expected<T> Blackboard::tryCastWithPolymorphicFallback(const Any* any) const
212+
{
213+
// Try direct cast first
214+
auto result = any->tryCast<T>();
215+
if(result)
216+
{
217+
return result.value();
218+
}
219+
220+
// For shared_ptr types, try polymorphic cast via registry (Issue #943)
221+
if constexpr(is_shared_ptr<T>::value)
222+
{
223+
if(polymorphic_registry_)
224+
{
225+
auto poly_result = any->tryCastWithRegistry<T>(*polymorphic_registry_);
226+
if(poly_result)
227+
{
228+
return poly_result.value();
229+
}
230+
}
231+
}
232+
233+
return nonstd::make_unexpected(result.error());
234+
}
235+
236+
template <typename T>
237+
inline T Blackboard::castWithPolymorphicFallback(const Any* any) const
238+
{
239+
auto result = tryCastWithPolymorphicFallback<T>(any);
240+
if(result)
241+
{
242+
return result.value();
243+
}
244+
throw std::runtime_error(result.error());
245+
}
246+
180247
template <typename T>
181248
inline T Blackboard::get(const std::string& key) const
182249
{
@@ -188,7 +255,7 @@ inline T Blackboard::get(const std::string& key) const
188255
throw RuntimeError("Blackboard::get() error. Entry [", key,
189256
"] hasn't been initialized, yet");
190257
}
191-
return any_ref.get()->cast<T>();
258+
return castWithPolymorphicFallback<T>(any);
192259
}
193260
throw RuntimeError("Blackboard::get() error. Missing key [", key, "]");
194261
}
@@ -325,11 +392,12 @@ inline bool Blackboard::get(const std::string& key, T& value) const
325392
{
326393
if(auto any_ref = getAnyLocked(key))
327394
{
328-
if(any_ref.get()->empty())
395+
const auto& any = any_ref.get();
396+
if(any->empty())
329397
{
330398
return false;
331399
}
332-
value = any_ref.get()->cast<T>();
400+
value = castWithPolymorphicFallback<T>(any);
333401
return true;
334402
}
335403
return false;
@@ -346,8 +414,13 @@ inline Expected<Timestamp> Blackboard::getStamped(const std::string& key, T& val
346414
return nonstd::make_unexpected(StrCat("Blackboard::getStamped() error. Entry [",
347415
key, "] hasn't been initialized, yet"));
348416
}
349-
value = entry->value.cast<T>();
350-
return Timestamp{ entry->sequence_id, entry->stamp };
417+
auto result = tryCastWithPolymorphicFallback<T>(&entry->value);
418+
if(result)
419+
{
420+
value = result.value();
421+
return Timestamp{ entry->sequence_id, entry->stamp };
422+
}
423+
return nonstd::make_unexpected(result.error());
351424
}
352425
return nonstd::make_unexpected(
353426
StrCat("Blackboard::getStamped() error. Missing key [", key, "]"));

‎include/behaviortree_cpp/bt_factory.h‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "behaviortree_cpp/behavior_tree.h"
1818
#include "behaviortree_cpp/contrib/json.hpp"
1919
#include "behaviortree_cpp/contrib/magic_enum.hpp"
20+
#include "behaviortree_cpp/utils/polymorphic_cast_registry.hpp"
2021

2122
#include <filesystem>
2223
#include <functional>
@@ -529,6 +530,45 @@ class BehaviorTreeFactory
529530
[[nodiscard]] const std::unordered_map<std::string, SubstitutionRule>&
530531
substitutionRules() const;
531532

533+
/**
534+
* @brief Register a polymorphic cast relationship between Derived and Base types.
535+
*
536+
* This enables passing shared_ptr<Derived> to ports expecting shared_ptr<Base>
537+
* without type mismatch errors. The relationship is automatically applied
538+
* to all trees created from this factory.
539+
*
540+
* Example:
541+
* factory.registerPolymorphicCast<Cat, Animal>();
542+
* factory.registerPolymorphicCast<Sphynx, Cat>();
543+
*
544+
* @tparam Derived The derived class (must inherit from Base)
545+
* @tparam Base The base class (must be polymorphic)
546+
*/
547+
template <typename Derived, typename Base>
548+
void registerPolymorphicCast()
549+
{
550+
polymorphicCastRegistry().registerCast<Derived, Base>();
551+
}
552+
553+
/**
554+
* @brief Access the polymorphic cast registry.
555+
*
556+
* The registry is shared with all trees created from this factory,
557+
* allowing trees to outlive the factory while maintaining access
558+
* to polymorphic cast relationships.
559+
*/
560+
[[nodiscard]] PolymorphicCastRegistry& polymorphicCastRegistry();
561+
[[nodiscard]] const PolymorphicCastRegistry& polymorphicCastRegistry() const;
562+
563+
/**
564+
* @brief Get a shared pointer to the polymorphic cast registry.
565+
*
566+
* This allows trees and blackboards to hold a reference to the registry
567+
* that outlives the factory.
568+
*/
569+
[[nodiscard]] std::shared_ptr<PolymorphicCastRegistry>
570+
polymorphicCastRegistryPtr() const;
571+
532572
private:
533573
struct PImpl;
534574
std::unique_ptr<PImpl> _p;

‎include/behaviortree_cpp/tree_node.h‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,35 @@ inline Expected<Timestamp> TreeNode::getInputStamped(const std::string& key,
591591
}
592592
else
593593
{
594-
destination = any_value.cast<T>();
594+
auto result = any_value.tryCast<T>();
595+
if(result)
596+
{
597+
destination = result.value();
598+
}
599+
else if constexpr(is_shared_ptr<T>::value)
600+
{
601+
// Try polymorphic cast via registry (Issue #943)
602+
if(auto* registry = config().blackboard->polymorphicCastRegistry())
603+
{
604+
auto poly_result = any_value.tryCastWithRegistry<T>(*registry);
605+
if(poly_result)
606+
{
607+
destination = poly_result.value();
608+
}
609+
else
610+
{
611+
throw std::runtime_error(result.error());
612+
}
613+
}
614+
else
615+
{
616+
throw std::runtime_error(result.error());
617+
}
618+
}
619+
else
620+
{
621+
throw std::runtime_error(result.error());
622+
}
595623
}
596624
return Timestamp{ entry->sequence_id, entry->stamp };
597625
}

0 commit comments

Comments
 (0)