OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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 #include <stdexcept>
47
48 namespace OpenMesh {
49
69 template<typename PROPTYPE, typename MeshT>
70 class PropertyManager {
71 private:
75 PropertyManager(const PropertyManager&);
76
80 const PropertyManager& operator=(const PropertyManager&);
81
82 public:
97 PropertyManager(MeshT &mesh, const char *propname, bool existing = false) : mesh_(&mesh), retain_(existing) {
98 if (existing) {
99 if (!mesh_->get_property_handle(prop_, propname)) {
100 std::ostringstream oss;
101 oss << "Requested property handle \"" << propname << "\" does not exist.";
102 throw std::runtime_error(oss.str());
103 }
104 } else {
105 mesh_->add_property(prop_, propname);
106 }
107 }
108
109 PropertyManager() : mesh_(0), retain_(false) {
110 }
111
112 ~PropertyManager() {
113 deleteProperty();
114 }
115
116 void swap(PropertyManager &rhs) {
117 std::swap(mesh_, rhs.mesh_);
118 std::swap(prop_, rhs.prop_);
119 std::swap(retain_, rhs.retain_);
120 }
121
122 static bool propertyExists(MeshT &mesh, const char *propname) {
123 PROPTYPE dummy;
124 return mesh.get_property_handle(dummy, propname);
125 }
126
127 bool isValid() const { return mesh_ != 0; }
128 operator bool() const { return isValid(); }
129
130 const PROPTYPE &getRawProperty() const { return prop_; }
131
132 #if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__
133
136 PropertyManager(PropertyManager &&rhs) : mesh_(rhs.mesh_), prop_(rhs.prop_), retain_(rhs.retain_) {
137 rhs.retain_ = true;
138 }
139
143 PropertyManager &operator=(PropertyManager &&rhs) {
144
145 deleteProperty();
146
147 mesh_ = rhs.mesh_;
148 prop_ = rhs.prop_;
149 retain_ = rhs.retain_;
150 rhs.retain_ = true;
151
152 return *this;
153 }
154
160 static PropertyManager createIfNotExists(MeshT &mesh, const char *propname) {
161 PROPTYPE dummy_prop;
162 PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
163 pm.retain();
164 return std::move(pm);
165 }
166
167 #else
168 class Proxy {
169 private:
170 Proxy(MeshT *mesh_, PROPTYPE prop_, bool retain_) :
171 mesh_(mesh_), prop_(prop_), retain_(retain_) {}
172 MeshT *mesh_;
173 PROPTYPE prop_;
174 bool retain_;
175
176 friend class PropertyManager;
177 };
178
179 operator Proxy() {
180 Proxy p(mesh_, prop_, retain_);
181 mesh_ = 0;
182 retain_ = true;
183 return p;
184 }
185
186 PropertyManager(Proxy p) : mesh_(p.mesh_), prop_(p.prop_), retain_(p.retain_) {}
187
188 PropertyManager &operator=(Proxy p) {
189 PropertyManager(p).swap(*this);
190 return *this;
191 }
192
198 static Proxy createIfNotExists(MeshT &mesh, const char *propname) {
199 PROPTYPE dummy_prop;
200 PropertyManager pm(mesh, propname, mesh.get_property_handle(dummy_prop, propname));
201 pm.retain();
202 return (Proxy)pm;
203 }
204 #endif
205
212 inline void retain(bool doRetain = true) {
213 retain_ = doRetain;
214 }
215
219 inline PROPTYPE &operator* () {
220 return prop_;
221 }
222
226 inline const PROPTYPE &operator* () const {
227 return prop_;
228 }
229
237 template<typename HandleType>
238 inline typename PROPTYPE::reference operator[] (const HandleType &handle) {
239 return mesh_->property(prop_, handle);
240 }
241
249 template<typename HandleType>
250 inline typename PROPTYPE::const_reference operator[] (const HandleType &handle) const {
251 return mesh_->property(prop_, handle);
252 }
253
254 private:
255 void deleteProperty() {
256 if (!retain_)
257 mesh_->remove_property(prop_);
258 }
259
260 private:
261 MeshT *mesh_;
262 PROPTYPE prop_;
263 bool retain_;
264 };
265
266 } /* namespace OpenMesh */
267 #endif /* PROPERTYMANAGER_HH_ */