Developer Documentation
ResourceManagerT_impl.hh
1 /*===========================================================================*\
2  * *
3  * OpenVolumeMesh *
4  * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openvolumemesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenVolumeMesh. *
9  * *
10  * OpenVolumeMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenVolumeMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenVolumeMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 #define RESOURCEMANAGERT_CC
36 
37 #include "ResourceManager.hh"
38 #include "PropertyDefines.hh"
39 #include "TypeName.hh"
40 
41 namespace OpenVolumeMesh {
42 
43 
44 template<class T>
45 VertexPropertyT<T> ResourceManager::request_vertex_property(const std::string& _name, const T _def) {
46 
47  return request_property<T, Entity::Vertex>(_name, _def);
48 }
49 
50 template<class T>
51 EdgePropertyT<T> ResourceManager::request_edge_property(const std::string& _name, const T _def) {
52 
53  return request_property<T, Entity::Edge>(_name, _def);
54 }
55 
56 template<class T>
57 HalfEdgePropertyT<T> ResourceManager::request_halfedge_property(const std::string& _name, const T _def) {
58 
59  return request_property<T, Entity::HalfEdge>(_name, _def);
60 }
61 
62 template<class T>
63 FacePropertyT<T> ResourceManager::request_face_property(const std::string& _name, const T _def) {
64 
65  return request_property<T, Entity::Face>(_name, _def);
66 }
67 
68 template<class T>
69 HalfFacePropertyT<T> ResourceManager::request_halfface_property(const std::string& _name, const T _def) {
70  return request_property<T, Entity::HalfFace>(_name, _def);
71 }
72 
73 template<class T>
74 CellPropertyT<T> ResourceManager::request_cell_property(const std::string& _name, const T _def) {
75 
76  return request_property<T, Entity::Cell>(_name, _def);
77 }
78 
79 template<class T>
80 MeshPropertyT<T> ResourceManager::request_mesh_property(const std::string& _name, const T _def) {
81 
82  return request_property<T, Entity::Mesh>(_name, _def);
83 }
84 
85 template<typename T, typename EntityTag>
86 PropertyTT<T, EntityTag>* ResourceManager::internal_find_property(const std::string& _name)
87 {
88  using PropT = PropertyTT<T, EntityTag>;
89  auto type_name = get_type_name<T>();
90  auto &propVec = entity_props<EntityTag>();
91 
92  if(!_name.empty()) {
93  for(auto &prop: propVec)
94  {
95  if(prop->name() == _name
96  && prop->internal_type_name() == type_name)
97  {
98  return static_cast<PropT*>(prop);
99  }
100  }
101  }
102  return nullptr;
103 }
104 
105 template<class T, class EntityTag>
106 PropertyTT<T, EntityTag> ResourceManager::internal_create_property(const std::string& _name, const T _def)
107 {
108  auto type_name = get_type_name<T>();
109  auto &propVec = entity_props<EntityTag>();
110  auto handle = PropHandleT<EntityTag>::from_unsigned(propVec.size());
111  auto prop = new PropertyTT<T, EntityTag>(_name, type_name, *this, handle, _def);
112  prop->resize(n<EntityTag>());
113  propVec.push_back(prop);
114  return *prop;
115 }
116 
117 template<typename T, typename EntityTag>
118 PropertyTT<T, EntityTag> ResourceManager::request_property(const std::string& _name, const T _def)
119 {
120  auto *prop = internal_find_property<T, EntityTag>(_name);
121  if (prop)
122  return *prop;
123  return internal_create_property<T, EntityTag>(_name, _def);
124 }
125 
126 #if OVM_CXX_17
127 
128 template<typename T, typename EntityTag>
129 std::optional<PropertyTT<T, EntityTag>>
130 ResourceManager::create_property(const std::string& _name, const T _def)
131 {
132  auto *prop = internal_find_property<T, EntityTag>(_name);
133  if (prop)
134  return {};
135  return {*internal_create_property<T, EntityTag>(_name, _def)};
136 }
137 
138 template<typename T, typename EntityTag>
139 std::optional<PropertyTT<T, EntityTag>>
140 ResourceManager::get_property(const std::string& _name)
141 {
142  auto *prop = internal_find_property<T, EntityTag>(_name);
143  if (prop)
144  return {*prop};
145  return {};
146 }
147 #endif
148 
149 
150 template<typename T, class EntityTag>
151 void ResourceManager::set_persistent(PropertyTT<T, EntityTag>& _prop, bool _flag)
152 {
153  if(_flag == _prop->persistent()) return;
154  _prop->set_persistent(_flag);
155 }
156 
157 template<class StdVecT>
158 void ResourceManager::remove_property(StdVecT& _vec, size_t _idx) {
159 
160  auto prop_ptr = _vec[_idx];
161  prop_ptr->setResMan(nullptr);
162  delete prop_ptr;
163  _vec.erase(_vec.begin() + _idx);
164  updatePropHandles(_vec);
165 }
166 
167 template<class StdVecT>
168 void ResourceManager::resize_props(StdVecT& _vec, size_t _n) {
169 
170  for(typename StdVecT::iterator it = _vec.begin();
171  it != _vec.end(); ++it) {
172  (*it)->resize(_n);
173  }
174 }
175 
176 template<class StdVecT>
177 void ResourceManager::entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle& _h) {
178 
179  for(typename StdVecT::iterator it = _vec.begin();
180  it != _vec.end(); ++it) {
181  (*it)->delete_element(_h.uidx());
182  }
183 }
184 
185 template<class StdVecT>
186 void ResourceManager::clearVec(StdVecT& _vec) {
187 
188  for (auto prop: _vec) {
189  prop->setResMan(nullptr);
190  delete prop;
191  }
192  _vec.clear();
193 }
194 
195 template<class StdVecT>
196 void ResourceManager::updatePropHandles(StdVecT &_vec)
197 {
198  size_t n = _vec.size();
199  for(size_t i = 0; i < n; ++i) {
200  _vec[i]->set_handle(OpenVolumeMeshHandle(static_cast<int>(i)));
201  }
202 }
203 
204 } // Namespace OpenVolumeMesh
PropertyTT< T, EntityTag > request_property(const std::string &_name=std::string(), const T _def=T())
size_t uidx() const
return unsigned idx - handle must be valid