Developer Documentation
ResourceManagerT.cc
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 /*===========================================================================*\
36  * *
37  * $Revision$ *
38  * $Date$ *
39  * $LastChangedBy$ *
40  * *
41 \*===========================================================================*/
42 
43 #define RESOURCEMANAGERT_CC
44 
45 #include "ResourceManager.hh"
46 #include "PropertyDefines.hh"
47 
48 namespace OpenVolumeMesh {
49 
50 template<class T>
51 VertexPropertyT<T> ResourceManager::request_vertex_property(const std::string& _name, const T _def) {
52 
53  return request_property<std::vector<BaseProperty*>,VertexPropertyT<T>,VertexPropHandle,T>(vertex_props_, _name, n_vertices(), _def);
54 }
55 
56 template<class T>
57 EdgePropertyT<T> ResourceManager::request_edge_property(const std::string& _name, const T _def) {
58 
59  return request_property<std::vector<BaseProperty*>,EdgePropertyT<T>,EdgePropHandle,T>(edge_props_, _name, n_edges(), _def);
60 }
61 
62 template<class T>
63 HalfEdgePropertyT<T> ResourceManager::request_halfedge_property(const std::string& _name, const T _def) {
64 
65  return request_property<std::vector<BaseProperty*>,HalfEdgePropertyT<T>,HalfEdgePropHandle,T>(halfedge_props_, _name, n_edges()*2u, _def);
66 }
67 
68 template<class T>
69 FacePropertyT<T> ResourceManager::request_face_property(const std::string& _name, const T _def) {
70 
71  return request_property<std::vector<BaseProperty*>,FacePropertyT<T>,FacePropHandle,T>(face_props_, _name, n_faces(), _def);
72 }
73 
74 template<class T>
75 HalfFacePropertyT<T> ResourceManager::request_halfface_property(const std::string& _name, const T _def) {
76 
77  return request_property<std::vector<BaseProperty*>,HalfFacePropertyT<T>,HalfFacePropHandle,T>(halfface_props_, _name, n_faces()*2u, _def);
78 }
79 
80 template<class T>
81 CellPropertyT<T> ResourceManager::request_cell_property(const std::string& _name, const T _def) {
82 
83  return request_property<std::vector<BaseProperty*>,CellPropertyT<T>,CellPropHandle,T>(cell_props_, _name, n_cells(), _def);
84 }
85 
86 template<class T>
87 MeshPropertyT<T> ResourceManager::request_mesh_property(const std::string& _name, const T _def) {
88 
89  return request_property<std::vector<BaseProperty*>,MeshPropertyT<T>,MeshPropHandle,T>(mesh_props_, _name, 1, _def);
90 }
91 
92 template<class StdVecT, class PropT, class HandleT, class T>
93 PropT ResourceManager::request_property(StdVecT& _vec, const std::string& _name, size_t _size, const T _def) {
94 
95  if(!_name.empty()) {
96  for(typename StdVecT::iterator it = _vec.begin();
97  it != _vec.end(); ++it) {
98  if((*it)->name() == _name) {
99 #if OVM_FORCE_STATIC_CAST
100  return *static_cast<PropT*>(*it);
101 #else
102  PropT* prop = dynamic_cast<PropT*>(*it);
103  if(prop != NULL) return *prop;
104 #endif
105  }
106  }
107  }
108 
109  HandleT handle(_vec.size());
110 
111  PropT* prop = new PropT(_name, *this, handle, _def);
112  prop->resize(_size);
113 
114  // Store property pointer
115  _vec.push_back(prop);
116 
117  return *prop;
118 }
119 
120 template<class T>
121 void ResourceManager::set_persistent(VertexPropertyT<T>& _prop, bool _flag) {
122 
123  set_persistentT(_prop, _flag);
124 }
125 
126 template<class T>
127 void ResourceManager::set_persistent(EdgePropertyT<T>& _prop, bool _flag) {
128 
129  set_persistentT(_prop, _flag);
130 }
131 
132 template<class T>
133 void ResourceManager::set_persistent(HalfEdgePropertyT<T>& _prop, bool _flag) {
134 
135  set_persistentT(_prop, _flag);
136 }
137 
138 template<class T>
139 void ResourceManager::set_persistent(FacePropertyT<T>& _prop, bool _flag) {
140 
141  set_persistentT(_prop, _flag);
142 }
143 
144 template<class T>
145 void ResourceManager::set_persistent(HalfFacePropertyT<T>& _prop, bool _flag) {
146 
147  set_persistentT(_prop, _flag);
148 }
149 
150 template<class T>
151 void ResourceManager::set_persistent(CellPropertyT<T>& _prop, bool _flag) {
152 
153  set_persistentT(_prop, _flag);
154 }
155 
156 template<class T>
157 void ResourceManager::set_persistent(MeshPropertyT<T>& _prop, bool _flag) {
158 
159  set_persistentT(_prop, _flag);
160 }
161 
162 template<class PropT>
163 void ResourceManager::set_persistentT(PropT& _prop, bool _flag) {
164 
165  if(_flag == _prop->persistent()) return;
166 
167  _prop->set_persistent(_flag);
168 }
169 
170 template<class StdVecT>
171 void ResourceManager::remove_property(StdVecT& _vec, size_t _idx) {
172 
173  (*(_vec.begin() + _idx))->lock();
174  delete *(_vec.begin() + _idx);
175  _vec.erase(_vec.begin() + _idx);
176  size_t n = _vec.size();
177  for(size_t i = 0; i < n; ++i) {
178  _vec[i]->set_handle(OpenVolumeMeshHandle(i));
179  }
180 }
181 
182 template<class StdVecT>
183 void ResourceManager::resize_props(StdVecT& _vec, size_t _n) {
184 
185  for(typename StdVecT::iterator it = _vec.begin();
186  it != _vec.end(); ++it) {
187  (*it)->resize(_n);
188  }
189 }
190 
191 template<class StdVecT>
192 void ResourceManager::entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle& _h) {
193 
194  for(typename StdVecT::iterator it = _vec.begin();
195  it != _vec.end(); ++it) {
196  (*it)->delete_element(_h.idx());
197  }
198 }
199 
200 template<class StdVecT>
201 void ResourceManager::clearVec(StdVecT& _vec) {
202 
203  StdVecT newVec;
204  for(typename StdVecT::iterator it = _vec.begin();
205  it != _vec.end(); ++it) {
206  if(!(*it)->persistent()) {
207 #ifndef NDEBUG
208  std::cerr << "Keeping property \"" << (*it)->name()
209  << "\" since it is still in use!" << std::endl;
210 #endif
211  (*it)->resize(0);
212  newVec.push_back(*it);
213  }
214  else
215  delete *it;
216  }
217 
218  _vec = newVec;
219 }
220 
221 } // Namespace OpenVolumeMesh
virtual size_t n_faces() const =0
Get number of faces in mesh.
virtual size_t n_cells() const =0
Get number of cells in mesh.
virtual size_t n_edges() const =0
Get number of edges in mesh.
virtual size_t n_vertices() const =0
Get number of vertices in mesh.