Skip to content

Conversation

@avdg81
Copy link
Contributor

@avdg81 avdg81 commented Feb 20, 2025

📝 Description
This PR adds a few compile-time checks that avoid attempting to instantiate abstract base classes while loading objects from a serializer. So far, it was not possible to use (smart) pointers to abstract base classes when serializing objects of derived classes. By adding a few compile-time checks, we now can supply (smart) pointers to abstract base classes to the existing (de)serialization code.

For each modified load function, a corresponding unit test has been created.

To keep the scope of changes to the registry as local as possible during a test run, a test class is registered and deregistered as needed using the RAII idiom. To this end, class Serializer has been extended with a member function to deregister data types.

When loading a serialized smart pointer (or an owning raw pointer), don't attempt to instantiate the corresponding data type when it is abstract. This is useful when passing a (smart) pointer to an interface class, which was not supported so far. Note that without the added check, a compile-time error would be raised when passing a (smart) pointer to an abstract class.

Other changes include:
- Added a member function to class `Serializer` to deregister data types for (de)serialization.
- Added four unit tests that verify whether an instance can be saved and loaded through a (smart) pointer to an interface class.
Copy link
Contributor

@rfaasse rfaasse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition to get the serialization of abstract classes to work! Apart from the mysterious build issues, I don't have any blocking comments or questions (since we also discussed it quite a bit already).

markelov208
markelov208 previously approved these changes Feb 24, 2025
Copy link
Contributor

@markelov208 markelov208 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Anne, nice background to make everything serializable. I have no blocking comments. There are no any checks for the abstract classes, should they be added? Thank you.

@rfaasse rfaasse requested a review from roigcarlo February 24, 2025 09:28
@avdg81 avdg81 marked this pull request as ready for review February 24, 2025 09:29
@avdg81 avdg81 requested a review from a team as a code owner February 24, 2025 09:29
Copy link
Contributor

@matekelemen matekelemen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, just a minor comment.

pValue = Kratos::shared_ptr<TDataType>(new TDataType);
if constexpr (!std::is_abstract_v<TDataType>) {
if(!pValue) {
pValue = Kratos::shared_ptr<TDataType>(new TDataType);
Copy link
Contributor

@matekelemen matekelemen Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid double allocation if you know it's an std::shared_ptr.

Suggested change
pValue = Kratos::shared_ptr<TDataType>(new TDataType);
pValue = std::make_shared<TDataType>();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @matekelemen,

Thanks a lot for your review. I completely agree with your suggestion. In fact, I would like to get rid of a few more "naked" news, by using Kratos::make_intrusive and std::make_unique. However, when I tried your suggestion, compilation failed since a few classes don't have a publicly accessible default constructor (see classes NurbsCurveGeometry, NurbsSurfaceGeometry, and NurbsVolumeGeometry). As a result, std::make_shared cannot construct instances of these classes. I guess that the @KratosMultiphysics/technical-committee needs to decide about the desired behavior. If they would like to keep the constructors of the aforementioned classes private, using std::make_shared is not an option. If, however, they feel it is okay to make these constructors public, we can change the implementation as per your suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's nasty. In that case, you can do smth like this in the meantime.

@avdg81
Copy link
Contributor Author

avdg81 commented Feb 27, 2025

@roigcarlo Just a friendly reminder that I'd like to have your opinion on this PR. Is it ready to be merged? Or would you like me to make additional changes? Thanks.

Copy link
Member

@roigcarlo roigcarlo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry for the late reply, I must have marked this as view by mistake. 👍 from my side

@avdg81
Copy link
Contributor Author

avdg81 commented Mar 3, 2025

Hey, sorry for the late reply, I must have marked this as view by mistake. 👍 from my side

No worries. Thanks for the review.

@avdg81 avdg81 merged commit 4c34b3a into master Mar 3, 2025
11 checks passed
@avdg81 avdg81 deleted the core/support-serialization-through-abstract-classes branch March 3, 2025 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GeoMechanicsApplication] Fix serialization of the stress state policies

6 participants