OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PropertyManager.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2012 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 
47 namespace OpenMesh {
48 
68 template<typename PROPTYPE, typename MeshT>
70  private:
75 
79  const PropertyManager& operator=(const PropertyManager&);
80 
81  public:
96  PropertyManager(MeshT &mesh, const char *propname, bool existing = false) : mesh_(&mesh), retain_(existing) {
97  if (existing) {
98  if (!mesh_->get_property_handle(prop_, propname)) {
99  std::ostringstream oss;
100  oss << "Requested property handle \"" << propname << "\" does not exist.";
101  throw std::runtime_error(oss.str());
102  }
103  } else {
104  mesh_->add_property(prop_, propname);
105  }
106  }
107 
108  ~PropertyManager() {
109  deleteProperty();
110  }
111 
112  void swap(PropertyManager &rhs) {
113  std::swap(mesh_, rhs.mesh_);
114  std::swap(prop_, rhs.prop_);
115  std::swap(retain_, rhs.retain_);
116  }
117 
118 #if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__
119 
122  PropertyManager(PropertyManager &&rhs) : mesh_(rhs.mesh_), prop_(rhs.prop_), retain_(rhs.retain_) {
123  rhs.retain_ = true;
124  }
125 
129  PropertyManager &operator=(PropertyManager &&rhs) {
130 
131  deleteProperty();
132 
133  mesh_ = rhs.mesh_;
134  prop_ = rhs.prop_;
135  retain_ = rhs.retain_;
136  rhs.retain_ = true;
137 
138  return *this;
139  }
140 
146  static PropertyManager createIfNotExists(MeshT &mesh, const char *propname) {
147  PROPTYPE dummy_prop;
148  PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
149  pm.retain();
150  return std::move(pm);
151  }
152 
153 #else
154  class Proxy {
155  private:
156  Proxy(MeshT *mesh_, PROPTYPE prop_, bool retain_) :
157  mesh_(mesh_), prop_(prop_), retain_(retain_) {}
158  MeshT *mesh_;
159  PROPTYPE prop_;
160  bool retain_;
161 
162  friend class PropertyManager;
163  };
164 
165  operator Proxy() {
166  Proxy p(mesh_, prop_, retain_);
167  mesh_ = 0;
168  retain_ = true;
169  return p;
170  }
171 
172  PropertyManager(Proxy p) : mesh_(p.mesh_), prop_(p.prop_), retain_(p.retain_) {}
173 
174  PropertyManager &operator=(Proxy p) {
175  PropertyManager(p).swap(*this);
176  return *this;
177  }
178 
184  static Proxy 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 (Proxy)pm;
189  }
190 #endif
191 
198  inline void retain(bool doRetain = true) {
199  retain_ = doRetain;
200  }
201 
205  inline PROPTYPE &operator* () {
206  return prop_;
207  }
208 
212  inline const PROPTYPE &operator* () const {
213  return prop_;
214  }
215 
223  template<typename HandleType>
224  inline typename PROPTYPE::reference operator[] (const HandleType &handle) {
225  return mesh_->property(prop_, handle);
226  }
227 
235  template<typename HandleType>
236  inline typename PROPTYPE::const_reference operator[] (const HandleType &handle) const {
237  return mesh_->property(prop_, handle);
238  }
239 
240  private:
241  void deleteProperty() {
242  if (!retain_)
243  mesh_->remove_property(prop_);
244  }
245 
246  private:
247  MeshT *mesh_;
248  PROPTYPE prop_;
249  bool retain_;
250 };
251 
252 } /* namespace OpenMesh */
253 #endif /* PROPERTYMANAGER_HH_ */

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