Developer Documentation
ResourceManager.hh
1#pragma once
2/*===========================================================================*\
3 * *
4 * OpenVolumeMesh *
5 * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
6 * www.openvolumemesh.org *
7 * *
8 *---------------------------------------------------------------------------*
9 * This file is part of OpenVolumeMesh. *
10 * *
11 * OpenVolumeMesh is free software: you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License as *
13 * published by the Free Software Foundation, either version 3 of *
14 * the License, or (at your option) any later version with the *
15 * following exceptions: *
16 * *
17 * If other files instantiate templates or use macros *
18 * or inline functions from this file, or you compile this file and *
19 * link it with other files to produce an executable, this file does *
20 * not by itself cause the resulting executable to be covered by the *
21 * GNU Lesser General Public License. This exception does not however *
22 * invalidate any other reasons why the executable file might be *
23 * covered by the GNU Lesser General Public License. *
24 * *
25 * OpenVolumeMesh is distributed in the hope that it will be useful, *
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
28 * GNU Lesser General Public License for more details. *
29 * *
30 * You should have received a copy of the GNU LesserGeneral Public *
31 * License along with OpenVolumeMesh. If not, *
32 * see <http://www.gnu.org/licenses/>. *
33 * *
34\*===========================================================================*/
35
36
37#ifndef NDEBUG
38#include <iostream>
39#endif
40#include <string>
41#include <vector>
42#include <memory>
43#include <set>
44#include <type_traits>
45#include <optional>
46
47#include <OpenVolumeMesh/Config/Export.hh>
48#include <OpenVolumeMesh/Core/Entities.hh>
49#include <OpenVolumeMesh/Core/EntityUtils.hh>
50#include <OpenVolumeMesh/Core/ForwardDeclarations.hh>
51#include <OpenVolumeMesh/Core/detail/Tracking.hh>
52#include <OpenVolumeMesh/Core/Properties/PropertyIterator.hh>
53#include <OpenVolumeMesh/Core/Properties/PropertyStorageBase.hh>
54#include <OpenVolumeMesh/Core/Properties/PropertyPtr.hh>
55
56
57namespace OpenVolumeMesh {
58class OVM_EXPORT ResourceManager {
59
60public:
61 using Properties = std::set<PropertyStorageBase*>;
62
63 ResourceManager() = default;
64 virtual ~ResourceManager() = default;
65
66 ResourceManager(const ResourceManager &other);
67 ResourceManager& operator=(const ResourceManager &other);
68
69 ResourceManager(ResourceManager &&other) = delete;
70 ResourceManager& operator=(ResourceManager &&other) = delete;
71
72private:
73 using PersistentProperties = std::set<std::shared_ptr<PropertyStorageBase>>;
74
75 template <class T, typename EntityTag>
76 static PropertyPtr<T, EntityTag> prop_ptr_from_storage(PropertyStorageBase *_prop);
77
78 template<class EntityTag>
79 void resize_props(size_t _n);
80
81 template<class EntityTag>
82 void reserve_props(size_t _n);
83
84 template<class Handle>
85 void entity_deleted(Handle);
86
87 template<typename T, typename EntityTag>
88 std::optional<PropertyPtr<T, EntityTag>> internal_find_property(const std::string& _name) const;
89
90 template<typename T, typename EntityTag>
91 PropertyPtr<T, EntityTag> internal_create_property(std::string _name, const T _def, bool shared) const;
92
93
94 void clone_persistent_properties_from(ResourceManager const&);
95
96 PerEntity<PersistentProperties> persistent_props_;
97
98protected:
99 friend class PropertyStorageBase;
100
101 template<typename EntityTag>
102 detail::Tracker<PropertyStorageBase> & storage_tracker() const;
103 detail::Tracker<PropertyStorageBase> & storage_tracker(EntityType type) const;
104
105 mutable PerEntity<detail::Tracker<PropertyStorageBase>> storage_trackers_;
106
107protected:
108
110 void resize_vprops(size_t _nv);
111
113 void resize_eprops(size_t _ne);
114
116 void resize_fprops(size_t _nf);
117
119 void resize_cprops(size_t _nc);
120
121 void reserve_vprops(size_t n);
122 void reserve_eprops(size_t n);
123 void reserve_fprops(size_t n);
124 void reserve_cprops(size_t n);
125
126 void vertex_deleted(const VertexHandle& _h);
127 void edge_deleted(const EdgeHandle& _h);
128 void face_deleted(const FaceHandle& _h);
129 void cell_deleted(const CellHandle& _h);
130
131 template <typename Handle>
132 void swap_property_elements(Handle _idx_a, Handle _idx_b);
133
134 template <typename Handle>
135 void copy_property_elements(Handle _idx_a, Handle _idx_b);
136
137public:
139 void clear_all_props();
141 template<typename EntityTag> void clear_props();
142
144 template<typename EntityTag>
145 size_t n() const;
146
147
149 virtual size_t n_vertices() const = 0;
151 virtual size_t n_edges() const = 0;
153 virtual size_t n_halfedges() const = 0;
155 virtual size_t n_faces() const = 0;
157 virtual size_t n_halffaces() const = 0;
159 virtual size_t n_cells() const = 0;
160
162 template<typename EntityTag> size_t n_props() const;
163
165 template<typename EntityTag> size_t n_persistent_props() const;
166
170 template<typename T, typename EntityTag>
171 PropertyPtr<T, EntityTag> request_property(const std::string& _name = std::string(), const T _def = T());
172
175 template<typename T, typename EntityTag>
176 [[deprecated("Use create_{shared,persistent}_property instead")]]
177 std::optional<PropertyPtr<T, EntityTag>> create_property(std::string _name = std::string(), const T _def = T());
178
181 template<typename T, typename EntityTag>
182 std::optional<PropertyPtr<T, EntityTag>> create_shared_property(std::string _name, const T _def = T());
183
186 template<typename T, typename EntityTag>
187 std::optional<PropertyPtr<T, EntityTag>> create_persistent_property(std::string _name, const T _def = T());
188
191 template<typename T, typename EntityTag>
192 PropertyPtr<T, EntityTag> create_private_property(std::string _name = {}, const T _def = T()) const;
193
196 template<typename T, typename EntityTag>
197 std::optional<PropertyPtr<T, EntityTag>> get_property(const std::string& _name);
198
201 template<typename T, typename EntityTag>
202 std::optional<const PropertyPtr<T, EntityTag>> get_property(const std::string& _name) const;
203
204 template <typename T, typename EntityTag>
205 bool property_exists(const std::string& _name) const
206 {
207 return internal_find_property<T, EntityTag>(_name).has_value();
208 }
209
210
211 template<typename T, class EntityTag>
212 void set_persistent(PropertyPtr<T, EntityTag>& _prop, bool _enable = true);
213
214 template<typename T, class EntityTag>
215 void set_shared(PropertyPtr<T, EntityTag>& _prop, bool _enable = true);
216
217 template<typename EntityTag>
218 PropertyIterator<PersistentProperties::const_iterator>
219 persistent_props_begin() const
220 {return persistent_props_.get<EntityTag>().cbegin();}
221
222 template<typename EntityTag>
223 PropertyIterator<PersistentProperties::const_iterator>
224 persistent_props_end() const
225 {return persistent_props_.get<EntityTag>().cend();}
226
227
229
230 inline void clear_vertex_props() { clear_props<Entity::Vertex>();}
231 inline void clear_edge_props() { clear_props<Entity::Edge>();}
232 inline void clear_halfedge_props() { clear_props<Entity::HalfEdge>();}
233 inline void clear_face_props() { clear_props<Entity::Face>();}
234 inline void clear_halfface_props() { clear_props<Entity::HalfFace>();}
235 inline void clear_cell_props() { clear_props<Entity::Cell>();}
236 inline void clear_mesh_props() { clear_props<Entity::Mesh>();}
237
238 // TODO: should we deprecate request_*_property, replace with get_or_create_*_property for more clarity?
239 template<class T> VertexPropertyT<T> request_vertex_property (const std::string& _name = std::string(), const T _def = T());
240 template<class T> EdgePropertyT<T> request_edge_property (const std::string& _name = std::string(), const T _def = T());
241 template<class T> HalfEdgePropertyT<T> request_halfedge_property(const std::string& _name = std::string(), const T _def = T());
242 template<class T> FacePropertyT<T> request_face_property (const std::string& _name = std::string(), const T _def = T());
243 template<class T> HalfFacePropertyT<T> request_halfface_property(const std::string& _name = std::string(), const T _def = T());
244 template<class T> CellPropertyT<T> request_cell_property (const std::string& _name = std::string(), const T _def = T());
245 template<class T> MeshPropertyT<T> request_mesh_property (const std::string& _name = std::string(), const T _def = T());
246
247
251
252 size_t n_vertex_props() const {return n_props<Entity::Vertex>();}
253 auto vertex_props_begin() const {return persistent_props_begin<Entity::Vertex>();}
254 auto vertex_props_end() const {return persistent_props_end <Entity::Vertex>();}
255
258 template<typename T>
259 std::optional<VertexPropertyPtr<T>> create_shared_vertex_property(std::string _name, const T _def = T())
260 { return create_shared_property<T, Entity::Vertex>(std::move(_name), std::move(_def)); }
261
264 template<typename T>
265 std::optional<VertexPropertyPtr<T>> create_persistent_vertex_property(std::string _name, const T _def = T())
266 { return create_persistent_property<T, Entity::Vertex>(std::move(_name), std::move(_def)); }
267
270 template<typename T>
271 VertexPropertyPtr<T> create_private_vertex_property(std::string _name = {}, const T _def = T()) const
272 { return create_private_property<T, Entity::Vertex>(std::move(_name), std::move(_def)); }
273
276 template<typename T>
277 std::optional<VertexPropertyPtr<T>> get_vertex_property(const std::string& _name) {
278 return get_property<T, Entity::Vertex>(_name);
279 }
280
283 template<typename T>
284 std::optional<const VertexPropertyPtr<T>> get_vertex_property(const std::string& _name) const {
285 return get_property<T, Entity::Vertex>(_name);
286 }
287
288 template <class T>
289 bool vertex_property_exists(const std::string& _name) const {
290 return property_exists<T, Entity::Vertex>(_name);
291 }
292
293
294
298
299 size_t n_edge_props() const {return n_props<Entity::Edge>();}
300 auto edge_props_begin() const {return persistent_props_begin<Entity::Edge>();}
301 auto edge_props_end() const {return persistent_props_end <Entity::Edge>();}
302
305 template<typename T>
306 std::optional<EdgePropertyPtr<T>> create_shared_edge_property(std::string _name, const T _def = T())
307 { return create_shared_property<T, Entity::Edge>(std::move(_name), std::move(_def)); }
308
311 template<typename T>
312 std::optional<EdgePropertyPtr<T>> create_persistent_edge_property(std::string _name, const T _def = T())
313 { return create_persistent_property<T, Entity::Edge>(std::move(_name), std::move(_def)); }
314
317 template<typename T>
318 EdgePropertyPtr<T> create_private_edge_property(std::string _name = {}, const T _def = T()) const
319 { return create_private_property<T, Entity::Edge>(std::move(_name), std::move(_def)); }
320
323 template<typename T>
324 std::optional<EdgePropertyPtr<T>> get_edge_property(const std::string& _name) {
325 return get_property<T, Entity::Edge>(_name);
326 }
327
330 template<typename T>
331 std::optional<const EdgePropertyPtr<T>> get_edge_property(const std::string& _name) const {
332 return get_property<T, Entity::Edge>(_name);
333 }
334
335 template <class T>
336 bool edge_property_exists(const std::string& _name) const {
337 return property_exists<T, Entity::Edge>(_name);
338 }
339
340
341
345
346 size_t n_halfedge_props() const {return n_props<Entity::HalfEdge>();}
347 auto halfedge_props_begin() const {return persistent_props_begin<Entity::HalfEdge>();}
348 auto halfedge_props_end() const {return persistent_props_end <Entity::HalfEdge>();}
349
352 template<typename T>
353 std::optional<HalfEdgePropertyPtr<T>> create_shared_halfedge_property(std::string _name, const T _def = T())
354 { return create_shared_property<T, Entity::HalfEdge>(std::move(_name), std::move(_def)); }
355
358 template<typename T>
359 std::optional<HalfEdgePropertyPtr<T>> create_persistent_halfedge_property(std::string _name, const T _def = T())
360 { return create_persistent_property<T, Entity::HalfEdge>(std::move(_name), std::move(_def)); }
361
364 template<typename T>
365 HalfEdgePropertyPtr<T> create_private_halfedge_property(std::string _name = {}, const T _def = T()) const
366 { return create_private_property<T, Entity::HalfEdge>(std::move(_name), std::move(_def)); }
367
370 template<typename T>
371 std::optional<HalfEdgePropertyPtr<T>> get_halfedge_property(const std::string& _name) {
372 return get_property<T, Entity::HalfEdge>(_name);
373 }
374
377 template<typename T>
378 std::optional<const HalfEdgePropertyPtr<T>> get_halfedge_property(const std::string& _name) const {
379 return get_property<T, Entity::HalfEdge>(_name);
380 }
381
382 template <class T>
383 bool halfedge_property_exists(const std::string& _name) const {
384 return property_exists<T, Entity::HalfEdge>(_name);
385 }
386
387
388
392
393 size_t n_face_props() const {return n_props<Entity::Face>();}
394 auto face_props_begin() const {return persistent_props_begin<Entity::Face>();}
395 auto face_props_end() const {return persistent_props_end <Entity::Face>();}
396
399 template<typename T>
400 std::optional<FacePropertyPtr<T>> create_shared_face_property(std::string _name, const T _def = T())
401 { return create_shared_property<T, Entity::Face>(std::move(_name), std::move(_def)); }
402
405 template<typename T>
406 std::optional<FacePropertyPtr<T>> create_persistent_face_property(std::string _name, const T _def = T())
407 { return create_persistent_property<T, Entity::Face>(std::move(_name), std::move(_def)); }
408
411 template<typename T>
412 FacePropertyPtr<T> create_private_face_property(std::string _name = {}, const T _def = T()) const
413 { return create_private_property<T, Entity::Face>(std::move(_name), std::move(_def)); }
414
417 template<typename T>
418 std::optional<FacePropertyPtr<T>> get_face_property(const std::string& _name) {
419 return get_property<T, Entity::Face>(_name);
420 }
421
424 template<typename T>
425 std::optional<const FacePropertyPtr<T>> get_face_property(const std::string& _name) const {
426 return get_property<T, Entity::Face>(_name);
427 }
428
429 template <class T>
430 bool face_property_exists(const std::string& _name) const {
431 return property_exists<T, Entity::Face>(_name);
432 }
433
434
435
439
440 size_t n_halfface_props() const {return n_props<Entity::HalfFace>();}
441 auto halfface_props_begin() const {return persistent_props_begin<Entity::HalfFace>();}
442 auto halfface_props_end() const {return persistent_props_end <Entity::HalfFace>();}
443
446 template<typename T>
447 std::optional<HalfFacePropertyPtr<T>> create_shared_halfface_property(std::string _name, const T _def = T())
448 { return create_shared_property<T, Entity::HalfFace>(std::move(_name), std::move(_def)); }
449
452 template<typename T>
453 std::optional<HalfFacePropertyPtr<T>> create_persistent_halfface_property(std::string _name, const T _def = T())
454 { return create_persistent_property<T, Entity::HalfFace>(std::move(_name), std::move(_def)); }
455
458 template<typename T>
459 HalfFacePropertyPtr<T> create_private_halfface_property(std::string _name = {}, const T _def = T()) const
460 { return create_private_property<T, Entity::HalfFace>(std::move(_name), std::move(_def)); }
461
464 template<typename T>
465 std::optional<HalfFacePropertyPtr<T>> get_halfface_property(const std::string& _name) {
466 return get_property<T, Entity::HalfFace>(_name);
467 }
468
471 template<typename T>
472 std::optional<const HalfFacePropertyPtr<T>> get_halfface_property(const std::string& _name) const {
473 return get_property<T, Entity::HalfFace>(_name);
474 }
475
476 template <class T>
477 bool halfface_property_exists(const std::string& _name) const {
478 return property_exists<T, Entity::HalfFace>(_name);
479 }
480
481
482
486
487 size_t n_cell_props() const {return n_props<Entity::Cell>();}
488 auto cell_props_begin() const {return persistent_props_begin<Entity::Cell>();}
489 auto cell_props_end() const {return persistent_props_end <Entity::Cell>();}
490
493 template<typename T>
494 std::optional<CellPropertyPtr<T>> create_shared_cell_property(std::string _name, const T _def = T())
495 { return create_shared_property<T, Entity::Cell>(std::move(_name), std::move(_def)); }
496
499 template<typename T>
500 std::optional<CellPropertyPtr<T>> create_persistent_cell_property(std::string _name, const T _def = T())
501 { return create_persistent_property<T, Entity::Cell>(std::move(_name), std::move(_def)); }
502
505 template<typename T>
506 CellPropertyPtr<T> create_private_cell_property(std::string _name = {}, const T _def = T()) const
507 { return create_private_property<T, Entity::Cell>(std::move(_name), std::move(_def)); }
508
511 template<typename T>
512 std::optional<CellPropertyPtr<T>> get_cell_property(const std::string& _name) {
513 return get_property<T, Entity::Cell>(_name);
514 }
515
518 template<typename T>
519 std::optional<const CellPropertyPtr<T>> get_cell_property(const std::string& _name) const {
520 return get_property<T, Entity::Cell>(_name);
521 }
522
523 template <class T>
524 bool cell_property_exists(const std::string& _name) const {
525 return property_exists<T, Entity::Cell>(_name);
526 }
527
528
529
530};
531
532}
533
534#include <OpenVolumeMesh/Core/ResourceManagerT_impl.hh>
535
std::optional< const VertexPropertyPtr< T > > get_vertex_property(const std::string &_name) const
HalfEdgePropertyPtr< T > create_private_halfedge_property(std::string _name={}, const T _def=T()) const
std::optional< HalfEdgePropertyPtr< T > > create_shared_halfedge_property(std::string _name, const T _def=T())
std::optional< EdgePropertyPtr< T > > create_persistent_edge_property(std::string _name, const T _def=T())
std::optional< FacePropertyPtr< T > > create_shared_face_property(std::string _name, const T _def=T())
std::optional< HalfFacePropertyPtr< T > > get_halfface_property(const std::string &_name)
VertexPropertyPtr< T > create_private_vertex_property(std::string _name={}, const T _def=T()) const
FacePropertyPtr< T > create_private_face_property(std::string _name={}, const T _def=T()) const
std::optional< HalfFacePropertyPtr< T > > create_shared_halfface_property(std::string _name, const T _def=T())
std::optional< FacePropertyPtr< T > > create_persistent_face_property(std::string _name, const T _def=T())
size_t n() const
Get number of entities of given kind in mesh.
std::optional< CellPropertyPtr< T > > get_cell_property(const std::string &_name)
std::optional< HalfEdgePropertyPtr< T > > create_persistent_halfedge_property(std::string _name, const T _def=T())
virtual size_t n_edges() const =0
Get number of edges in mesh.
std::optional< PropertyPtr< T, EntityTag > > create_property(std::string _name=std::string(), const T _def=T())
std::optional< FacePropertyPtr< T > > get_face_property(const std::string &_name)
std::optional< VertexPropertyPtr< T > > get_vertex_property(const std::string &_name)
std::optional< EdgePropertyPtr< T > > get_edge_property(const std::string &_name)
virtual size_t n_faces() const =0
Get number of faces in mesh.
virtual size_t n_halffaces() const =0
Get number of halffaces in mesh.
std::optional< CellPropertyPtr< T > > create_shared_cell_property(std::string _name, const T _def=T())
std::optional< VertexPropertyPtr< T > > create_persistent_vertex_property(std::string _name, const T _def=T())
std::optional< HalfEdgePropertyPtr< T > > get_halfedge_property(const std::string &_name)
EdgePropertyPtr< T > create_private_edge_property(std::string _name={}, const T _def=T()) const
virtual size_t n_halfedges() const =0
Get number of halfedges in mesh.
std::optional< VertexPropertyPtr< T > > create_shared_vertex_property(std::string _name, const T _def=T())
std::optional< HalfFacePropertyPtr< T > > create_persistent_halfface_property(std::string _name, const T _def=T())
void clear_vertex_props()
convenience functions:
std::optional< EdgePropertyPtr< T > > create_shared_edge_property(std::string _name, const T _def=T())
virtual size_t n_cells() const =0
Get number of cells in mesh.
std::optional< const EdgePropertyPtr< T > > get_edge_property(const std::string &_name) const
std::optional< const HalfEdgePropertyPtr< T > > get_halfedge_property(const std::string &_name) const
HalfFacePropertyPtr< T > create_private_halfface_property(std::string _name={}, const T _def=T()) const
std::optional< const HalfFacePropertyPtr< T > > get_halfface_property(const std::string &_name) const
std::optional< CellPropertyPtr< T > > create_persistent_cell_property(std::string _name, const T _def=T())
std::optional< const FacePropertyPtr< T > > get_face_property(const std::string &_name) const
virtual size_t n_vertices() const =0
Get number of vertices in mesh.
std::optional< const CellPropertyPtr< T > > get_cell_property(const std::string &_name) const
CellPropertyPtr< T > create_private_cell_property(std::string _name={}, const T _def=T()) const