OpenMesh
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$ *
45  * $Date$ *
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 =========================================================
68 {
69 public:
70 
71  //-------------------------------------------------- constructor / destructor
72 
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 /*
204  * In C++11 an beyond we can introduce more efficient and more legible
205  * implementations of the following methods.
206  */
207 #if ((defined(_MSC_VER) && (_MSC_VER >= 1900)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(OPENMESH_VECTOR_LEGACY)
208 
211  void reserve(size_t _n) const {
212  std::for_each(properties_.begin(), properties_.end(),
213  [_n](BaseProperty* p) { if (p) p->reserve(_n); });
214  }
215 
219  void resize(size_t _n) const {
220  std::for_each(properties_.begin(), properties_.end(),
221  [_n](BaseProperty* p) { if (p) p->resize(_n); });
222  }
223 
232  void resize_if_smaller(size_t _n) const {
233  std::for_each(properties_.begin(), properties_.end(),
234  [_n](BaseProperty* p) { if (p && p->n_elements() < _n) p->resize(_n); });
235  }
236 
241  void swap(size_t _i0, size_t _i1) const {
242  std::for_each(properties_.begin(), properties_.end(),
243  [_i0, _i1](BaseProperty* p) { if (p) p->swap(_i0, _i1); });
244  }
245 #else
246 
249  void reserve(size_t _n) const {
250  std::for_each(properties_.begin(), properties_.end(), Reserve(_n));
251  }
252 
256  void resize(size_t _n) const {
257  std::for_each(properties_.begin(), properties_.end(), Resize(_n));
258  }
259 
268  void resize_if_smaller(size_t _n) const {
269  std::for_each(properties_.begin(), properties_.end(), ResizeIfSmaller(_n));
270  }
271 
276  void swap(size_t _i0, size_t _i1) const {
277  std::for_each(properties_.begin(), properties_.end(), Swap(_i0, _i1));
278  }
279 #endif
280 
281 
282 
283 protected: // generic add/get
284 
285  size_t _add( BaseProperty* _bp )
286  {
287  Properties::iterator p_it=properties_.begin(), p_end=properties_.end();
288  size_t idx=0;
289  for (; p_it!=p_end && *p_it!=NULL; ++p_it, ++idx) {};
290  if (p_it==p_end) properties_.push_back(NULL);
291  properties_[idx] = _bp;
292  return idx;
293  }
294 
295  BaseProperty& _property( size_t _idx )
296  {
297  assert( _idx < properties_.size());
298  assert( properties_[_idx] != NULL);
299  BaseProperty *p = properties_[_idx];
300  assert( p != NULL );
301  return *p;
302  }
303 
304  const BaseProperty& _property( size_t _idx ) const
305  {
306  assert( _idx < properties_.size());
307  assert( properties_[_idx] != NULL);
308  BaseProperty *p = properties_[_idx];
309  assert( p != NULL );
310  return *p;
311  }
312 
313 
314  typedef Properties::iterator iterator;
315  typedef Properties::const_iterator const_iterator;
316  iterator begin() { return properties_.begin(); }
317  iterator end() { return properties_.end(); }
318  const_iterator begin() const { return properties_.begin(); }
319  const_iterator end() const { return properties_.end(); }
320 
321  friend class BaseKernel;
322 
323 private:
324 
325  //-------------------------------------------------- synchronization functors
326 
327 #ifndef DOXY_IGNORE_THIS
328  struct Reserve
329  {
330  Reserve(size_t _n) : n_(_n) {}
331  void operator()(BaseProperty* _p) const { if (_p) _p->reserve(n_); }
332  size_t n_;
333  };
334 
335  struct Resize
336  {
337  Resize(size_t _n) : n_(_n) {}
338  void operator()(BaseProperty* _p) const { if (_p) _p->resize(n_); }
339  size_t n_;
340  };
341 
342  struct ResizeIfSmaller
343  {
344  ResizeIfSmaller(size_t _n) : n_(_n) {}
345  void operator()(BaseProperty* _p) const { if (_p && _p->n_elements() < n_) _p->resize(n_); }
346  size_t n_;
347  };
348 
349  struct ClearAll
350  {
351  ClearAll() {}
352  void operator()(BaseProperty* _p) const { if (_p) _p->clear(); }
353  };
354 
355  struct Swap
356  {
357  Swap(size_t _i0, size_t _i1) : i0_(_i0), i1_(_i1) {}
358  void operator()(BaseProperty* _p) const { if (_p) _p->swap(i0_, i1_); }
359  size_t i0_, i1_;
360  };
361 
362  struct Delete
363  {
364  Delete() {}
365  void operator()(BaseProperty* _p) const { if (_p) delete _p; _p=NULL; }
366  };
367 #endif
368 
369  Properties properties_;
370 };
371 
372 }//namespace OpenMesh
373 
374 #endif//OPENMESH_PROPERTYCONTAINER
375 
void resize_if_smaller(size_t _n) const
Same as.
Definition: PropertyContainer.hh:268
void swap(size_t _i0, size_t _i1) const
Swaps the items with index _i0 and index _i1 in all property vectors.
Definition: PropertyContainer.hh:276
Default property class for any type T.
Definition: Property.hh:94
virtual void swap(size_t _i0, size_t _i1)=0
Let two elements swap their storage place.
This class provides low-level property management like adding/removing properties and access to prope...
Definition: BaseKernel.hh:102
virtual void reserve(size_t _n)=0
Reserve memory for n elements.
A a container for properties.
Definition: PropertyContainer.hh:67
Base property handle.
Definition: Property.hh:471
int idx() const
Get the underlying index of this handle.
Definition: Handles.hh:74
virtual size_t n_elements() const =0
Number of elements in property.
void reserve(size_t _n) const
Reserves space for _n elements in all property vectors.
Definition: PropertyContainer.hh:249
virtual void clear()=0
Clear all elements and free memory.
Abstract class defining the basic interface of a dynamic property.
Definition: BaseProperty.hh:65
void resize(size_t _n) const
Resizes all property vectors to the specified size.
Definition: PropertyContainer.hh:256
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
virtual void resize(size_t _n)=0
Resize storage to hold n elements.

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