diff --git a/src/OpenVolumeMesh/Core/BaseProperty.hh b/src/OpenVolumeMesh/Core/BaseProperty.hh index 129eda9ff86365b5a877b7e9d83a009e66b2cac5..79190687c0b4ea21bd2d1e6f2cbac17d7a71d876 100644 --- a/src/OpenVolumeMesh/Core/BaseProperty.hh +++ b/src/OpenVolumeMesh/Core/BaseProperty.hh @@ -55,16 +55,10 @@ class BaseProperty { public: friend class ResourceManager; - BaseProperty(ResourceManager& _resMan) : resMan_(_resMan) {} - - BaseProperty(const BaseProperty& _cpy) : resMan_(_cpy.resMan_) {} + BaseProperty(ResourceManager& _resMan) : resMan_(_resMan), lock_(false) {} virtual ~BaseProperty() {} - virtual BaseProperty& operator= (const BaseProperty& _rhs); - - virtual BaseProperty& operator= (BaseProperty& _rhs); - virtual const std::string& name() const = 0; virtual void delete_element(size_t _idx) = 0; @@ -73,16 +67,24 @@ public: virtual std::istream& deserialize(std::istream& _istr) = 0; + virtual OpenVolumeMeshHandle handle() const = 0; + + virtual bool persistent() const = 0; + protected: virtual void resize(unsigned int /*_size*/) = 0; - virtual const void* ptr() const = 0; - virtual void set_handle(const OpenVolumeMeshHandle& /*_handle*/) = 0; - virtual OpenVolumeMeshHandle handle() const = 0; + void lock() { lock_ = true; } + + void unlock() { lock_ = false; } + + bool locked() const { return lock_; } ResourceManager& resMan_; + + bool lock_; }; } // Namespace OpenVolumeMesh diff --git a/src/OpenVolumeMesh/Core/BaseProperty.cc b/src/OpenVolumeMesh/Core/MemoryInclude.hh similarity index 72% rename from src/OpenVolumeMesh/Core/BaseProperty.cc rename to src/OpenVolumeMesh/Core/MemoryInclude.hh index 6ce8b8c9220664f623844ccb904be3b889666428..8f077267249b4331cad1ba48387ff0832aaa0a37 100644 --- a/src/OpenVolumeMesh/Core/BaseProperty.cc +++ b/src/OpenVolumeMesh/Core/MemoryInclude.hh @@ -34,26 +34,41 @@ /*===========================================================================*\ * * - * $Revision$ * - * $Date$ * - * $LastChangedBy$ * + * $Revision: 36 $ * + * $Date: 2012-01-10 18:00:06 +0100 (Di, 10 Jan 2012) $ * + * $LastChangedBy: kremer $ * * * \*===========================================================================*/ -#include "BaseProperty.hh" +#ifndef MEMORYINCLUDE_HH_ +#define MEMORYINCLUDE_HH_ -#include "ResourceManager.hh" +/** This set of defines maps the pointer namespaces to the namespace ptr depending on + * the current architecture and compilers. + */ +#if (__cplusplus >= 201103L) + // C++11: + #include + namespace ptr = std; + #define ACG_UNIQUE_POINTER_SUPPORTED 1 +#elif defined(__GXX_EXPERIMENTAL_CXX0X__) + // C++11 via -std=c++0x on gcc: + #include + namespace ptr = std; + #define ACG_UNIQUE_POINTER_SUPPORTED 1 +#else + // C++98 and TR1: + #if (_MSC_VER >= 1600) + // VStudio 2010 supports some C++11 features + #include + namespace ptr = std; + #define ACG_UNIQUE_POINTER_SUPPORTED 1 + #else + // hope for TR1 equivalents + #include + namespace ptr = std::tr1; + #define ACG_UNIQUE_POINTER_SUPPORTED 0 + #endif +#endif -namespace OpenVolumeMesh { - -BaseProperty& BaseProperty::operator= (const BaseProperty& _rhs) { - resMan_ = _rhs.resMan_; - return *this; -} - -BaseProperty& BaseProperty::operator= (BaseProperty& _rhs) { - resMan_ = _rhs.resMan_; - return *this; -} - -} // Namespace OpenVolumeMesh +#endif /* MEMORYINCLUDE_HH_ */ diff --git a/src/OpenVolumeMesh/Core/PropertyDefinesT.cc b/src/OpenVolumeMesh/Core/PropertyDefinesT.cc index 67bf4e3238030c29527c9056b15b98d09f077b28..7b29221d21c879b82d0e505dc8eecd9139cb6252 100644 --- a/src/OpenVolumeMesh/Core/PropertyDefinesT.cc +++ b/src/OpenVolumeMesh/Core/PropertyDefinesT.cc @@ -57,13 +57,13 @@ VertexPropertyT::VertexPropertyT(const std::string& _name, ResourceManager& _ template std::ostream& VertexPropertyT::serialize(std::ostream& _ostr) const { _ostr << "VProp" << std::endl; - _ostr << PropertyPtr, VertexPropHandle>::h_->ptr_->serialize(_ostr); + _ostr << PropertyPtr, VertexPropHandle>::get()->serialize(_ostr); return _ostr; } template std::istream& VertexPropertyT::deserialize(std::istream& _istr) { - PropertyPtr, VertexPropHandle>::h_->ptr_->deserialize(_istr); + PropertyPtr, VertexPropHandle>::get()->deserialize(_istr); return _istr; } @@ -76,13 +76,13 @@ EdgePropertyT::EdgePropertyT(const std::string& _name, ResourceManager& _resM template std::ostream& EdgePropertyT::serialize(std::ostream& _ostr) const { _ostr << "EProp" << std::endl; - _ostr << PropertyPtr, EdgePropHandle>::h_->ptr_->serialize(_ostr); + _ostr << PropertyPtr, EdgePropHandle>::get()->serialize(_ostr); return _ostr; } template std::istream& EdgePropertyT::deserialize(std::istream& _istr) { - PropertyPtr, EdgePropHandle>::h_->ptr_->deserialize(_istr); + PropertyPtr, EdgePropHandle>::get()->deserialize(_istr); return _istr; } @@ -95,13 +95,13 @@ HalfEdgePropertyT::HalfEdgePropertyT(const std::string& _name, ResourceManage template std::ostream& HalfEdgePropertyT::serialize(std::ostream& _ostr) const { _ostr << "HEProp" << std::endl; - _ostr << PropertyPtr, HalfEdgePropHandle>::h_->ptr_->serialize(_ostr); + _ostr << PropertyPtr, HalfEdgePropHandle>::get()->serialize(_ostr); return _ostr; } template std::istream& HalfEdgePropertyT::deserialize(std::istream& _istr) { - PropertyPtr, HalfEdgePropHandle>::h_->ptr_->deserialize(_istr); + PropertyPtr, HalfEdgePropHandle>::get()->deserialize(_istr); return _istr; } @@ -114,13 +114,13 @@ FacePropertyT::FacePropertyT(const std::string& _name, ResourceManager& _resM template std::ostream& FacePropertyT::serialize(std::ostream& _ostr) const { _ostr << "FProp" << std::endl; - _ostr << PropertyPtr, FacePropHandle>::h_->ptr_->serialize(_ostr); + _ostr << PropertyPtr, FacePropHandle>::get()->serialize(_ostr); return _ostr; } template std::istream& FacePropertyT::deserialize(std::istream& _istr) { - PropertyPtr, FacePropHandle>::h_->ptr_->deserialize(_istr); + PropertyPtr, FacePropHandle>::get()->deserialize(_istr); return _istr; } @@ -133,13 +133,13 @@ HalfFacePropertyT::HalfFacePropertyT(const std::string& _name, ResourceManage template std::ostream& HalfFacePropertyT::serialize(std::ostream& _ostr) const { _ostr << "HFProp" << std::endl; - _ostr << PropertyPtr, HalfFacePropHandle>::h_->ptr_->serialize(_ostr); + _ostr << PropertyPtr, HalfFacePropHandle>::get()->serialize(_ostr); return _ostr; } template std::istream& HalfFacePropertyT::deserialize(std::istream& _istr) { - PropertyPtr, HalfFacePropHandle>::h_->ptr_->deserialize(_istr); + PropertyPtr, HalfFacePropHandle>::get()->deserialize(_istr); return _istr; } @@ -152,13 +152,13 @@ CellPropertyT::CellPropertyT(const std::string& _name, ResourceManager& _resM template std::ostream& CellPropertyT::serialize(std::ostream& _ostr) const { _ostr << "CProp" << std::endl; - _ostr << PropertyPtr, CellPropHandle>::h_->ptr_->serialize(_ostr); + _ostr << PropertyPtr, CellPropHandle>::get()->serialize(_ostr); return _ostr; } template std::istream& CellPropertyT::deserialize(std::istream& _istr) { - PropertyPtr, CellPropHandle>::h_->ptr_->deserialize(_istr); + PropertyPtr, CellPropHandle>::get()->deserialize(_istr); return _istr; } @@ -171,13 +171,13 @@ MeshPropertyT::MeshPropertyT(const std::string& _name, ResourceManager& _resM template std::ostream& MeshPropertyT::serialize(std::ostream& _ostr) const { _ostr << "MProp" << std::endl; - _ostr << PropertyPtr, MeshPropHandle>::h_->ptr_->serialize(_ostr); + _ostr << PropertyPtr, MeshPropHandle>::get()->serialize(_ostr); return _ostr; } template std::istream& MeshPropertyT::deserialize(std::istream& _istr) { - PropertyPtr, MeshPropHandle>::h_->ptr_->deserialize(_istr); + PropertyPtr, MeshPropHandle>::get()->deserialize(_istr); return _istr; } diff --git a/src/OpenVolumeMesh/Core/PropertyPtr.hh b/src/OpenVolumeMesh/Core/PropertyPtr.hh index 001c01d17974319dc3c10efd199442a284d670f8..4c58041b109f0f8d9074387ee1175d58b8a9677e 100644 --- a/src/OpenVolumeMesh/Core/PropertyPtr.hh +++ b/src/OpenVolumeMesh/Core/PropertyPtr.hh @@ -50,6 +50,8 @@ #include "BaseProperty.hh" #include "PropertyHandles.hh" +#include "MemoryInclude.hh" + namespace OpenVolumeMesh { class ResourceManager; @@ -63,31 +65,7 @@ class ResourceManager; */ template -class PropertyPtr : public BaseProperty { -protected: - - /** - * \class Holder - * A helper class that encapsulates a pointer - * to an object and counts its number of references. - */ - class Holder { - public: - Holder(PropT* _ptr) : ptr_(_ptr), count_(1u), persistent_(false) {} - ~Holder() { - if(ptr_ != NULL) { - delete ptr_; - ptr_ = NULL; - } - } - - PropT* ptr_; - unsigned int count_; - bool persistent_; - }; - - Holder* h_; - +class PropertyPtr : public ptr::shared_ptr, public BaseProperty { public: friend class ResourceManager; @@ -96,56 +74,38 @@ public: typedef typename PropT::vector_type::const_iterator const_iterator; typedef typename PropT::vector_type::iterator iterator; + typedef typename PropT::reference reference; + typedef typename PropT::const_reference const_reference; /// Constructor PropertyPtr(PropT* _ptr, ResourceManager& _resMan, Handle _handle); - /// Copy Constructor - PropertyPtr(const PropertyPtr& _cpy); - /// Destructor virtual ~PropertyPtr(); - /// Assignment operator with const reference - PropertyPtr& operator= (const PropertyPtr& _rhs); - - /// Assignment operator with non-const reference - PropertyPtr& operator= (PropertyPtr& _rhs); - - const typename PropT::const_reference operator[](size_t _idx) const; - - typename PropT::reference operator[](size_t _idx); - virtual const std::string& name() const; - /// Access the encapsulated pointer - PropT* operator->(); - - /// Access the encapsulated dereferenced pointer - PropT& operator*(); - virtual void delete_element(size_t _idx); - unsigned int counter() const { return h_->count_; } + const_iterator begin() const { return ptr::shared_ptr::get()->begin(); } + iterator begin() { return ptr::shared_ptr::get()->begin(); } - const_iterator begin() const { return h_->ptr_->begin(); } + const_iterator end() const { return ptr::shared_ptr::get()->end(); } + iterator end() { return ptr::shared_ptr::get()->end(); } - iterator begin() { return h_->ptr_->begin(); } + reference operator[](size_t _idx) { return (*ptr::shared_ptr::get())[_idx]; } + const_reference operator[](size_t _idx) const { return (*ptr::shared_ptr::get())[_idx]; } - const_iterator end() const { return h_->ptr_->end(); } + virtual OpenVolumeMeshHandle handle() const; - iterator end() { return h_->ptr_->end(); } + virtual bool persistent() const { return ptr::shared_ptr::get()->persistent(); } protected: virtual void resize(unsigned int _size); - virtual const void* ptr() const { return h_->ptr_; } - virtual void set_handle(const OpenVolumeMeshHandle& _handle); - virtual OpenVolumeMeshHandle handle() const; - private: Handle handle_; diff --git a/src/OpenVolumeMesh/Core/PropertyPtrT.cc b/src/OpenVolumeMesh/Core/PropertyPtrT.cc index 7a5d271e0ba2ee1754226609de1c3dcac06b4a23..ba47ea5909a407a3d3d0092641146030ecd064ae 100644 --- a/src/OpenVolumeMesh/Core/PropertyPtrT.cc +++ b/src/OpenVolumeMesh/Core/PropertyPtrT.cc @@ -49,85 +49,37 @@ namespace OpenVolumeMesh { template -PropertyPtr::PropertyPtr(PropT* _ptr, ResourceManager& _resMan, Handle _handle) : - BaseProperty(_resMan), h_(new Holder(_ptr)), handle_(_handle) { -} - -template -PropertyPtr::PropertyPtr(const PropertyPtr& _cpy) : - BaseProperty(_cpy), h_(_cpy.h_), handle_(_cpy.handle_) { - ++h_->count_; +PropertyPtr::PropertyPtr(PropT* _ptr, ResourceManager& _resMan, HandleT _handle) : + ptr::shared_ptr(_ptr), BaseProperty(_resMan), handle_(_handle) { } template PropertyPtr::~PropertyPtr() { - if(--h_->count_ == 0) { - resMan_.released_property(handle_); - delete h_; - } -} - -template -PropertyPtr& -PropertyPtr::operator= (const PropertyPtr& _rhs) { - - if(--h_->count_ == 0) delete h_; - - h_ = _rhs.h_; - ++h_->count_; - - return *this; -} - -template -PropertyPtr& -PropertyPtr::operator= (PropertyPtr& _rhs) { - - if(--h_->count_ == 0) delete h_; - h_ = _rhs.h_; - ++h_->count_; - - return *this; -} - -template -const typename PropT::const_reference -PropertyPtr::operator[](size_t _idx) const { - assert(h_->ptr_->n_elements() > _idx); - return (*h_->ptr_)[_idx]; -} - -template -typename PropT::reference -PropertyPtr::operator[](size_t _idx) { - assert(h_->ptr_->n_elements() > _idx); - return (*h_->ptr_)[_idx]; + /* + * If use count is 2 and prop is not set persistent, + * remove it, since the resource manager is the + * only one who stores the property. + */ + if(!locked() && !persistent() && ptr::shared_ptr::use_count() == 2) { + resMan_.release_property(handle_); + unlock(); + } } template void PropertyPtr::resize(unsigned int _size) { - h_->ptr_->resize(_size); + ptr::shared_ptr::get()->resize(_size); } template const std::string& PropertyPtr::name() const { - return h_->ptr_->name(); -} - -template -PropT* PropertyPtr::operator->() { - return h_->ptr_; -} - -template -PropT& PropertyPtr::operator*() { - return *h_->ptr_; + return ptr::shared_ptr::get()->name(); } template void PropertyPtr::delete_element(size_t _idx) { - h_->ptr_->delete_element(_idx); + ptr::shared_ptr::get()->delete_element(_idx); } template diff --git a/src/OpenVolumeMesh/Core/ResourceManager.cc b/src/OpenVolumeMesh/Core/ResourceManager.cc index cca64c97f637f204784a07d352b89432b95126e2..748543f3a4ecf5c013232415a8668ee3b276e22b 100644 --- a/src/OpenVolumeMesh/Core/ResourceManager.cc +++ b/src/OpenVolumeMesh/Core/ResourceManager.cc @@ -50,201 +50,95 @@ ResourceManager::ResourceManager() { ResourceManager::~ResourceManager() { - // Delete all persistent properties - for(std::vector::iterator it = persistent_vprops_.begin(); - it != persistent_vprops_.end(); ++it) { - delete *it; - } - persistent_vprops_.clear(); - for(std::vector::iterator it = persistent_eprops_.begin(); - it != persistent_eprops_.end(); ++it) { - delete *it; - } - persistent_eprops_.clear(); - for(std::vector::iterator it = persistent_heprops_.begin(); - it != persistent_heprops_.end(); ++it) { - delete *it; - } - persistent_heprops_.clear(); - for(std::vector::iterator it = persistent_fprops_.begin(); - it != persistent_fprops_.end(); ++it) { - delete *it; - } - persistent_fprops_.clear(); - for(std::vector::iterator it = persistent_hfprops_.begin(); - it != persistent_hfprops_.end(); ++it) { - delete *it; - } - persistent_hfprops_.clear(); - for(std::vector::iterator it = persistent_cprops_.begin(); - it != persistent_cprops_.end(); ++it) { - delete *it; - } - persistent_cprops_.clear(); - for(std::vector::iterator it = persistent_mprops_.begin(); - it != persistent_mprops_.end(); ++it) { - delete *it; - } - persistent_mprops_.clear(); + // Delete persistent props + clearVec(vertex_props_); + clearVec(edge_props_); + clearVec(halfedge_props_); + clearVec(face_props_); + clearVec(halfface_props_); + clearVec(cell_props_); + clearVec(mesh_props_); } void ResourceManager::resize_vprops(unsigned int _nv) { - for(std::vector::iterator it = vertex_props_.begin(); - it != vertex_props_.end(); ++it) { - (*it)->resize(_nv); - } + resize_props(vertex_props_, _nv); } void ResourceManager::resize_eprops(unsigned int _ne) { - for(std::vector::iterator it = edge_props_.begin(); - it != edge_props_.end(); ++it) { - (*it)->resize(_ne); - } - for(std::vector::iterator it = halfedge_props_.begin(); - it != halfedge_props_.end(); ++it) { - (*it)->resize(2u*_ne); - } + resize_props(edge_props_, _ne); + resize_props(halfedge_props_, _ne*2u); } void ResourceManager::resize_fprops(unsigned int _nf) { - for(std::vector::iterator it = face_props_.begin(); - it != face_props_.end(); ++it) { - (*it)->resize(_nf); - } - for(std::vector::iterator it = halfface_props_.begin(); - it != halfface_props_.end(); ++it) { - (*it)->resize(2u*_nf); - } + resize_props(face_props_, _nf); + resize_props(halfface_props_, _nf*2u); } void ResourceManager::resize_cprops(unsigned int _nc) { - for(std::vector::iterator it = cell_props_.begin(); - it != cell_props_.end(); ++it) { - (*it)->resize(_nc); - } + resize_props(cell_props_, _nc); } void ResourceManager::vertex_deleted(const VertexHandle& _h) { - for(std::vector::iterator it = vertex_props_.begin(); - it != vertex_props_.end(); ++it) { - (*it)->delete_element(_h.idx()); - } + entity_deleted(vertex_props_, _h); } void ResourceManager::edge_deleted(const EdgeHandle& _h) { - for(std::vector::iterator it = edge_props_.begin(); - it != edge_props_.end(); ++it) { - (*it)->delete_element(_h.idx()); - } - for(std::vector::iterator it = halfedge_props_.begin(); - it != halfedge_props_.end(); ++it) { - (*it)->delete_element(_h.idx()*2 + 1); - (*it)->delete_element(_h.idx()*2); - } + entity_deleted(edge_props_, _h); + entity_deleted(halfedge_props_, OpenVolumeMeshHandle(_h.idx()*2 + 1)); + entity_deleted(halfedge_props_, OpenVolumeMeshHandle(_h.idx()*2)); } void ResourceManager::face_deleted(const FaceHandle& _h) { - for(std::vector::iterator it = face_props_.begin(); - it != face_props_.end(); ++it) { - (*it)->delete_element(_h.idx()); - } - for(std::vector::iterator it = halfface_props_.begin(); - it != halfface_props_.end(); ++it) { - (*it)->delete_element(_h.idx()*2 + 1); - (*it)->delete_element(_h.idx()*2); - } + entity_deleted(face_props_, _h); + entity_deleted(halfface_props_, OpenVolumeMeshHandle(_h.idx()*2 + 1)); + entity_deleted(halfface_props_, OpenVolumeMeshHandle(_h.idx()*2)); } void ResourceManager::cell_deleted(const CellHandle& _h) { - for(std::vector::iterator it = cell_props_.begin(); - it != cell_props_.end(); ++it) { - (*it)->delete_element(_h.idx()); - } + entity_deleted(cell_props_, _h); } -void ResourceManager::released_property(VertexPropHandle _handle) { +void ResourceManager::release_property(VertexPropHandle _handle) { - delete (*(vertex_props_.begin() + _handle.idx())); - std::vector::iterator it = vertex_props_.erase(vertex_props_.begin() + _handle.idx()); - VertexPropHandle decrHandle(_handle.idx()); - for(; it < vertex_props_.end(); ++it) { - (*it)->set_handle(decrHandle); - decrHandle.idx(decrHandle.idx()+1); - } + remove_property(vertex_props_, _handle.idx()); } -void ResourceManager::released_property(EdgePropHandle _handle) { +void ResourceManager::release_property(EdgePropHandle _handle) { - delete (*(edge_props_.begin() + _handle.idx())); - std::vector::iterator it = edge_props_.erase(edge_props_.begin() + _handle.idx()); - EdgePropHandle decrHandle(_handle.idx()); - for(; it < edge_props_.end(); ++it) { - (*it)->set_handle(decrHandle); - decrHandle.idx(decrHandle.idx()+1); - } + remove_property(edge_props_, _handle.idx()); } -void ResourceManager::released_property(HalfEdgePropHandle _handle) { +void ResourceManager::release_property(HalfEdgePropHandle _handle) { - delete (*(halfedge_props_.begin() + _handle.idx())); - std::vector::iterator it = halfedge_props_.erase(halfedge_props_.begin() + _handle.idx()); - HalfEdgePropHandle decrHandle(_handle.idx()); - for(; it < halfedge_props_.end(); ++it) { - (*it)->set_handle(decrHandle); - decrHandle.idx(decrHandle.idx()+1); - } + remove_property(halfedge_props_, _handle.idx()); } -void ResourceManager::released_property(FacePropHandle _handle) { +void ResourceManager::release_property(FacePropHandle _handle) { - delete (*(face_props_.begin() + _handle.idx())); - std::vector::iterator it = face_props_.erase(face_props_.begin() + _handle.idx()); - FacePropHandle decrHandle(_handle.idx()); - for(; it < face_props_.end(); ++it) { - (*it)->set_handle(decrHandle); - decrHandle.idx(decrHandle.idx()+1); - } + remove_property(face_props_, _handle.idx()); } -void ResourceManager::released_property(HalfFacePropHandle _handle) { +void ResourceManager::release_property(HalfFacePropHandle _handle) { - delete (*(halfface_props_.begin() + _handle.idx())); - std::vector::iterator it = halfface_props_.erase(halfface_props_.begin() + _handle.idx()); - HalfFacePropHandle decrHandle(_handle.idx()); - for(; it < halfface_props_.end(); ++it) { - (*it)->set_handle(decrHandle); - decrHandle.idx(decrHandle.idx()+1); - } + remove_property(halfface_props_, _handle.idx()); } -void ResourceManager::released_property(CellPropHandle _handle) { +void ResourceManager::release_property(CellPropHandle _handle) { - delete (*(cell_props_.begin() + _handle.idx())); - std::vector::iterator it = cell_props_.erase(cell_props_.begin() + _handle.idx()); - CellPropHandle decrHandle(_handle.idx()); - for(; it < cell_props_.end(); ++it) { - (*it)->set_handle(decrHandle); - decrHandle.idx(decrHandle.idx()+1); - } + remove_property(cell_props_, _handle.idx()); } -void ResourceManager::released_property(MeshPropHandle _handle) { +void ResourceManager::release_property(MeshPropHandle _handle) { - delete (*(mesh_props_.begin() + _handle.idx())); - std::vector::iterator it = mesh_props_.erase(mesh_props_.begin() + _handle.idx()); - MeshPropHandle decrHandle(_handle.idx()); - for(; it < mesh_props_.end(); ++it) { - (*it)->set_handle(decrHandle); - decrHandle.idx(decrHandle.idx()+1); - } + remove_property(mesh_props_, _handle.idx()); } } // Namespace OpenVolumeMesh diff --git a/src/OpenVolumeMesh/Core/ResourceManager.hh b/src/OpenVolumeMesh/Core/ResourceManager.hh index 507fed49d3f5254fb6fdd9ecd0053b9b3770ac8c..16d07de5582f90cd24772b241e706fbdb5d3124b 100644 --- a/src/OpenVolumeMesh/Core/ResourceManager.hh +++ b/src/OpenVolumeMesh/Core/ResourceManager.hh @@ -120,19 +120,19 @@ public: template MeshPropertyT request_mesh_property(const std::string& _name = std::string()); - void released_property(VertexPropHandle _handle); + void release_property(VertexPropHandle _handle); - void released_property(EdgePropHandle _handle); + void release_property(EdgePropHandle _handle); - void released_property(HalfEdgePropHandle _handle); + void release_property(HalfEdgePropHandle _handle); - void released_property(FacePropHandle _handle); + void release_property(FacePropHandle _handle); - void released_property(HalfFacePropHandle _handle); + void release_property(HalfFacePropHandle _handle); - void released_property(CellPropHandle _handle); + void release_property(CellPropHandle _handle); - void released_property(MeshPropHandle _handle); + void release_property(MeshPropHandle _handle); unsigned int n_vertex_props() const { return vertex_props_.size(); } @@ -164,6 +164,24 @@ public: private: + template + void resize_props(StdVecT& _vec, unsigned int _n); + + template + void entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle& _h); + + template + void remove_property(StdVecT& _vec, size_t _idx); + + template + PropT request_property(StdVecT& _vec, const std::string& _name, size_t _size); + + template + void set_persistentT(PropT& _prop, bool _flag); + + template + void clearVec(StdVecT& _vec); + std::vector vertex_props_; std::vector edge_props_; @@ -177,20 +195,6 @@ private: std::vector cell_props_; std::vector mesh_props_; - - std::vector persistent_vprops_; - - std::vector persistent_eprops_; - - std::vector persistent_heprops_; - - std::vector persistent_fprops_; - - std::vector persistent_hfprops_; - - std::vector persistent_cprops_; - - std::vector persistent_mprops_; }; } diff --git a/src/OpenVolumeMesh/Core/ResourceManagerT.cc b/src/OpenVolumeMesh/Core/ResourceManagerT.cc index c2f68aac11092356edbb2ad019514861b01a74bc..05387f5120bc14013d07f375c19f4a28e81a8409 100644 --- a/src/OpenVolumeMesh/Core/ResourceManagerT.cc +++ b/src/OpenVolumeMesh/Core/ResourceManagerT.cc @@ -53,202 +53,66 @@ namespace OpenVolumeMesh { template VertexPropertyT ResourceManager::request_vertex_property(const std::string& _name) { - if(!_name.empty()) { - for(std::vector::iterator it = persistent_vprops_.begin(); - it != persistent_vprops_.end(); ++it) { - if((*it)->name() == _name) { - VertexPropertyT* prop = dynamic_cast* >(*it); - return *prop; - } - } - } - - VertexPropHandle handle = vertex_props_.size(); - - VertexPropertyT* prop = new VertexPropertyT(_name, *this, handle); - (*prop)->resize(n_vertices()); - - // Store property pointer - vertex_props_.push_back(prop); - - // Decrease reference counter by one - // since the property is going to be copied - // on return - --(*prop).h_->count_; - - return *prop; + return request_property,VertexPropertyT,VertexPropHandle>(vertex_props_, _name, n_vertices()); } template EdgePropertyT ResourceManager::request_edge_property(const std::string& _name) { - if(!_name.empty()) { - for(std::vector::iterator it = persistent_eprops_.begin(); - it != persistent_eprops_.end(); ++it) { - if((*it)->name() == _name) { - EdgePropertyT* prop = dynamic_cast* >(*it); - return *prop; - } - } - } - - EdgePropHandle handle = edge_props_.size(); - - EdgePropertyT* prop = new EdgePropertyT(_name, *this, handle); - (*prop)->resize(n_edges()); - - // Store property pointer - edge_props_.push_back(prop); - - // Decrease reference counter by one - // since the property is going to be copied - // on return - --(*prop).h_->count_; - - return *prop; + return request_property,EdgePropertyT,EdgePropHandle>(edge_props_, _name, n_edges()); } template HalfEdgePropertyT ResourceManager::request_halfedge_property(const std::string& _name) { - if(!_name.empty()) { - for(std::vector::iterator it = persistent_heprops_.begin(); - it != persistent_heprops_.end(); ++it) { - if((*it)->name() == _name) { - HalfEdgePropertyT* prop = dynamic_cast* >(*it); - return *prop; - } - } - } - - HalfEdgePropHandle handle = halfedge_props_.size(); - - HalfEdgePropertyT* prop = new HalfEdgePropertyT(_name, *this, handle); - (*prop)->resize(n_halfedges()); - - // Store property pointer - halfedge_props_.push_back(prop); - - // Decrease reference counter by one - // since the property is going to be copied - // on return - --(*prop).h_->count_; - - return *prop; + return request_property,HalfEdgePropertyT,HalfEdgePropHandle>(halfedge_props_, _name, n_edges()*2u); } template FacePropertyT ResourceManager::request_face_property(const std::string& _name) { - if(!_name.empty()) { - for(std::vector::iterator it = persistent_fprops_.begin(); - it != persistent_fprops_.end(); ++it) { - if((*it)->name() == _name) { - FacePropertyT* prop = dynamic_cast* >(*it); - return *prop; - } - } - } - - FacePropHandle handle = face_props_.size(); - - FacePropertyT* prop = new FacePropertyT(_name, *this, handle); - (*prop)->resize(n_faces()); - - // Store property pointer - face_props_.push_back(prop); - - // Decrease reference counter by one - // since the property is going to be copied - // on return - --(*prop).h_->count_; - - return *prop; + return request_property,FacePropertyT,FacePropHandle>(face_props_, _name, n_faces()); } template HalfFacePropertyT ResourceManager::request_halfface_property(const std::string& _name) { - if(!_name.empty()) { - for(std::vector::iterator it = persistent_hfprops_.begin(); - it != persistent_hfprops_.end(); ++it) { - if((*it)->name() == _name) { - HalfFacePropertyT* prop = dynamic_cast* >(*it); - return *prop; - } - } - } - - HalfFacePropHandle handle = halfface_props_.size(); - - HalfFacePropertyT* prop = new HalfFacePropertyT(_name, *this, handle); - (*prop)->resize(n_halffaces()); - - // Store property pointer - halfface_props_.push_back(prop); - - // Decrease reference counter by one - // since the property is going to be copied - // on return - --(*prop).h_->count_; - - return *prop; + return request_property,HalfFacePropertyT,HalfFacePropHandle>(halfface_props_, _name, n_faces()*2u); } template CellPropertyT ResourceManager::request_cell_property(const std::string& _name) { - if(!_name.empty()) { - for(std::vector::iterator it = persistent_cprops_.begin(); - it != persistent_cprops_.end(); ++it) { - if((*it)->name() == _name) { - CellPropertyT* prop = dynamic_cast* >(*it); - return *prop; - } - } - } - - CellPropHandle handle = cell_props_.size(); - - CellPropertyT* prop = new CellPropertyT(_name, *this, handle); - (*prop)->resize(n_cells()); - - // Store property pointer - cell_props_.push_back(prop); - - // Decrease reference counter by one - // since the property is going to be copied - // on return - --(*prop).h_->count_; - - return *prop; + return request_property,CellPropertyT,CellPropHandle>(cell_props_, _name, n_cells()); } template MeshPropertyT ResourceManager::request_mesh_property(const std::string& _name) { + return request_property,MeshPropertyT,MeshPropHandle>(mesh_props_, _name, 1); +} + +template +PropT ResourceManager::request_property(StdVecT& _vec, const std::string& _name, size_t _size) { + if(!_name.empty()) { - for(std::vector::iterator it = persistent_mprops_.begin(); - it != persistent_mprops_.end(); ++it) { + for(typename StdVecT::iterator it = _vec.begin(); + it != _vec.end(); ++it) { + if(!(*it)->persistent()) continue; if((*it)->name() == _name) { - MeshPropertyT* prop = dynamic_cast* >(*it); + PropT* prop = dynamic_cast(*it); return *prop; } } } - MeshPropHandle handle = mesh_props_.size(); + HandleT handle(_vec.size()); - MeshPropertyT* prop = new MeshPropertyT(_name, *this, handle); - (*prop)->resize(1u); + PropT* prop = new PropT(_name, *this, handle); + prop->resize(_size); // Store property pointer - mesh_props_.push_back(prop); - - // Decrease reference counter by one - // since the property is going to be copied - // on return - --(*prop).h_->count_; + _vec.push_back(prop); return *prop; } @@ -256,155 +120,91 @@ MeshPropertyT ResourceManager::request_mesh_property(const std::string& _name template void ResourceManager::set_persistent(VertexPropertyT& _prop, bool _flag) { - if(_flag == _prop.h_->persistent_) return; - - if(!_flag) { - for(std::vector::iterator it = persistent_vprops_.begin(); - it != persistent_vprops_.end(); ++it) { - if((*it)->ptr() == _prop.h_->ptr_) { - _prop.h_->persistent_ = false; - delete (*it); - persistent_vprops_.erase(it); - break; - } - } - } else { - _prop.h_->persistent_ = true; - VertexPropertyT* prop = new VertexPropertyT(_prop); - persistent_vprops_.push_back(prop); - } + set_persistentT(_prop, _flag); } template void ResourceManager::set_persistent(EdgePropertyT& _prop, bool _flag) { - if(_flag == _prop.h_->persistent_) return; - - if(!_flag) { - for(std::vector::iterator it = persistent_eprops_.begin(); - it != persistent_eprops_.end(); ++it) { - if((*it)->ptr() == _prop.h_->ptr_) { - _prop.h_->persistent_ = false; - delete (*it); - persistent_eprops_.erase(it); - break; - } - } - } else { - _prop.h_->persistent_ = true; - EdgePropertyT* prop = new EdgePropertyT(_prop); - persistent_eprops_.push_back(prop); - } + set_persistentT(_prop, _flag); } template void ResourceManager::set_persistent(HalfEdgePropertyT& _prop, bool _flag) { - if(_flag == _prop.h_->persistent_) return; - - if(!_flag) { - for(std::vector::iterator it = persistent_heprops_.begin(); - it != persistent_heprops_.end(); ++it) { - if((*it)->ptr() == _prop.h_->ptr_) { - _prop.h_->persistent_ = false; - delete (*it); - persistent_heprops_.erase(it); - break; - } - } - } else { - _prop.h_->persistent_ = true; - HalfEdgePropertyT* prop = new HalfEdgePropertyT(_prop); - persistent_heprops_.push_back(prop); - } + set_persistentT(_prop, _flag); } template void ResourceManager::set_persistent(FacePropertyT& _prop, bool _flag) { - if(_flag == _prop.h_->persistent_) return; - - if(!_flag) { - for(std::vector::iterator it = persistent_fprops_.begin(); - it != persistent_fprops_.end(); ++it) { - if((*it)->ptr() == _prop.h_->ptr_) { - _prop.h_->persistent_ = false; - delete (*it); - persistent_fprops_.erase(it); - break; - } - } - } else { - _prop.h_->persistent_ = true; - FacePropertyT* prop = new FacePropertyT(_prop); - persistent_fprops_.push_back(prop); - } + set_persistentT(_prop, _flag); } template void ResourceManager::set_persistent(HalfFacePropertyT& _prop, bool _flag) { - if(_flag == _prop.h_->persistent_) return; - - if(!_flag) { - for(std::vector::iterator it = persistent_hfprops_.begin(); - it != persistent_hfprops_.end(); ++it) { - if((*it)->ptr() == _prop.h_->ptr_) { - _prop.h_->persistent_ = false; - delete (*it); - persistent_hfprops_.erase(it); - break; - } - } - } else { - _prop.h_->persistent_ = true; - HalfFacePropertyT* prop = new HalfFacePropertyT(_prop); - persistent_hfprops_.push_back(prop); - } + set_persistentT(_prop, _flag); } template void ResourceManager::set_persistent(CellPropertyT& _prop, bool _flag) { - if(_flag == _prop.h_->persistent_) return; - - if(!_flag) { - for(std::vector::iterator it = persistent_cprops_.begin(); - it != persistent_cprops_.end(); ++it) { - if((*it)->ptr() == _prop.h_->ptr_) { - _prop.h_->persistent_ = false; - delete (*it); - persistent_cprops_.erase(it); - break; - } - } - } else { - _prop.h_->persistent_ = true; - CellPropertyT* prop = new CellPropertyT(_prop); - persistent_cprops_.push_back(prop); - } + set_persistentT(_prop, _flag); } template void ResourceManager::set_persistent(MeshPropertyT& _prop, bool _flag) { - if(_flag == _prop.h_->persistent_) return; + set_persistentT(_prop, _flag); +} + +template +void ResourceManager::set_persistentT(PropT& _prop, bool _flag) { - if(!_flag) { - for(std::vector::iterator it = persistent_mprops_.begin(); - it != persistent_mprops_.end(); ++it) { - if((*it)->ptr() == _prop.h_->ptr_) { - _prop.h_->persistent_ = false; - delete (*it); - persistent_mprops_.erase(it); - break; - } - } - } else { - _prop.h_->persistent_ = true; - MeshPropertyT* prop = new MeshPropertyT(_prop); - persistent_mprops_.push_back(prop); + if(_flag == _prop->persistent()) return; + + _prop->set_persistent(_flag); +} + +template +void ResourceManager::remove_property(StdVecT& _vec, size_t _idx) { + + (*(_vec.begin() + _idx))->lock(); + delete *(_vec.begin() + _idx); + _vec.erase(_vec.begin() + _idx); + size_t n = _vec.size(); + for(size_t i = 0; i < n; ++i) { + _vec[i]->set_handle(OpenVolumeMeshHandle(i)); + } +} + +template +void ResourceManager::resize_props(StdVecT& _vec, unsigned int _n) { + + for(typename StdVecT::iterator it = _vec.begin(); + it != _vec.end(); ++it) { + (*it)->resize(_n); + } +} + +template +void ResourceManager::entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle& _h) { + + for(typename StdVecT::iterator it = _vec.begin(); + it != _vec.end(); ++it) { + (*it)->delete_element(_h.idx()); + } +} + +template +void ResourceManager::clearVec(StdVecT& _vec) { + + for(typename StdVecT::iterator it = _vec.begin(); + it != _vec.end(); ++it) { + delete *it; } + _vec.clear(); } } // Namespace OpenVolumeMesh diff --git a/src/Unittests/unittests_properties.hh b/src/Unittests/unittests_properties.hh index 223cac670349cb7785e16fa9f8a95bd2a9db51a3..a8c41ad5233b3b9471c8d0d2aac36bde76dda678 100644 --- a/src/Unittests/unittests_properties.hh +++ b/src/Unittests/unittests_properties.hh @@ -11,6 +11,16 @@ using namespace OpenVolumeMesh; +TEST_F(PolyhedralMeshBase, PropertySmartPointerTestSimple) { + + generatePolyhedralMesh(mesh_); + + for(int i = 0; i < 1; ++i) { + + VertexPropertyT v_prop = mesh_.request_vertex_property("SimpleTestProp"); + } +} + TEST_F(PolyhedralMeshBase, PropertySmartPointerTest1) { generatePolyhedralMesh(mesh_);