Developer Documentation
ResourceManager.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 #pragma once
36 
37 #ifndef NDEBUG
38 #include <iostream>
39 #endif
40 #include <string>
41 #include <vector>
42 #include <type_traits>
43 
44 #include "../System/Compiler.hh"
45 #include "OpenVolumeMesh/Config/Export.hh"
46 #include "OpenVolumeMeshProperty.hh"
47 #include "PropertyHandles.hh"
48 #include "TypeName.hh"
49 #include "ForwardDeclarations.hh"
50 
51 #if OVM_CXX_17
52 #include <optional>
53 #endif
54 
55 namespace OpenVolumeMesh {
56 
57 // Forward declarations
58 class BaseProperty;
59 
60 class OVM_EXPORT ResourceManager {
61 public:
62  ResourceManager() = default;
63  ResourceManager(const ResourceManager &other);
65  ResourceManager& operator=(const ResourceManager &other);
66  ResourceManager& operator=(ResourceManager &&other);
67  virtual ~ResourceManager();
68 
69  template <class PropT, class HandleT> friend class PropertyPtr;
70 
72  void resize_vprops(size_t _nv);
73 
75  void resize_eprops(size_t _ne);
76 
78  void resize_fprops(size_t _nf);
79 
81  void resize_cprops(size_t _nc);
82 
83 protected:
84 
85  void vertex_deleted(const VertexHandle& _h);
86 
87  void edge_deleted(const EdgeHandle& _h);
88 
89  void face_deleted(const FaceHandle& _h);
90 
91  void cell_deleted(const CellHandle& _h);
92 
93  void swap_cell_properties(CellHandle _h1, CellHandle _h2);
94 
95  void swap_face_properties(FaceHandle _h1, FaceHandle _h2);
96 
97  void swap_halfface_properties(HalfFaceHandle _h1, HalfFaceHandle _h2);
98 
99  void swap_edge_properties(EdgeHandle _h1, EdgeHandle _h2);
100 
101  void swap_halfedge_properties(HalfEdgeHandle _h1, HalfEdgeHandle _h2);
102 
103  void swap_vertex_properties(VertexHandle _h1, VertexHandle _h2);
104 
105  template <typename PropIterator, typename Handle>
106  void swap_property_elements(PropIterator _begin, PropIterator _end, Handle _h1, Handle _h2)
107  {
108  PropIterator p_iter = _begin;
109  for (; p_iter != _end; ++p_iter)
110  (*p_iter)->swap_elements(_h1.uidx(), _h2.uidx());
111  }
112 
113 
114 public:
115 
116  void clear_vertex_props() { clearVec(vertex_props_); }
117 
118  void clear_edge_props() { clearVec(edge_props_); }
119 
120  void clear_halfedge_props() { clearVec(halfedge_props_); }
121 
122  void clear_face_props() { clearVec(face_props_); }
123 
124  void clear_halfface_props() { clearVec(halfface_props_); }
125 
126  void clear_cell_props() { clearVec(cell_props_); }
127 
128  void clear_mesh_props() { clearVec(mesh_props_); }
129 
131  virtual size_t n_vertices() const = 0;
133  virtual size_t n_edges() const = 0;
135  virtual size_t n_halfedges() const = 0;
137  virtual size_t n_faces() const = 0;
139  virtual size_t n_halffaces() const = 0;
141  virtual size_t n_cells() const = 0;
142 
143 
146  template<typename T, typename EntityTag>
147  PropertyTT<T, EntityTag> request_property(const std::string& _name = std::string(), const T _def = T());
148 
149 #if OVM_CXX_17
150 
152  template<typename T, typename EntityTag>
153  std::optional<PropertyTT<T, EntityTag>> create_property(const std::string& _name = std::string(), const T _def = T());
154 
157  template<typename T, typename EntityTag>
158  std::optional<PropertyTT<T, EntityTag>> get_property(const std::string& _name = std::string());
159 #endif
160 
161  template<class T> VertexPropertyT<T> request_vertex_property(const std::string& _name = std::string(), const T _def = T());
162 
163  template<class T> EdgePropertyT<T> request_edge_property(const std::string& _name = std::string(), const T _def = T());
164 
165  template<class T> HalfEdgePropertyT<T> request_halfedge_property(const std::string& _name = std::string(), const T _def = T());
166 
167  template<class T> FacePropertyT<T> request_face_property(const std::string& _name = std::string(), const T _def = T());
168 
169  template<class T> HalfFacePropertyT<T> request_halfface_property(const std::string& _name = std::string(), const T _def = T());
170 
171  template<class T> CellPropertyT<T> request_cell_property(const std::string& _name = std::string(), const T _def = T());
172 
173  template<class T> MeshPropertyT<T> request_mesh_property(const std::string& _name = std::string(), const T _def = T());
174 
175 
176 private:
177 
178  void release_property(VertexPropHandle _handle);
179 
180  void release_property(EdgePropHandle _handle);
181 
182  void release_property(HalfEdgePropHandle _handle);
183 
184  void release_property(FacePropHandle _handle);
185 
186  void release_property(HalfFacePropHandle _handle);
187 
188  void release_property(CellPropHandle _handle);
189 
190  void release_property(MeshPropHandle _handle);
191 
192 public:
193 
194  size_t n_vertex_props() const { return vertex_props_.size(); }
195 
196  size_t n_edge_props() const { return edge_props_.size(); }
197 
198  size_t n_halfedge_props() const { return halfedge_props_.size(); }
199 
200  size_t n_face_props() const { return face_props_.size(); }
201 
202  size_t n_halfface_props() const { return halfface_props_.size(); }
203 
204  size_t n_cell_props() const { return cell_props_.size(); }
205 
206  size_t n_mesh_props() const { return mesh_props_.size(); }
207 
208  template<typename T, class EntityTag>
209  void set_persistent(PropertyTT<T, EntityTag>& _prop, bool _flag = true);
210 
211  typedef std::vector<BaseProperty*> Properties;
212 
213  Properties::const_iterator vertex_props_begin() const { return vertex_props_.begin(); }
214 
215  Properties::const_iterator vertex_props_end() const { return vertex_props_.end(); }
216 
217  Properties::const_iterator edge_props_begin() const { return edge_props_.begin(); }
218 
219  Properties::const_iterator edge_props_end() const { return edge_props_.end(); }
220 
221  Properties::const_iterator halfedge_props_begin() const { return halfedge_props_.begin(); }
222 
223  Properties::const_iterator halfedge_props_end() const { return halfedge_props_.end(); }
224 
225  Properties::const_iterator face_props_begin() const { return face_props_.begin(); }
226 
227  Properties::const_iterator face_props_end() const { return face_props_.end(); }
228 
229  Properties::const_iterator halfface_props_begin() const { return halfface_props_.begin(); }
230 
231  Properties::const_iterator halfface_props_end() const { return halfface_props_.end(); }
232 
233  Properties::const_iterator cell_props_begin() const { return cell_props_.begin(); }
234 
235  Properties::const_iterator cell_props_end() const { return cell_props_.end(); }
236 
237  Properties::const_iterator mesh_props_begin() const { return mesh_props_.begin(); }
238 
239  Properties::const_iterator mesh_props_end() const { return mesh_props_.end(); }
240 
241 private:
242 
243  template <class FullPropT, class PropIterT>
244  bool property_exists(const PropIterT& _begin, const PropIterT& _end, const std::string& _name) const
245  {
246  auto type_name = get_type_name<typename FullPropT::value_type>();
247 
248  if(_name.empty()) {
249 #ifndef NDEBUG
250  std::cerr << "property_exists(): Checking for the existence of anonymous properties is" << std::endl;
251  std::cerr << "ambiguous!" << std::endl;
252 #endif
253  return false;
254  }
255 
256  PropIterT it = _begin;
257  for(; it != _end; ++it)
258  {
259  if((*it)->name() == _name
260  && (*it)->internal_type_name() == type_name)
261  {
262  return true;
263  }
264  }
265  return false;
266  }
267 
268 public:
269 
270  template <class PropT>
271  bool vertex_property_exists(const std::string& _name) const {
272  return property_exists<VertexPropertyT<PropT> >(vertex_props_begin(), vertex_props_end(), _name);
273  }
274 
275  template <class PropT>
276  bool edge_property_exists(const std::string& _name) const {
277  return property_exists<EdgePropertyT<PropT> >(edge_props_begin(), edge_props_end(), _name);
278  }
279 
280  template <class PropT>
281  bool halfedge_property_exists(const std::string& _name) const {
282  return property_exists<HalfEdgePropertyT<PropT> >(halfedge_props_begin(), halfedge_props_end(), _name);
283  }
284 
285  template <class PropT>
286  bool face_property_exists(const std::string& _name) const {
287  return property_exists<FacePropertyT<PropT> >(face_props_begin(), face_props_end(), _name);
288  }
289 
290  template <class PropT>
291  bool halfface_property_exists(const std::string& _name) const {
292  return property_exists<HalfFacePropertyT<PropT> >(halfface_props_begin(), halfface_props_end(), _name);
293  }
294 
295  template <class PropT>
296  bool cell_property_exists(const std::string& _name) const {
297  return property_exists<CellPropertyT<PropT> >(cell_props_begin(), cell_props_end(), _name);
298  }
299 
300  template <class PropT>
301  bool mesh_property_exists(const std::string& _name) const {
302  return property_exists<MeshPropertyT<PropT> >(mesh_props_begin(), mesh_props_end(), _name);
303  }
304 
305 protected:
306 
307  void delete_multiple_vertex_props(const std::vector<bool>& _tags);
308 
309  void delete_multiple_edge_props(const std::vector<bool>& _tags);
310 
311  void delete_multiple_face_props(const std::vector<bool>& _tags);
312 
313  void delete_multiple_cell_props(const std::vector<bool>& _tags);
314 
315 private:
316 
317  template<class StdVecT>
318  void resize_props(StdVecT& _vec, size_t _n);
319 
320  template<class StdVecT>
321  void entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle& _h);
322 
323  template<class StdVecT>
324  void remove_property(StdVecT& _vec, size_t _idx);
325 
326  template<typename T, typename EntityTag>
327  PropertyTT<T, EntityTag> *internal_find_property(const std::string& _name);
328 
329  template<typename T, typename EntityTag>
330  PropertyTT<T, EntityTag> internal_create_property(const std::string& _name, const T _def = T());
331 
332  template<class StdVecT>
333  void clearVec(StdVecT& _vec);
334 
335  template<class StdVecT>
336  void updatePropHandles(StdVecT& _vec);
337 
338  template<bool Move>
339  void assignProperties(typename std::conditional<Move, Properties&, const Properties&>::type src,
340  Properties &dest);
341  template<bool Move>
342  void assignAllPropertiesFrom(typename std::conditional<Move, ResourceManager*, const ResourceManager*>::type src);
343 
344  Properties vertex_props_;
345 
346  Properties edge_props_;
347 
348  Properties halfedge_props_;
349 
350  Properties face_props_;
351 
352  Properties halfface_props_;
353 
354  Properties cell_props_;
355 
356  Properties mesh_props_;
357 
358  template<typename Entity>
359  Properties &entity_props();
360 
361  template<typename Entity>
362  size_t n();
363 };
364 
365 }
366 
367 #if defined(INCLUDE_TEMPLATES) && !defined(RESOURCEMANAGERT_CC)
368 #include "ResourceManagerT_impl.hh"
369 #endif
370