| OpenMesh

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PropertyContainer.hh
1 /* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openmesh.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenMesh. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 * ========================================================================= */
41
42 /*===========================================================================*\
43 * *
44 * $Revision: 1258 $ *
45 * $Date: 2015-04-28 15:07:46 +0200 (Di, 28 Apr 2015) $ *
46 * *
47 \*===========================================================================*/
48
49 #ifndef OPENMESH_PROPERTYCONTAINER
50 #define OPENMESH_PROPERTYCONTAINER
51
52 // Use static casts when not debugging
53 #ifdef NDEBUG
54 #define OM_FORCE_STATIC_CAST
55 #endif
56
57 #include <OpenMesh/Core/Utils/Property.hh>
58
59 //-----------------------------------------------------------------------------
60 namespace OpenMesh
61 {
62 //== FORWARDDECLARATIONS ======================================================
63 class BaseKernel;
64
65 //== CLASS DEFINITION =========================================================
67 class PropertyContainer
68 {
69 public:
70
71 //-------------------------------------------------- constructor / destructor
72
73 PropertyContainer() {}
74 virtual ~PropertyContainer() { std::for_each(properties_.begin(), properties_.end(), Delete()); }
75
76
77 //------------------------------------------------------------- info / access
78
79 typedef std::vector<BaseProperty*> Properties;
80 const Properties& properties() const { return properties_; }
81 size_t size() const { return properties_.size(); }
82
83
84
85 //--------------------------------------------------------- copy / assignment
86
87 PropertyContainer(const PropertyContainer& _rhs) { operator=(_rhs); }
88
89 PropertyContainer& operator=(const PropertyContainer& _rhs)
90 {
91 // The assignment below relies on all previous BaseProperty* elements having been deleted
92 std::for_each(properties_.begin(), properties_.end(), Delete());
93 properties_ = _rhs.properties_;
94 Properties::iterator p_it=properties_.begin(), p_end=properties_.end();
95 for (; p_it!=p_end; ++p_it)
96 if (*p_it)
97 *p_it = (*p_it)->clone();
98 return *this;
99 }
100
101
102
103 //--------------------------------------------------------- manage properties
104
105 template <class T>
106 BasePropHandleT<T> add(const T&, const std::string& _name="<unknown>")
107 {
108 Properties::iterator p_it=properties_.begin(), p_end=properties_.end();
109 int idx=0;
110 for ( ; p_it!=p_end && *p_it!=NULL; ++p_it, ++idx ) {};
111 if (p_it==p_end) properties_.push_back(NULL);
112 properties_[idx] = new PropertyT<T>(_name);
113 return BasePropHandleT<T>(idx);
114 }
115
116
117 template <class T>
118 BasePropHandleT<T> handle(const T&, const std::string& _name) const
119 {
120 Properties::const_iterator p_it = properties_.begin();
121 for (int idx=0; p_it != properties_.end(); ++p_it, ++idx)
122 {
123 if (*p_it != NULL &&
124 (*p_it)->name() == _name //skip deleted properties
125 // Skip type check
126 #ifndef OM_FORCE_STATIC_CAST
127 && dynamic_cast<PropertyT<T>*>(properties_[idx]) != NULL //check type
128 #endif
129 )
130 {
131 return BasePropHandleT<T>(idx);
132 }
133 }
134 return BasePropHandleT<T>();
135 }
136
137 BaseProperty* property( const std::string& _name ) const
138 {
139 Properties::const_iterator p_it = properties_.begin();
140 for (int idx=0; p_it != properties_.end(); ++p_it, ++idx)
141 {
142 if (*p_it != NULL && (*p_it)->name() == _name) //skip deleted properties
143 {
144 return *p_it;
145 }
146 }
147 return NULL;
148 }
149
150 template <class T> PropertyT<T>& property(BasePropHandleT<T> _h)
151 {
152 assert(_h.idx() >= 0 && _h.idx() < (int)properties_.size());
153 assert(properties_[_h.idx()] != NULL);
154 #ifdef OM_FORCE_STATIC_CAST
155 return *static_cast <PropertyT<T>*> (properties_[_h.idx()]);
156 #else
157 PropertyT<T>* p = dynamic_cast<PropertyT<T>*>(properties_[_h.idx()]);
158 assert(p != NULL);
159 return *p;
160 #endif
161 }
162
163
164 template <class T> const PropertyT<T>& property(BasePropHandleT<T> _h) const
165 {
166 assert(_h.idx() >= 0 && _h.idx() < (int)properties_.size());
167 assert(properties_[_h.idx()] != NULL);
168 #ifdef OM_FORCE_STATIC_CAST
169 return *static_cast<PropertyT<T>*>(properties_[_h.idx()]);
170 #else
171 PropertyT<T>* p = dynamic_cast<PropertyT<T>*>(properties_[_h.idx()]);
172 assert(p != NULL);
173 return *p;
174 #endif
175 }
176
177
178 template <class T> void remove(BasePropHandleT<T> _h)
179 {
180 assert(_h.idx() >= 0 && _h.idx() < (int)properties_.size());
181 delete properties_[_h.idx()];
182 properties_[_h.idx()] = NULL;
183 }
184
185
186 void clear()
187 {
188 // Clear properties vector:
189 // Replaced the old version with new one
190 // which performs a swap to clear values and
191 // deallocate memory.
192
193 // Old version (changed 22.07.09) {
194 // std::for_each(properties_.begin(), properties_.end(), Delete());
195 // }
196
197 std::for_each(properties_.begin(), properties_.end(), ClearAll());
198 }
199
200
201 //---------------------------------------------------- synchronize properties
202
203 void reserve(size_t _n) const {
204 std::for_each(properties_.begin(), properties_.end(), Reserve(_n));
205 }
206
207 void resize(size_t _n) const {
208 std::for_each(properties_.begin(), properties_.end(), Resize(_n));
209 }
210
211 void swap(size_t _i0, size_t _i1) const {
212 std::for_each(properties_.begin(), properties_.end(), Swap(_i0, _i1));
213 }
214
215
216
217 protected: // generic add/get
218
219 size_t _add( BaseProperty* _bp )
220 {
221 Properties::iterator p_it=properties_.begin(), p_end=properties_.end();
222 size_t idx=0;
223 for (; p_it!=p_end && *p_it!=NULL; ++p_it, ++idx) {};
224 if (p_it==p_end) properties_.push_back(NULL);
225 properties_[idx] = _bp;
226 return idx;
227 }
228
229 BaseProperty& _property( size_t _idx )
230 {
231 assert( _idx < properties_.size());
232 assert( properties_[_idx] != NULL);
233 BaseProperty *p = properties_[_idx];
234 assert( p != NULL );
235 return *p;
236 }
237
238 const BaseProperty& _property( size_t _idx ) const
239 {
240 assert( _idx < properties_.size());
241 assert( properties_[_idx] != NULL);
242 BaseProperty *p = properties_[_idx];
243 assert( p != NULL );
244 return *p;
245 }
246
247
248 typedef Properties::iterator iterator;
249 typedef Properties::const_iterator const_iterator;
250 iterator begin() { return properties_.begin(); }
251 iterator end() { return properties_.end(); }
252 const_iterator begin() const { return properties_.begin(); }
253 const_iterator end() const { return properties_.end(); }
254
255 friend class BaseKernel;
256
257 private:
258
259 //-------------------------------------------------- synchronization functors
260
261 #ifndef DOXY_IGNORE_THIS
262 struct Reserve
263 {
264 Reserve(size_t _n) : n_(_n) {}
265 void operator()(BaseProperty* _p) const { if (_p) _p->reserve(n_); }
266 size_t n_;
267 };
268
269 struct Resize
270 {
271 Resize(size_t _n) : n_(_n) {}
272 void operator()(BaseProperty* _p) const { if (_p) _p->resize(n_); }
273 size_t n_;
274 };
275
276 struct ClearAll
277 {
278 ClearAll() {}
279 void operator()(BaseProperty* _p) const { if (_p) _p->clear(); }
280 };
281
282 struct Swap
283 {
284 Swap(size_t _i0, size_t _i1) : i0_(_i0), i1_(_i1) {}
285 void operator()(BaseProperty* _p) const { if (_p) _p->swap(i0_, i1_); }
286 size_t i0_, i1_;
287 };
288
289 struct Delete
290 {
291 Delete() {}
292 void operator()(BaseProperty* _p) const { if (_p) delete _p; _p=NULL; }
293 };
294 #endif
295
296 Properties properties_;
297 };
298
299 }//namespace OpenMesh
300
301 #endif//OPENMESH_PROPERTYCONTAINER
302
OpenMesh::BaseProperty::swap
virtual void swap(size_t _i0, size_t _i1)=0
Let two elements swap their storage place.
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
OpenMesh::BaseProperty::clear
virtual void clear()=0
Clear all elements and free memory.
OpenMesh::BaseProperty::resize
virtual void resize(size_t _n)=0
Resize storage to hold n elements.
OpenMesh::BaseHandle::idx
int idx() const
Get the underlying index of this handle.
Definition: Handles.hh:74
OpenMesh::BaseProperty::reserve
virtual void reserve(size_t _n)=0
Reserve memory for n elements.
OpenMesh::BasePropHandleT
Base property handle.
Definition: Property.hh:471
OpenMesh::PropertyContainer
A a container for properties.
Definition: PropertyContainer.hh:67
OpenMesh::BaseProperty
Abstract class defining the basic interface of a dynamic property.
Definition: BaseProperty.hh:65
OpenMesh::BaseKernel
This class provides the basic property management like adding/removing properties and access to prope...
Definition: BaseKernel.hh:99
OpenMesh::PropertyT
Default property class for any type T.
Definition: Property.hh:94