OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PropertyManager.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2014 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 
323  private:
324  void deleteProperty() {
325  if (!retain_)
326  mesh_->remove_property(prop_);
327  }
328 
329  private:
330  MeshT *mesh_;
331  PROPTYPE prop_;
332  bool retain_;
333  std::string name_;
334 };
335 
336 } /* namespace OpenMesh */
337 #endif /* PROPERTYMANAGER_HH_ */
PropertyManager(MeshT &mesh, const char *propname, bool existing=false)
Constructor.
Definition: PropertyManager.hh:104
Definition: PropertyManager.hh:195
PROPTYPE::reference operator[](const HandleType &handle)
Enables convenient access to the encapsulated property.
Definition: PropertyManager.hh:276
static Proxy createIfNotExists(MeshT &mesh, const char *propname)
Create a property manager for the supplied property and mesh.
Definition: PropertyManager.hh:230
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
PROPTYPE & operator*()
Access the encapsulated property.
Definition: PropertyManager.hh:257
This class is intended to manage the lifecycle of properties.
Definition: PropertyManager.hh:71
void retain(bool doRetain=true)
Disable lifecycle management for this property.
Definition: PropertyManager.hh:250

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