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 
187 #else
188  class Proxy {
189  private:
190  Proxy(MeshT *mesh_, PROPTYPE prop_, bool retain_, const std::string &name_) :
191  mesh_(mesh_), prop_(prop_), retain_(retain_), name_(name_) {}
192  MeshT *mesh_;
193  PROPTYPE prop_;
194  bool retain_;
195  std::string name_;
196 
197  friend class PropertyManager;
198  };
199 
200  operator Proxy() {
201  Proxy p(mesh_, prop_, retain_, name_);
202  mesh_ = 0;
203  retain_ = true;
204  return p;
205  }
206 
207  PropertyManager(Proxy p) : mesh_(p.mesh_), prop_(p.prop_), retain_(p.retain_), name_(p.name_) {}
208 
209  PropertyManager &operator=(Proxy p) {
210  PropertyManager(p).swap(*this);
211  return *this;
212  }
213 
219  static Proxy createIfNotExists(MeshT &mesh, const char *propname) {
220  PROPTYPE dummy_prop;
221  PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
222  pm.retain();
223  return (Proxy)pm;
224  }
225 
226  Proxy duplicate(const char *clone_name) {
227  PropertyManager pm(*mesh_, clone_name, false);
228  pm.mesh_->property(pm.prop_) = mesh_->property(prop_);
229  return (Proxy)pm;
230  }
231 #endif
232 
239  inline void retain(bool doRetain = true) {
240  retain_ = doRetain;
241  }
242 
246  inline PROPTYPE &operator* () {
247  return prop_;
248  }
249 
253  inline const PROPTYPE &operator* () const {
254  return prop_;
255  }
256 
264  template<typename HandleType>
265  inline typename PROPTYPE::reference operator[] (const HandleType &handle) {
266  return mesh_->property(prop_, handle);
267  }
268 
276  template<typename HandleType>
277  inline typename PROPTYPE::const_reference operator[] (const HandleType &handle) const {
278  return mesh_->property(prop_, handle);
279  }
280 
305  template<typename HandleTypeIterator>
306  void set_range(HandleTypeIterator begin, HandleTypeIterator end,
307  typename PROPTYPE::const_reference value) {
308  for (; begin != end; ++begin)
309  (*this)[*begin] = value;
310  }
311 
312  private:
313  void deleteProperty() {
314  if (!retain_)
315  mesh_->remove_property(prop_);
316  }
317 
318  private:
319  MeshT *mesh_;
320  PROPTYPE prop_;
321  bool retain_;
322  std::string name_;
323 };
324 
325 } /* namespace OpenMesh */
326 #endif /* PROPERTYMANAGER_HH_ */
PropertyManager(MeshT &mesh, const char *propname, bool existing=false)
Constructor.
Definition: PropertyManager.hh:104
Definition: PropertyManager.hh:188
PROPTYPE::reference operator[](const HandleType &handle)
Enables convenient access to the encapsulated property.
Definition: PropertyManager.hh:265
static Proxy createIfNotExists(MeshT &mesh, const char *propname)
Create a property manager for the supplied property and mesh.
Definition: PropertyManager.hh:219
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:306
PROPTYPE & operator*()
Access the encapsulated property.
Definition: PropertyManager.hh:246
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:239

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