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,34 @@ 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+
172+ /* *
173+ * @brief Cast Any value with polymorphic fallback for shared_ptr types.
174+ *
175+ * First attempts a direct cast. If that fails and T is a shared_ptr type,
176+ * tries a polymorphic cast via the registry. Returns Expected with error on failure.
177+ */
178+ template <typename T>
179+ [[nodiscard]] Expected<T> tryCastWithPolymorphicFallback (const Any* any) const ;
180+
152181private:
153182 mutable std::mutex storage_mutex_;
154183 mutable std::recursive_mutex entry_mutex_;
@@ -159,6 +188,9 @@ class Blackboard
159188 std::shared_ptr<Entry> createEntryImpl (const std::string& key, const TypeInfo& info);
160189
161190 bool autoremapping_ = false ;
191+
192+ // Optional registry for polymorphic shared_ptr conversions
193+ std::shared_ptr<PolymorphicCastRegistry> polymorphic_registry_;
162194};
163195
164196/* *
@@ -177,6 +209,32 @@ void ImportBlackboardFromJSON(const nlohmann::json& json, Blackboard& blackboard
177209
178210// ------------------------------------------------------
179211
212+ template <typename T>
213+ inline Expected<T> Blackboard::tryCastWithPolymorphicFallback (const Any* any) const
214+ {
215+ // Try direct cast first
216+ auto result = any->tryCast <T>();
217+ if (result)
218+ {
219+ return result.value ();
220+ }
221+
222+ // For shared_ptr types, try polymorphic cast via registry (Issue #943)
223+ if constexpr (is_shared_ptr<T>::value)
224+ {
225+ if (polymorphic_registry_)
226+ {
227+ auto poly_result = any->tryCastWithRegistry <T>(*polymorphic_registry_);
228+ if (poly_result)
229+ {
230+ return poly_result.value ();
231+ }
232+ }
233+ }
234+
235+ return nonstd::make_unexpected (result.error ());
236+ }
237+
180238template <typename T>
181239inline T Blackboard::get (const std::string& key) const
182240{
@@ -188,7 +246,12 @@ inline T Blackboard::get(const std::string& key) const
188246 throw RuntimeError (" Blackboard::get() error. Entry [" , key,
189247 " ] hasn't been initialized, yet" );
190248 }
191- return any_ref.get ()->cast <T>();
249+ auto result = tryCastWithPolymorphicFallback<T>(any);
250+ if (!result)
251+ {
252+ throw std::runtime_error (result.error ());
253+ }
254+ return result.value ();
192255 }
193256 throw RuntimeError (" Blackboard::get() error. Missing key [" , key, " ]" );
194257}
@@ -325,11 +388,17 @@ inline bool Blackboard::get(const std::string& key, T& value) const
325388{
326389 if (auto any_ref = getAnyLocked (key))
327390 {
328- if (any_ref.get ()->empty ())
391+ const auto & any = any_ref.get ();
392+ if (any->empty ())
329393 {
330394 return false ;
331395 }
332- value = any_ref.get ()->cast <T>();
396+ auto result = tryCastWithPolymorphicFallback<T>(any);
397+ if (!result)
398+ {
399+ throw std::runtime_error (result.error ());
400+ }
401+ value = result.value ();
333402 return true ;
334403 }
335404 return false ;
@@ -346,8 +415,13 @@ inline Expected<Timestamp> Blackboard::getStamped(const std::string& key, T& val
346415 return nonstd::make_unexpected (StrCat (" Blackboard::getStamped() error. Entry [" ,
347416 key, " ] hasn't been initialized, yet" ));
348417 }
349- value = entry->value .cast <T>();
350- return Timestamp{ entry->sequence_id , entry->stamp };
418+ auto result = tryCastWithPolymorphicFallback<T>(&entry->value );
419+ if (result)
420+ {
421+ value = result.value ();
422+ return Timestamp{ entry->sequence_id , entry->stamp };
423+ }
424+ return nonstd::make_unexpected (result.error ());
351425 }
352426 return nonstd::make_unexpected (
353427 StrCat (" Blackboard::getStamped() error. Missing key [" , key, " ]" ));
0 commit comments