Use deserialize to remove CreateComponentImplementation's template dependency #1148
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Signed-off-by: Ashton Larkin [email protected]
🦟 Bug fix
Summary
When I worked on #856, I came across an issue where I could not update derived component data when dealing with
BaseComponent
pointers inEntityComponentManager::CreateComponentImplementation
: #856 (comment)The workaround at the time was to have
CreateComponentImplementation
return a flag that indicated whether the derived component data needed to be updated or not. If the data needed to be updated, it was taken care of in the template method that calledCreateComponentImplementation
, since the template method knew what the derived component type is.However, after working on #1131, I realized that we can use component's
Serialize
/Deserialize
methods inEntityComponentManager::CreateComponentImplementation
to properly update the derived component data when working withBaseComponent
pointers. The reason why this works is because these serialization methods are virtual methods that are implemented by both theBaseComponent
and derived component classes. A regular assignment operator wouldn't work because the component classes don't have this operator implemented as a virtual override (see https://godbolt.org/z/hezKTbeEj for an example of what happens when a default assignment operator is used with polymorphism - spoiler alert: the derived class data is lost in the assignment).As a result of this change,
CreateComponentImplementation
can return a pointer to the component instead of a update flag, and the template method that callsCreateComponentImplementation
can then use this component directly. I can't change the signature of this method inign-gazebo6
since it will break ABI, but I left aTODO
note about fixing it inign-gazebo7
. SinceCreateComponentImplementation
is a private method, we don't need to tick-tock it, right? We can change a private method without having to deprecate it, if I understand correctly.Checklist
codecheck
passed (See contributing)Note to maintainers: Remember to use Squash-Merge