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 #ifndef OPENMESH_BASEPROPERTY_HH
45 #define OPENMESH_BASEPROPERTY_HH
46 
47 #include <string>
48 #include <OpenMesh/Core/IO/StoreRestore.hh>
50 
51 namespace OpenMesh {
52 
53 //== CLASS DEFINITION =========================================================
54 
60 class OPENMESHDLLEXPORT BaseProperty
61 {
62 public:
63 
65  static const size_t UnknownSize = size_t(-1);
66 
67 public:
68 
83  BaseProperty(const std::string& _name = "<unknown>")
84  : name_(_name), persistent_(false)
85  {}
86 
88  BaseProperty(const BaseProperty & _rhs)
89  : name_( _rhs.name_ ), persistent_( _rhs.persistent_ ) {}
90 
92  virtual ~BaseProperty() {}
93 
94 public: // synchronized array interface
95 
97  virtual void reserve(size_t _n) = 0;
98 
100  virtual void resize(size_t _n) = 0;
101 
103  virtual void clear() = 0;
104 
106  virtual void push_back() = 0;
107 
109  virtual void swap(size_t _i0, size_t _i1) = 0;
110 
112  virtual void copy(size_t _io, size_t _i1) = 0;
113 
115  virtual BaseProperty* clone () const = 0;
116 
117 public: // named property interface
118 
120  const std::string& name() const { return name_; }
121 
122  virtual void stats(std::ostream& _ostr) const;
123 
124 public: // I/O support
125 
127  bool persistent(void) const { return persistent_; }
128 
131  virtual void set_persistent( bool _yn ) = 0;
132 
134  virtual size_t n_elements() const = 0;
135 
137  virtual size_t element_size() const = 0;
138 
140  virtual size_t size_of() const
141  {
142  return size_of( n_elements() );
143  }
144 
147  virtual size_t size_of(size_t _n_elem) const
148  {
149  return (element_size()!=UnknownSize)
150  ? (_n_elem*element_size())
151  : UnknownSize;
152  }
153 
155  virtual size_t store( std::ostream& _ostr, bool _swap ) const = 0;
156 
160  virtual size_t restore( std::istream& _istr, bool _swap ) = 0;
161 
162 protected:
163 
164  // To be used in a derived class, when overloading set_persistent()
165  template < typename T >
166  void check_and_set_persistent( bool _yn )
167  {
168  if ( _yn && !IO::is_streamable<T>() )
169  omerr() << "Warning! Type of property value is not binary storable!\n";
170  persistent_ = IO::is_streamable<T>() && _yn;
171  }
172 
173 private:
174 
175  std::string name_;
176  bool persistent_;
177 };
178 
179 }//namespace OpenMesh
180 
181 #endif //OPENMESH_BASEPROPERTY_HH
182 
183 
BaseProperty(const std::string &_name="<unknown>")
Default constructor.
Definition: BaseProperty.hh:83
virtual ~BaseProperty()
Destructor.
Definition: BaseProperty.hh:92
const std::string & name() const
Return the name of the property.
BaseProperty(const BaseProperty &_rhs)
Copy constructor.
Definition: BaseProperty.hh:88
virtual size_t size_of() const
Return size of property in bytes.
virtual size_t size_of(size_t _n_elem) const
bool persistent(void) const
Returns true if the persistent flag is enabled else false.