OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PropertyManager.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2015 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh 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  * OpenMesh 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 OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision$ *
38  * $Date$ *
39  * *
40 \*===========================================================================*/
41 
42 #ifndef PROPERTYMANAGER_HH_
43 #define PROPERTYMANAGER_HH_
44 
45 #include <sstream>
46 #include <stdexcept>
47 #include <string>
48 
49 namespace OpenMesh {
50 
70 template<typename PROPTYPE, typename MeshT>
72 #if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__
73  public:
74  PropertyManager(const PropertyManager&) = delete;
75  PropertyManager& operator=(const PropertyManager&) = delete;
76 #else
77  private:
82 
86  PropertyManager& operator=(const PropertyManager&);
87 #endif
88 
89  public:
104  PropertyManager(MeshT &mesh, const char *propname, bool existing = false) : mesh_(&mesh), retain_(existing), name_(propname) {
105  if (existing) {
106  if (!mesh_->get_property_handle(prop_, propname)) {
107  std::ostringstream oss;
108  oss << "Requested property handle \"" << propname << "\" does not exist.";
109  throw std::runtime_error(oss.str());
110  }
111  } else {
112  mesh_->add_property(prop_, propname);
113  }
114  }
115 
116  PropertyManager() : mesh_(0), retain_(false) {
117  }
118 
119  ~PropertyManager() {
120  deleteProperty();
121  }
122 
123  void swap(PropertyManager &rhs) {
124  std::swap(mesh_, rhs.mesh_);
125  std::swap(prop_, rhs.prop_);
126  std::swap(retain_, rhs.retain_);
127  std::swap(name_, rhs.name_);
128  }
129 
130  static bool propertyExists(MeshT &mesh, const char *propname) {
131  PROPTYPE dummy;
132  return mesh.get_property_handle(dummy, propname);
133  }
134 
135  bool isValid() const { return mesh_ != 0; }
136  operator bool() const { return isValid(); }
137 
138  const PROPTYPE &getRawProperty() const { return prop_; }
139 
140  const std::string &getName() const { return name_; }
141 
142  MeshT &getMesh() const { return *mesh_; }
143 
144 #if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__
145 
148  PropertyManager(PropertyManager &&rhs) : mesh_(rhs.mesh_), prop_(rhs.prop_), retain_(rhs.retain_), name_(rhs.name_) {
149  rhs.retain_ = true;
150  }
151 
155  PropertyManager &operator=(PropertyManager &&rhs) {
156 
157  deleteProperty();
158 
159  mesh_ = rhs.mesh_;
160  prop_ = rhs.prop_;
161  retain_ = rhs.retain_;
162  name_ = rhs.name_;
163  rhs.retain_ = true;
164 
165  return *this;
166  }
167 
173  static PropertyManager createIfNotExists(MeshT &mesh, const char *propname) {
174  PROPTYPE dummy_prop;
175  PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
176  pm.retain();
177  return std::move(pm);
178  }
179 
180 
181  PropertyManager duplicate(const char *clone_name) {
182  PropertyManager pm(*mesh_, clone_name, false);
183  pm.mesh_->property(pm.prop_) = mesh_->property(prop_);
184  return std::move(pm);
185  }
186 
190  PropertyManager move() {
191  return std::move(*this);
192  }
193 
194 #else
195  class Proxy {
196  private:
197  Proxy(MeshT *mesh_, PROPTYPE prop_, bool retain_, const std::string &name_) :
198  mesh_(mesh_), prop_(prop_), retain_(retain_), name_(name_) {}
199  MeshT *mesh_;
200  PROPTYPE prop_;
201  bool retain_;
202  std::string name_;
203 
204  friend class PropertyManager;
205  };
206 
207  operator Proxy() {
208  Proxy p(mesh_, prop_, retain_, name_);
209  mesh_ = 0;
210  retain_ = true;
211  return p;
212  }
213 
214  Proxy move() {
215  return (Proxy)*this;
216  }
217 
218  PropertyManager(Proxy p) : mesh_(p.mesh_), prop_(p.prop_), retain_(p.retain_), name_(p.name_) {}
219 
220  PropertyManager &operator=(Proxy p) {
221  PropertyManager(p).swap(*this);
222  return *this;
223  }
224 
230  static Proxy createIfNotExists(MeshT &mesh, const char *propname) {
231  PROPTYPE dummy_prop;
232  PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
233  pm.retain();
234  return (Proxy)pm;
235  }
236 
237  Proxy duplicate(const char *clone_name) {
238  PropertyManager pm(*mesh_, clone_name, false);
239  pm.mesh_->property(pm.prop_) = mesh_->property(prop_);
240  return (Proxy)pm;
241  }
242 #endif
243 
250  inline void retain(bool doRetain = true) {
251  retain_ = doRetain;
252  }
253 
257  inline PROPTYPE &operator* () {
258  return prop_;
259  }
260 
264  inline const PROPTYPE &operator* () const {
265  return prop_;
266  }
267 
275  template<typename HandleType>
276  inline typename PROPTYPE::reference operator[] (const HandleType &handle) {
277  return mesh_->property(prop_, handle);
278  }
279 
287  template<typename HandleType>
288  inline typename PROPTYPE::const_reference operator[] (const HandleType &handle) const {
289  return mesh_->property(prop_, handle);
290  }
291 
316  template<typename HandleTypeIterator>
317  void set_range(HandleTypeIterator begin, HandleTypeIterator end,
318  typename PROPTYPE::const_reference value) {
319  for (; begin != end; ++begin)
320  (*this)[*begin] = value;
321  }
322 
337  template<typename HandleTypeIterator, typename PROPTYPE_2,
338  typename MeshT_2, typename HandleTypeIterator_2>
339  void copy_to(HandleTypeIterator begin, HandleTypeIterator end,
340  PropertyManager<PROPTYPE_2, MeshT_2> &dst_propmanager,
341  HandleTypeIterator_2 dst_begin, HandleTypeIterator_2 dst_end) const {
342 
343  for (; begin != end && dst_begin != dst_end; ++begin, ++dst_begin) {
344  dst_propmanager[*dst_begin] = (*this)[*begin];
345  }
346  }
347 
348  template<typename RangeType, typename PROPTYPE_2,
349  typename MeshT_2, typename RangeType_2>
350  void copy_to(const RangeType &range,
351  PropertyManager<PROPTYPE_2, MeshT_2> &dst_propmanager,
352  const RangeType_2 &dst_range) const {
353  copy_to(range.begin(), range.end(), dst_propmanager,
354  dst_range.begin(), dst_range.end());
355  }
356 
371  template<typename RangeType, typename MeshT_2, typename RangeType_2>
372  static void copy(const char *prop_name,
373  MeshT &src_mesh, const RangeType &src_range,
374  MeshT_2 &dst_mesh, const RangeType_2 &dst_range) {
375 
377  DstPM dst(DstPM::createIfNotExists(dst_mesh, prop_name));
378 
380  SrcPM src(src_mesh, prop_name, true);
381 
382  src.copy_to(src_range, dst, dst_range);
383  }
384 
385  private:
386  void deleteProperty() {
387  if (!retain_)
388  mesh_->remove_property(prop_);
389  }
390 
391  private:
392  MeshT *mesh_;
393  PROPTYPE prop_;
394  bool retain_;
395  std::string name_;
396 };
397 
398 } /* namespace OpenMesh */
399 #endif /* PROPERTYMANAGER_HH_ */
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:372
PROPTYPE::reference operator[](const HandleType &handle)
Enables convenient access to the encapsulated property.
Definition: PropertyManager.hh:276
This class is intended to manage the lifecycle of properties.
Definition: PropertyManager.hh:71
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:339
PropertyManager(MeshT &mesh, const char *propname, bool existing=false)
Constructor.
Definition: PropertyManager.hh:104
void set_range(HandleTypeIterator begin, HandleTypeIterator end, typename PROPTYPE::const_reference value)
Conveniently set the property for an entire range of values.
Definition: PropertyManager.hh:317
Definition: PropertyManager.hh:195
void retain(bool doRetain=true)
Disable lifecycle management for this property.
Definition: PropertyManager.hh:250
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:56
PROPTYPE & operator*()
Access the encapsulated property.
Definition: PropertyManager.hh:257
static Proxy createIfNotExists(MeshT &mesh, const char *propname)
Create a property manager for the supplied property and mesh.
Definition: PropertyManager.hh:230

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .