| OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PropertyManager.hh
1 /* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openmesh.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenMesh. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 * ========================================================================= */
41
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $Date$ *
46 * *
47 \*===========================================================================*/
48
49 #ifndef PROPERTYMANAGER_HH_
50 #define PROPERTYMANAGER_HH_
51
52 #include <sstream>
53 #include <stdexcept>
54 #include <string>
55
56 namespace OpenMesh {
57
77 template<typename PROPTYPE, typename MeshT>
78 class PropertyManager {
79 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
80 public:
81 PropertyManager(const PropertyManager&) = delete;
82 PropertyManager& operator=(const PropertyManager&) = delete;
83 #else
84 private:
88 PropertyManager(const PropertyManager&);
89
93 PropertyManager& operator=(const PropertyManager&);
94 #endif
95
96 public:
111 PropertyManager(MeshT &mesh, const char *propname, bool existing = false) : mesh_(&mesh), retain_(existing), name_(propname) {
112 if (existing) {
113 if (!mesh_->get_property_handle(prop_, propname)) {
114 std::ostringstream oss;
115 oss << "Requested property handle \"" << propname << "\" does not exist.";
116 throw std::runtime_error(oss.str());
117 }
118 } else {
119 mesh_->add_property(prop_, propname);
120 }
121 }
122
123 PropertyManager() : mesh_(0), retain_(false) {
124 }
125
126 ~PropertyManager() {
127 deleteProperty();
128 }
129
130 void swap(PropertyManager &rhs) {
131 std::swap(mesh_, rhs.mesh_);
132 std::swap(prop_, rhs.prop_);
133 std::swap(retain_, rhs.retain_);
134 std::swap(name_, rhs.name_);
135 }
136
137 static bool propertyExists(MeshT &mesh, const char *propname) {
138 PROPTYPE dummy;
139 return mesh.get_property_handle(dummy, propname);
140 }
141
142 bool isValid() const { return mesh_ != 0; }
143 operator bool() const { return isValid(); }
144
145 const PROPTYPE &getRawProperty() const { return prop_; }
146
147 const std::string &getName() const { return name_; }
148
149 MeshT &getMesh() const { return *mesh_; }
150
151 #if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
152
154 typedef PropertyManager<PROPTYPE, MeshT> Proxy;
155
159 PropertyManager(PropertyManager &&rhs) : mesh_(rhs.mesh_), prop_(rhs.prop_), retain_(rhs.retain_), name_(rhs.name_) {
160 rhs.retain_ = true;
161 }
162
166 PropertyManager &operator=(PropertyManager &&rhs) {
167
168 deleteProperty();
169
170 mesh_ = rhs.mesh_;
171 prop_ = rhs.prop_;
172 retain_ = rhs.retain_;
173 name_ = rhs.name_;
174 rhs.retain_ = true;
175
176 return *this;
177 }
178
184 static PropertyManager createIfNotExists(MeshT &mesh, const char *propname) {
185 PROPTYPE dummy_prop;
186 PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
187 pm.retain();
188 return std::move(pm);
189 }
190
191
192 PropertyManager duplicate(const char *clone_name) {
193 PropertyManager pm(*mesh_, clone_name, false);
194 pm.mesh_->property(pm.prop_) = mesh_->property(prop_);
195 return std::move(pm);
196 }
197
201 PropertyManager move() {
202 return std::move(*this);
203 }
204
205 #else
206 class Proxy {
207 private:
208 Proxy(MeshT *mesh_, PROPTYPE prop_, bool retain_, const std::string &name_) :
209 mesh_(mesh_), prop_(prop_), retain_(retain_), name_(name_) {}
210 MeshT *mesh_;
211 PROPTYPE prop_;
212 bool retain_;
213 std::string name_;
214
215 friend class PropertyManager;
216 };
217
218 operator Proxy() {
219 Proxy p(mesh_, prop_, retain_, name_);
220 mesh_ = 0;
221 retain_ = true;
222 return p;
223 }
224
225 Proxy move() {
226 return (Proxy)*this;
227 }
228
229 PropertyManager(Proxy p) : mesh_(p.mesh_), prop_(p.prop_), retain_(p.retain_), name_(p.name_) {}
230
231 PropertyManager &operator=(Proxy p) {
232 PropertyManager(p).swap(*this);
233 return *this;
234 }
235
241 static Proxy createIfNotExists(MeshT &mesh, const char *propname) {
242 PROPTYPE dummy_prop;
243 PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
244 pm.retain();
245 return (Proxy)pm;
246 }
247
248 Proxy duplicate(const char *clone_name) {
249 PropertyManager pm(*mesh_, clone_name, false);
250 pm.mesh_->property(pm.prop_) = mesh_->property(prop_);
251 return (Proxy)pm;
252 }
253 #endif
254
261 inline void retain(bool doRetain = true) {
262 retain_ = doRetain;
263 }
264
268 inline PROPTYPE &operator* () {
269 return prop_;
270 }
271
275 inline const PROPTYPE &operator* () const {
276 return prop_;
277 }
278
286 template<typename HandleType>
287 inline typename PROPTYPE::reference operator[] (const HandleType &handle) {
288 return mesh_->property(prop_, handle);
289 }
290
298 template<typename HandleType>
299 inline typename PROPTYPE::const_reference operator[] (const HandleType &handle) const {
300 return mesh_->property(prop_, handle);
301 }
302
327 template<typename HandleTypeIterator, typename PROP_VALUE>
328 void set_range(HandleTypeIterator begin, HandleTypeIterator end,
329 const PROP_VALUE &value) {
330 for (; begin != end; ++begin)
331 (*this)[*begin] = value;
332 }
333
348 template<typename HandleTypeIterator, typename PROPTYPE_2,
349 typename MeshT_2, typename HandleTypeIterator_2>
350 void copy_to(HandleTypeIterator begin, HandleTypeIterator end,
351 PropertyManager<PROPTYPE_2, MeshT_2> &dst_propmanager,
352 HandleTypeIterator_2 dst_begin, HandleTypeIterator_2 dst_end) const {
353
354 for (; begin != end && dst_begin != dst_end; ++begin, ++dst_begin) {
355 dst_propmanager[*dst_begin] = (*this)[*begin];
356 }
357 }
358
359 template<typename RangeType, typename PROPTYPE_2,
360 typename MeshT_2, typename RangeType_2>
361 void copy_to(const RangeType &range,
362 PropertyManager<PROPTYPE_2, MeshT_2> &dst_propmanager,
363 const RangeType_2 &dst_range) const {
364 copy_to(range.begin(), range.end(), dst_propmanager,
365 dst_range.begin(), dst_range.end());
366 }
367
382 template<typename RangeType, typename MeshT_2, typename RangeType_2>
383 static void copy(const char *prop_name,
384 MeshT &src_mesh, const RangeType &src_range,
385 MeshT_2 &dst_mesh, const RangeType_2 &dst_range) {
386
387 typedef OpenMesh::PropertyManager<PROPTYPE, MeshT> DstPM;
388 DstPM dst(DstPM::createIfNotExists(dst_mesh, prop_name));
389
390 typedef OpenMesh::PropertyManager<PROPTYPE, MeshT_2> SrcPM;
391 SrcPM src(src_mesh, prop_name, true);
392
393 src.copy_to(src_range, dst, dst_range);
394 }
395
396 private:
397 void deleteProperty() {
398 if (!retain_)
399 mesh_->remove_property(prop_);
400 }
401
402 private:
403 MeshT *mesh_;
404 PROPTYPE prop_;
405 bool retain_;
406 std::string name_;
407 };
408
409 } /* namespace OpenMesh */
410 #endif /* PROPERTYMANAGER_HH_ */
OpenMesh::PropertyManager::set_range
void set_range(HandleTypeIterator begin, HandleTypeIterator end, const PROP_VALUE &value)
Conveniently set the property for an entire range of values.
Definition: PropertyManager.hh:328
OpenMesh::PropertyManager::operator*
PROPTYPE & operator*()
Access the encapsulated property.
Definition: PropertyManager.hh:268
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
OpenMesh::PropertyManager::copy
static void copy(const char *prop_name, MeshT &src_mesh, const RangeType &src_range, MeshT_2 &dst_mesh, const RangeType_2 &dst_range)
Copy the values of a property from a source range to a target range.
Definition: PropertyManager.hh:383
OpenMesh::PropertyManager
This class is intended to manage the lifecycle of properties.
Definition: PropertyManager.hh:78
OpenMesh::PropertyManager::operator[]
PROPTYPE::reference operator[](const HandleType &handle)
Enables convenient access to the encapsulated property.
Definition: PropertyManager.hh:287
OpenMesh::PropertyManager::retain
void retain(bool doRetain=true)
Disable lifecycle management for this property.
Definition: PropertyManager.hh:261
OpenMesh::PropertyManager::PropertyManager
PropertyManager(MeshT &mesh, const char *propname, bool existing=false)
Constructor.
Definition: PropertyManager.hh:111
OpenMesh::PropertyManager::copy_to
void copy_to(HandleTypeIterator begin, HandleTypeIterator end, PropertyManager< PROPTYPE_2, MeshT_2 > &dst_propmanager, HandleTypeIterator_2 dst_begin, HandleTypeIterator_2 dst_end) const
Conveniently transfer the values managed by one property manager onto the values managed by a differe...
Definition: PropertyManager.hh:350
OpenMesh::PropertyManager::createIfNotExists
static Proxy createIfNotExists(MeshT &mesh, const char *propname)
Create a property manager for the supplied property and mesh.
Definition: PropertyManager.hh:241