Developer Documentation
BaseProperty.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_BASEPROPERTY_HH
50 #define OPENMESH_BASEPROPERTY_HH
51 
52 #include <string>
53 #include <OpenMesh/Core/IO/StoreRestore.hh>
55 
56 namespace OpenMesh {
57 
58 //== CLASS DEFINITION =========================================================
59 
65 class OPENMESHDLLEXPORT BaseProperty
66 {
67 public:
68 
70  static const size_t UnknownSize = size_t(-1);
71 
72 public:
73 
88  BaseProperty(const std::string& _name = "<unknown>")
89  : name_(_name), persistent_(false)
90  {}
91 
93  BaseProperty(const BaseProperty & _rhs)
94  : name_( _rhs.name_ ), persistent_( _rhs.persistent_ ) {}
95 
97  virtual ~BaseProperty() {}
98 
99 public: // synchronized array interface
100 
102  virtual void reserve(size_t _n) = 0;
103 
105  virtual void resize(size_t _n) = 0;
106 
108  virtual void clear() = 0;
109 
111  virtual void push_back() = 0;
112 
114  virtual void swap(size_t _i0, size_t _i1) = 0;
115 
117  virtual void copy(size_t _io, size_t _i1) = 0;
118 
120  virtual BaseProperty* clone () const = 0;
121 
122 public: // named property interface
123 
125  const std::string& name() const { return name_; }
126 
127  virtual void stats(std::ostream& _ostr) const;
128 
129 public: // I/O support
130 
132  bool persistent(void) const { return persistent_; }
133 
136  virtual void set_persistent( bool _yn ) = 0;
137 
139  virtual size_t n_elements() const = 0;
140 
142  virtual size_t element_size() const = 0;
143 
145  virtual size_t size_of() const
146  {
147  return size_of( n_elements() );
148  }
149 
152  virtual size_t size_of(size_t _n_elem) const
153  {
154  return (element_size()!=UnknownSize)
155  ? (_n_elem*element_size())
156  : UnknownSize;
157  }
158 
160  virtual size_t store( std::ostream& _ostr, bool _swap ) const = 0;
161 
165  virtual size_t restore( std::istream& _istr, bool _swap ) = 0;
166 
167 protected:
168 
169  // To be used in a derived class, when overloading set_persistent()
170  template < typename T >
171  void check_and_set_persistent( bool _yn )
172  {
173  if ( _yn && !IO::is_streamable<T>() )
174  omerr() << "Warning! Type of property value is not binary storable!\n";
175  persistent_ = IO::is_streamable<T>() && _yn;
176  }
177 
178 private:
179 
180  std::string name_;
181  bool persistent_;
182 };
183 
184 }//namespace OpenMesh
185 
186 #endif //OPENMESH_BASEPROPERTY_HH
187 
188 
bool persistent(void) const
Returns true if the persistent flag is enabled else false.
virtual size_t size_of() const
Return size of property in bytes.
virtual size_t size_of(size_t _n_elem) const
BaseProperty(const BaseProperty &_rhs)
Copy constructor.
Definition: BaseProperty.hh:93
const std::string & name() const
Return the name of the property.
virtual ~BaseProperty()
Destructor.
Definition: BaseProperty.hh:97
BaseProperty(const std::string &_name="<unknown>")
Default constructor.
Definition: BaseProperty.hh:88