-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow upcasting #944
base: master
Are you sure you want to change the base?
Allow upcasting #944
Conversation
053abb4
to
f16e4f6
Compare
- Introduced `any_cast_base<T>` trait to register base types for polymorphic casting. - Added `is_shared_ptr` and `IsPolymorphicSharedPtr` traits to detect eligible types. - Modified `Any` to store shared_ptr as its registered base class if available. - Enhanced castPtr() and tryCast() to support safe downcasting from stored base class to derived class using static or dynamic pointer casting. - Ensures shared_ptr<Derived> can be stored and retrieved as shared_ptr<Base> when registered.
f16e4f6
to
8feb137
Compare
8feb137
to
38f9b42
Compare
There maybe a better way to allow type casting of a polymorphic class. This pull request introduces support for upcasting and downcasting
So users who wants this behavior needs to register their classes to it's base class. I don't think we can get away without this process... // Register cast base type for self to allow direct cast, otherwise defaults to a dynamic cast
template <>
struct BT::any_cast_base<Greeter>
{
using type = Greeter;
};
// Register cast base type for HelloGreeter
template <>
struct BT::any_cast_base<HelloGreeter>
{
using type = Greeter;
}; Optionally we could add a macro to simplify this process (not in this PR): #define BT_REGISTER_BASE_TYPE(Derived, Base) \
template <> \
struct BT::any_cast_base<Derived> \
{ \
using type = Base; \
};
BT_REGISTER_BASE_TYPE(Greeter, Greeter)
BT_REGISTER_BASE_TYPE(HelloGreeter, Greeter) It's a little bit more permissive, then what I intended initially. You can now downcast to something that is invalid (must still be a valid class of the stored base class), e.g. the blackboard will not see a mismatch, but it will fail when trying to retrieve it in a BT::Node. Regarding the previous point, should we try to downcast early, in the blackboard to stop the tree from running early if it is invalid ? Instead of running the tree and let it fail down the line ? The Sonarcube check fails, but it does not seem related to this PR. |
Added is_polymorphic_safe to defer evaluation of std::is_polymorphic<T> until T is known to be a complete type. This prevents compilation errors when used with forward-declared types.
Introduced _cached_derived_ptr to temporarily store downcasted shared_ptr results which are polymorphic and base-registered.
The I would prefer solution 2 or 3. Solution 1Returns a
Implemented in 3021ff0 Solution 2Clean API:
Implemented in d11512a Solution 3Seperate castPtr function when T is a shared pointer with a polymorphic class and a registered base, e.g.
Not implemented |
Unit tests with desired behavior from #943.
Any Tests Failures
Blackboard Test Failure