From 3c164d552d2c46967dffb01fc7ee6a7dddc08e7f Mon Sep 17 00:00:00 2001 From: Martin Heistermann Date: Tue, 6 Feb 2018 12:29:23 +0100 Subject: [PATCH] Implement proper copy constructor for ResourceManager to allow mesh copying. Fixes #5. --- src/OpenVolumeMesh/Core/BaseProperty.hh | 2 + src/OpenVolumeMesh/Core/PropertyDefines.hh | 14 ++++ src/OpenVolumeMesh/Core/PropertyDefinesT.cc | 85 +++++++++++++++++++++ src/OpenVolumeMesh/Core/ResourceManager.cc | 17 +++++ src/OpenVolumeMesh/Core/ResourceManager.hh | 3 +- src/OpenVolumeMesh/Core/ResourceManagerT.cc | 4 +- 6 files changed, 122 insertions(+), 3 deletions(-) diff --git a/src/OpenVolumeMesh/Core/BaseProperty.hh b/src/OpenVolumeMesh/Core/BaseProperty.hh index b454650..9899263 100644 --- a/src/OpenVolumeMesh/Core/BaseProperty.hh +++ b/src/OpenVolumeMesh/Core/BaseProperty.hh @@ -65,6 +65,8 @@ public: virtual const std::string& name() const = 0; + virtual BaseProperty* clone(ResourceManager &_resMan, OpenVolumeMeshHandle _handle) const = 0; + virtual void delete_element(size_t _idx) = 0; virtual void swap_elements(size_t _idx0, size_t _idx1) = 0; diff --git a/src/OpenVolumeMesh/Core/PropertyDefines.hh b/src/OpenVolumeMesh/Core/PropertyDefines.hh index 851936f..485a70b 100644 --- a/src/OpenVolumeMesh/Core/PropertyDefines.hh +++ b/src/OpenVolumeMesh/Core/PropertyDefines.hh @@ -79,7 +79,9 @@ template class VertexPropertyT : public PropertyPtr, VertexPropHandle> { public: VertexPropertyT(const std::string& _name, ResourceManager& _resMan, VertexPropHandle _handle, const T _def = T()); + VertexPropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager& _resMan, VertexPropHandle _handle); virtual ~VertexPropertyT() {} + virtual BaseProperty* clone(ResourceManager &_resMan, OpenVolumeMeshHandle _handle) const; virtual void serialize(std::ostream& _ostr) const; virtual void deserialize(std::istream& _istr); virtual const std::string entityType() const { return "VProp"; } @@ -89,7 +91,9 @@ template class EdgePropertyT : public PropertyPtr, EdgePropHandle> { public: EdgePropertyT(const std::string& _name, ResourceManager& _resMan, EdgePropHandle _handle, const T _def = T()); + EdgePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager& _resMan, EdgePropHandle _handle); virtual ~EdgePropertyT() {} + virtual BaseProperty* clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const; virtual void serialize(std::ostream& _ostr) const; virtual void deserialize(std::istream& _istr); virtual const std::string entityType() const { return "EProp"; } @@ -99,7 +103,9 @@ template class HalfEdgePropertyT : public PropertyPtr, HalfEdgePropHandle> { public: HalfEdgePropertyT(const std::string& _name, ResourceManager& _resMan, HalfEdgePropHandle _handle, const T _def = T()); + HalfEdgePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager& _resMan, HalfEdgePropHandle _handle); virtual ~HalfEdgePropertyT() {} + virtual BaseProperty* clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const; virtual void serialize(std::ostream& _ostr) const; virtual void deserialize(std::istream& _istr); virtual const std::string entityType() const { return "HEProp"; } @@ -109,7 +115,9 @@ template class FacePropertyT : public PropertyPtr, FacePropHandle> { public: FacePropertyT(const std::string& _name, ResourceManager& _resMan, FacePropHandle _handle, const T _def = T()); + FacePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager& _resMan, FacePropHandle _handle); virtual ~FacePropertyT() {} + virtual BaseProperty* clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const; virtual void serialize(std::ostream& _ostr) const; virtual void deserialize(std::istream& _istr); virtual const std::string entityType() const { return "FProp"; } @@ -119,7 +127,9 @@ template class HalfFacePropertyT : public PropertyPtr, HalfFacePropHandle> { public: HalfFacePropertyT(const std::string& _name, ResourceManager& _resMan, HalfFacePropHandle _handle, const T _def = T()); + HalfFacePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager& _resMan, HalfFacePropHandle _handle); virtual ~HalfFacePropertyT() {} + virtual BaseProperty* clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const; virtual void serialize(std::ostream& _ostr) const; virtual void deserialize(std::istream& _istr); virtual const std::string entityType() const { return "HFProp"; } @@ -129,7 +139,9 @@ template class CellPropertyT : public PropertyPtr, CellPropHandle> { public: CellPropertyT(const std::string& _name, ResourceManager& _resMan, CellPropHandle _handle, const T _def = T()); + CellPropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager& _resMan, CellPropHandle _handle); virtual ~CellPropertyT() {} + virtual BaseProperty* clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const; virtual void serialize(std::ostream& _ostr) const; virtual void deserialize(std::istream& _istr); virtual const std::string entityType() const { return "CProp"; } @@ -139,7 +151,9 @@ template class MeshPropertyT : public PropertyPtr, MeshPropHandle> { public: MeshPropertyT(const std::string& _name, ResourceManager& _resMan, MeshPropHandle _handle, const T _def = T()); + MeshPropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager& _resMan, MeshPropHandle _handle); virtual ~MeshPropertyT() {} + virtual BaseProperty* clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const; virtual void serialize(std::ostream& _ostr) const; virtual void deserialize(std::istream& _istr); virtual const std::string entityType() const { return "MProp"; } diff --git a/src/OpenVolumeMesh/Core/PropertyDefinesT.cc b/src/OpenVolumeMesh/Core/PropertyDefinesT.cc index a36a806..3f93c2d 100644 --- a/src/OpenVolumeMesh/Core/PropertyDefinesT.cc +++ b/src/OpenVolumeMesh/Core/PropertyDefinesT.cc @@ -56,6 +56,19 @@ VertexPropertyT::VertexPropertyT(const std::string& _name, ResourceManager& _ } +template +VertexPropertyT::VertexPropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager &_resMan, VertexPropHandle _handle) : + PropertyPtr, VertexPropHandle>(_prop, _resMan, _handle) +{ +} + +template +BaseProperty *VertexPropertyT::clone(ResourceManager &_resMan, OpenVolumeMeshHandle _handle) const +{ + auto prop_clone = ptr::shared_ptr>::get()->clone(); + return new VertexPropertyT(prop_clone, _resMan, VertexPropHandle(_handle)); +} + template void VertexPropertyT::serialize(std::ostream& _ostr) const { PropertyPtr, VertexPropHandle>::get()->serialize(_ostr); @@ -71,6 +84,18 @@ EdgePropertyT::EdgePropertyT(const std::string& _name, ResourceManager& _resM PropertyPtr, EdgePropHandle>(new OpenVolumeMeshPropertyT(_name, _def), _resMan, _handle) { } +template +EdgePropertyT::EdgePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager &_resMan, EdgePropHandle _handle) : + PropertyPtr, EdgePropHandle>(_prop, _resMan, _handle) +{ +} + +template +BaseProperty *EdgePropertyT::clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const +{ + auto prop_clone = ptr::shared_ptr>::get()->clone(); + return new EdgePropertyT(prop_clone, _resMan, EdgePropHandle(_handle)); +} template void EdgePropertyT::serialize(std::ostream& _ostr) const { @@ -87,6 +112,18 @@ HalfEdgePropertyT::HalfEdgePropertyT(const std::string& _name, ResourceManage PropertyPtr, HalfEdgePropHandle>(new OpenVolumeMeshPropertyT(_name, _def), _resMan, _handle) { } +template +HalfEdgePropertyT::HalfEdgePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager &_resMan, HalfEdgePropHandle _handle) : + PropertyPtr, HalfEdgePropHandle>(_prop, _resMan, _handle) +{ +} + +template +BaseProperty *HalfEdgePropertyT::clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const +{ + auto prop_clone = ptr::shared_ptr>::get()->clone(); + return new HalfEdgePropertyT(prop_clone, _resMan, HalfEdgePropHandle(_handle)); +} template void HalfEdgePropertyT::serialize(std::ostream& _ostr) const { @@ -103,6 +140,18 @@ FacePropertyT::FacePropertyT(const std::string& _name, ResourceManager& _resM PropertyPtr, FacePropHandle>(new OpenVolumeMeshPropertyT(_name, _def), _resMan, _handle) { } +template +FacePropertyT::FacePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager &_resMan, FacePropHandle _handle) : + PropertyPtr, FacePropHandle>(_prop, _resMan, _handle) +{ +} + +template +BaseProperty *FacePropertyT::clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const +{ + auto prop_clone = ptr::shared_ptr>::get()->clone(); + return new FacePropertyT(prop_clone, _resMan, FacePropHandle(_handle)); +} template void FacePropertyT::serialize(std::ostream& _ostr) const { @@ -119,6 +168,18 @@ HalfFacePropertyT::HalfFacePropertyT(const std::string& _name, ResourceManage PropertyPtr, HalfFacePropHandle>(new OpenVolumeMeshPropertyT(_name, _def), _resMan, _handle) { } +template +HalfFacePropertyT::HalfFacePropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager &_resMan, HalfFacePropHandle _handle) : + PropertyPtr, HalfFacePropHandle>(_prop, _resMan, _handle) +{ +} + +template +BaseProperty *HalfFacePropertyT::clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const +{ + auto prop_clone = ptr::shared_ptr>::get()->clone(); + return new HalfFacePropertyT(prop_clone, _resMan, HalfFacePropHandle(_handle)); +} template void HalfFacePropertyT::serialize(std::ostream& _ostr) const { @@ -135,6 +196,18 @@ CellPropertyT::CellPropertyT(const std::string& _name, ResourceManager& _resM PropertyPtr, CellPropHandle>(new OpenVolumeMeshPropertyT(_name, _def), _resMan, _handle) { } +template +CellPropertyT::CellPropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager &_resMan, CellPropHandle _handle) : + PropertyPtr, CellPropHandle>(_prop, _resMan, _handle) +{ +} + +template +BaseProperty *CellPropertyT::clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const +{ + auto prop_clone = ptr::shared_ptr>::get()->clone(); + return new CellPropertyT(prop_clone, _resMan, CellPropHandle(_handle)); +} template void CellPropertyT::serialize(std::ostream& _ostr) const { @@ -151,6 +224,18 @@ MeshPropertyT::MeshPropertyT(const std::string& _name, ResourceManager& _resM PropertyPtr, MeshPropHandle>(new OpenVolumeMeshPropertyT(_name, _def), _resMan, _handle) { } +template +MeshPropertyT::MeshPropertyT(OpenVolumeMeshPropertyT *_prop, ResourceManager &_resMan, MeshPropHandle _handle) : + PropertyPtr, MeshPropHandle>(_prop, _resMan, _handle) +{ +} + +template +BaseProperty *MeshPropertyT::clone(ResourceManager &_resMan, const OpenVolumeMeshHandle _handle) const +{ + auto prop_clone = ptr::shared_ptr>::get()->clone(); + return new MeshPropertyT(prop_clone, _resMan, MeshPropHandle(_handle)); +} template void MeshPropertyT::serialize(std::ostream& _ostr) const { diff --git a/src/OpenVolumeMesh/Core/ResourceManager.cc b/src/OpenVolumeMesh/Core/ResourceManager.cc index c3a8624..126ea5a 100644 --- a/src/OpenVolumeMesh/Core/ResourceManager.cc +++ b/src/OpenVolumeMesh/Core/ResourceManager.cc @@ -47,6 +47,22 @@ namespace OpenVolumeMesh { ResourceManager::ResourceManager() { } +ResourceManager::ResourceManager(const ResourceManager &other) { + auto cloneProps = [this](const Properties &src, Properties &dest) { + dest.reserve(src.size()); + for (BaseProperty *bp: src) { + dest.push_back(bp->clone(*this, bp->handle())); + } + }; + cloneProps(other.vertex_props_, vertex_props_); + cloneProps(other.edge_props_, edge_props_); + cloneProps(other.halfedge_props_, halfedge_props_); + cloneProps(other.face_props_, face_props_); + cloneProps(other.halfface_props_, halfface_props_); + cloneProps(other.cell_props_, cell_props_); + cloneProps(other.mesh_props_, mesh_props_); +} + ResourceManager::~ResourceManager() { // Delete persistent props @@ -230,4 +246,5 @@ void ResourceManager::delete_multiple_cell_props(const std::vector& _tags) } } + } // Namespace OpenVolumeMesh diff --git a/src/OpenVolumeMesh/Core/ResourceManager.hh b/src/OpenVolumeMesh/Core/ResourceManager.hh index 84b202e..08aaea3 100644 --- a/src/OpenVolumeMesh/Core/ResourceManager.hh +++ b/src/OpenVolumeMesh/Core/ResourceManager.hh @@ -76,6 +76,7 @@ class PropertyPtr; class ResourceManager { public: ResourceManager(); + ResourceManager(const ResourceManager &other); virtual ~ResourceManager(); template friend class PropertyPtr; @@ -338,7 +339,7 @@ private: void set_persistentT(PropT& _prop, bool _flag); template - void clearVec(StdVecT& _vec); + void clearVec(StdVecT& _vec, bool _keep_persistent = true); Properties vertex_props_; diff --git a/src/OpenVolumeMesh/Core/ResourceManagerT.cc b/src/OpenVolumeMesh/Core/ResourceManagerT.cc index bf7831c..12ed96c 100644 --- a/src/OpenVolumeMesh/Core/ResourceManagerT.cc +++ b/src/OpenVolumeMesh/Core/ResourceManagerT.cc @@ -198,12 +198,12 @@ void ResourceManager::entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle& } template -void ResourceManager::clearVec(StdVecT& _vec) { +void ResourceManager::clearVec(StdVecT& _vec, bool _keep_persistent) { StdVecT newVec; for(typename StdVecT::iterator it = _vec.begin(); it != _vec.end(); ++it) { - if(!(*it)->persistent()) { + if(_keep_persistent && !(*it)->persistent()) { #ifndef NDEBUG std::cerr << "Keeping property \"" << (*it)->name() << "\" since it is still in use!" << std::endl; -- GitLab