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 #else
181  class Proxy {
182  private:
183  Proxy(MeshT *mesh_, PROPTYPE prop_, bool retain_, const std::string &name_) :
184  mesh_(mesh_), prop_(prop_), retain_(retain_), name_(name_) {}
185  MeshT *mesh_;
186  PROPTYPE prop_;
187  bool retain_;
188  std::string name_;
189 
190  friend class PropertyManager;
191  };
192 
193  operator Proxy() {
194  Proxy p(mesh_, prop_, retain_, name_);
195  mesh_ = 0;
196  retain_ = true;
197  return p;
198  }
199 
200  PropertyManager(Proxy p) : mesh_(p.mesh_), prop_(p.prop_), retain_(p.retain_), name_(p.name_) {}
201 
202  PropertyManager &operator=(Proxy p) {
203  PropertyManager(p).swap(*this);
204  return *this;
205  }
206 
212  static Proxy createIfNotExists(MeshT &mesh, const char *propname) {
213  PROPTYPE dummy_prop;
214  PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
215  pm.retain();
216  return (Proxy)pm;
217  }
218 #endif
219 
226  inline void retain(bool doRetain = true) {
227  retain_ = doRetain;
228  }
229 
233  inline PROPTYPE &operator* () {
234  return prop_;
235  }
236 
240  inline const PROPTYPE &operator* () const {
241  return prop_;
242  }
243 
251  template<typename HandleType>
252  inline typename PROPTYPE::reference operator[] (const HandleType &handle) {
253  return mesh_->property(prop_, handle);
254  }
255 
263  template<typename HandleType>
264  inline typename PROPTYPE::const_reference operator[] (const HandleType &handle) const {
265  return mesh_->property(prop_, handle);
266  }
267 
268  private:
269  void deleteProperty() {
270  if (!retain_)
271  mesh_->remove_property(prop_);
272  }
273 
274  private:
275  MeshT *mesh_;
276  PROPTYPE prop_;
277  bool retain_;
278  std::string name_;
279 };
280 
281 } /* namespace OpenMesh */
282 #endif /* PROPERTYMANAGER_HH_ */
Definition: PropertyManager.hh:181
PropertyManager(MeshT &mesh, const char *propname, bool existing=false)
Constructor.
Definition: PropertyManager.hh:104
PROPTYPE & operator*()
Access the encapsulated property.
Definition: PropertyManager.hh:233
PROPTYPE::reference operator[](const HandleType &handle)
Enables convenient access to the encapsulated property.
Definition: PropertyManager.hh:252
static Proxy createIfNotExists(MeshT &mesh, const char *propname)
Create a property manager for the supplied property and mesh.
Definition: PropertyManager.hh:212
void retain(bool doRetain=true)
Disable lifecycle management for this property.
Definition: PropertyManager.hh:226
This class is intended to manage the lifecycle of properties.
Definition: PropertyManager.hh:71

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