Add Component::extractComponent and Component::removeComponent
#4174
+160
−14
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.
Fixes issue N/A
This PR adds
Component::extractComponentandComponent::removeComponentto theComponentAPI.Abstractly, the motivation for doing this is that the
ComponentAPI currently only supports adding generic components viaComponent::addComponentbut has no generic way to remove components added via that method. Therefore, if downstream code wants to useComponentas a composite type (presumably intended, given the API design), that code can only use it to build trees, but can't easily edit or manipulate a built one.Concretely, OpenSim Creator has been patching opensim-core downstream with this diff so that it can provide some basic support for deleting things from an OpenSim model (link).
The
extractComponentvariant allows the caller to take ownership of an object in aComponent'sComponentSet. This is useful for (e.g.) moving a subcomponent from one owner to another one. OpenSim Creator uses it to support moving markers from bodies to the model's markerset at runtime (link) but it could be used, in principle, to move any subcomponent (i.e. stuff in the genericComponentSet) from any owner to any other one.Brief summary of changes
Component::removeComponentandComponent::extractComponentProperty<T>::extractComponentto thePropertyAPI, which is required because the<components>(ComponentSet) field of aComponentis stored as aProperty.Property<T>::removeComponentto internally useextractComponent: it has the same side-effect (removal of the component) but assumes the caller wants to immediately drop the componentProperty<T>::removeComponentVirtualand replaces it withProperty<T>::extractComponentVirtual. The side-effects are the same (component removal) but the extraction method also ensures that the caller can get access to the thing that was removed. This has a small penalty (of allocation, in the simple property case), but property removal is rare (only one usage in opensim-core) and will not be part of a simulation hot loop. It is unlikely that downstream code implements its ownProperty<T>specialization.Testing I've completed
Looking for feedback on...
CHANGELOG.md (choose one)
This change is