OpenMesh
SR_binary_spec.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 
50 //=============================================================================
51 //
52 // Helper Functions for binary reading / writing
53 //
54 //=============================================================================
55 
56 #ifndef OPENMESH_SR_BINARY_SPEC_HH
57 #define OPENMESH_SR_BINARY_SPEC_HH
58 
59 //== INCLUDES =================================================================
60 
61 #include <OpenMesh/Core/System/config.h>
62 // -------------------- STL
63 #include <iterator>
64 #include <string>
65 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
67 #else
68 # include <limits>
69 #endif
70 #include <vector>
71 #include <stdexcept> // logic_error
72 #include <numeric> // accumulate
73 // -------------------- OpenMesh
74 #include <OpenMesh/Core/Geometry/VectorT.hh>
75 #include <OpenMesh/Core/Mesh/Status.hh>
76 #include <OpenMesh/Core/IO/SR_types.hh>
77 #include <OpenMesh/Core/IO/SR_rbo.hh>
78 #include <OpenMesh/Core/IO/SR_binary.hh>
79 
80 //== NAMESPACES ===============================================================
81 
82 namespace OpenMesh {
83 namespace IO {
84 
85 
86 //=============================================================================
87 
88 #ifndef DOXY_IGNORE_THIS
89 
90 //-----------------------------------------------------------------------------
91 // struct binary, helper for storing/restoring
92 
93 #define SIMPLE_BINARY( T ) \
94  template <> struct binary< T > { \
95  typedef T value_type; \
96  static const bool is_streamable = true; \
97  static size_t size_of(const value_type&) { return sizeof(value_type); } \
98  static size_t size_of(void) { return sizeof(value_type); } \
99  static size_t store( std::ostream& _os, const value_type& _val, \
100  bool _swap=false) { \
101  value_type tmp = _val; \
102  if (_swap) reverse_byte_order(tmp); \
103  _os.write( (const char*)&tmp, sizeof(value_type) ); \
104  return _os.good() ? sizeof(value_type) : 0; \
105  } \
106  \
107  static size_t restore( std::istream& _is, value_type& _val, \
108  bool _swap=false) { \
109  _is.read( (char*)&_val, sizeof(value_type) ); \
110  if (_swap) reverse_byte_order(_val); \
111  return _is.good() ? sizeof(value_type) : 0; \
112  } \
113  }
114 
115 SIMPLE_BINARY(bool);
116 //SIMPLE_BINARY(int);
117 
118 // Why is this needed? Should not be used as not 32 bit compatible
119 //SIMPLE_BINARY(unsigned long);
120 SIMPLE_BINARY(float);
121 SIMPLE_BINARY(double);
122 SIMPLE_BINARY(long double);
123 SIMPLE_BINARY(char);
124 
125 SIMPLE_BINARY(int8_t);
126 SIMPLE_BINARY(int16_t);
127 SIMPLE_BINARY(int32_t);
128 SIMPLE_BINARY(int64_t);
129 SIMPLE_BINARY(uint8_t);
130 SIMPLE_BINARY(uint16_t);
131 SIMPLE_BINARY(uint32_t);
132 SIMPLE_BINARY(uint64_t);
133 
134 #undef SIMPLE_BINARY
135 
136 // For unsigned long which is of size 64 bit on 64 bit
137 // architectures: convert into 32 bit unsigned integer value
138 // in order to stay compatible between 32/64 bit architectures.
139 // This allows cross reading BUT forbids storing unsigned longs
140 // as data type since higher order word (4 bytes) will be truncated.
141 // Does not work in case the data type that is to be stored
142 // exceeds the value range of unsigned int in size, which is improbable...
143 
144 #define SIMPLE_BINARY( T ) \
145  template <> struct binary< T > { \
146  typedef T value_type; \
147  static const bool is_streamable = true; \
148  static size_t size_of(const value_type&) { return sizeof(value_type); } \
149  static size_t size_of(void) { return sizeof(value_type); } \
150  static size_t store( std::ostream& _os, const value_type& _val, \
151  bool _swap=false) { \
152  value_type tmp = _val; \
153  if (_swap) reverse_byte_order(tmp); \
154  /* Convert unsigned long to unsigned int for compatibility reasons */ \
155  unsigned int t1 = static_cast<unsigned int>(tmp); \
156  _os.write( (const char*)&t1, sizeof(unsigned int) ); \
157  return _os.good() ? sizeof(unsigned int) : 0; \
158  } \
159  \
160  static size_t restore( std::istream& _is, value_type& _val, \
161  bool _swap=false) { \
162  unsigned int t1; \
163  _is.read( (char*)&t1, sizeof(unsigned int) ); \
164  _val = t1; \
165  if (_swap) reverse_byte_order(_val); \
166  return _is.good() ? sizeof(unsigned int) : 0; \
167  } \
168  }
169 
170 SIMPLE_BINARY(unsigned long);
171 
172 #undef SIMPLE_BINARY
173 
174 #define VECTORT_BINARY( T ) \
175  template <> struct binary< T > { \
176  typedef T value_type; \
177  static const bool is_streamable = true; \
178  static size_t size_of(void) { return sizeof(value_type); } \
179  static size_t size_of(const value_type&) { return size_of(); } \
180  static size_t store( std::ostream& _os, const value_type& _val, \
181  bool _swap=false) { \
182  value_type tmp = _val; \
183  size_t i, b = size_of(_val), N = value_type::size_; \
184  if (_swap) for (i=0; i<N; ++i) \
185  reverse_byte_order( tmp[i] ); \
186  _os.write( (const char*)&tmp[0], b ); \
187  return _os.good() ? b : 0; \
188  } \
189  \
190  static size_t restore( std::istream& _is, value_type& _val, \
191  bool _swap=false) { \
192  size_t i, N=value_type::size_; \
193  size_t b = N * sizeof(value_type::value_type); \
194  _is.read( (char*)&_val[0], b ); \
195  if (_swap) for (i=0; i<N; ++i) \
196  reverse_byte_order( _val[i] ); \
197  return _is.good() ? b : 0; \
198  } \
199  }
200 
201 #define VECTORTS_BINARY( N ) \
202  VECTORT_BINARY( Vec##N##c ); \
203  VECTORT_BINARY( Vec##N##uc ); \
204  VECTORT_BINARY( Vec##N##s ); \
205  VECTORT_BINARY( Vec##N##us ); \
206  VECTORT_BINARY( Vec##N##i ); \
207  VECTORT_BINARY( Vec##N##ui ); \
208  VECTORT_BINARY( Vec##N##f ); \
209  VECTORT_BINARY( Vec##N##d );
210 
211 VECTORTS_BINARY( 1 )
212 VECTORTS_BINARY( 2 )
213 VECTORTS_BINARY( 3 )
214 VECTORTS_BINARY( 4 )
215 VECTORTS_BINARY( 6 )
216 
217 #undef VECTORTS_BINARY
218 #undef VECTORT_BINARY
219 
220 template <> struct binary< std::string > {
221  typedef std::string value_type;
222  typedef uint16_t length_t;
223 
224  static const bool is_streamable = true;
225 
226  static size_t size_of() { return UnknownSize; }
227  static size_t size_of(const value_type &_v)
228  { return sizeof(length_t) + _v.size(); }
229 
230  static
231  size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
232  {
233 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
234  if (_v.size() < Utils::NumLimitsT<length_t>::max() )
235 #else
236  if (_v.size() < std::numeric_limits<length_t>::max() )
237 #endif
238  {
239  length_t len = length_t(_v.size());
240 
241  size_t bytes = binary<length_t>::store( _os, len, _swap );
242  _os.write( _v.data(), len );
243  return _os.good() ? len+bytes : 0;
244  }
245  throw std::runtime_error("Cannot store string longer than 64Kb");
246  }
247 
248  static
249  size_t restore(std::istream& _is, value_type& _val, bool _swap=false)
250  {
251  length_t len;
252  size_t bytes = binary<length_t>::restore( _is, len, _swap );
253  _val.resize(len);
254  _is.read( const_cast<char*>(_val.data()), len );
255 
256  return _is.good() ? (len+bytes) : 0;
257  }
258 };
259 
260 
261 template <> struct binary<OpenMesh::Attributes::StatusInfo>
262 {
263  typedef OpenMesh::Attributes::StatusInfo value_type;
264  typedef value_type::value_type status_t;
265 
266  static const bool is_streamable = true;
267 
268  static size_t size_of() { return sizeof(status_t); }
269  static size_t size_of(const value_type&) { return size_of(); }
270 
271  static size_t n_bytes(size_t _n_elem)
272  { return _n_elem*sizeof(status_t); }
273 
274  static
275  size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
276  {
277  status_t v=_v.bits();
278  return binary<status_t>::store(_os, v, _swap);
279  }
280 
281  static
282  size_t restore( std::istream& _os, value_type& _v, bool _swap=false)
283  {
284  status_t v;
285  size_t b = binary<status_t>::restore(_os, v, _swap);
286  _v.set_bits(v);
287  return b;
288  }
289 };
290 
291 
292 //-----------------------------------------------------------------------------
293 // std::vector<T> specializations for struct binary<>
294 
295 template <typename T>
296 struct FunctorStore {
297  FunctorStore( std::ostream& _os, bool _swap) : os_(_os), swap_(_swap) { }
298  size_t operator () ( size_t _v1, const T& _s2 )
299  { return _v1+binary<T>::store(os_, _s2, swap_ ); }
300 
301  std::ostream& os_;
302  bool swap_;
303 };
304 
305 
306 template <typename T>
307 struct FunctorRestore {
308  FunctorRestore( std::istream& _is, bool _swap) : is_(_is), swap_(_swap) { }
309  size_t operator () ( size_t _v1, T& _s2 )
310  { return _v1+binary<T>::restore(is_, _s2, swap_ ); }
311  std::istream& is_;
312  bool swap_;
313 };
314 
315 #include <OpenMesh/Core/IO/SR_binary_vector_of_fundamentals.inl>
316 #include <OpenMesh/Core/IO/SR_binary_vector_of_string.inl>
317 #include <OpenMesh/Core/IO/SR_binary_vector_of_bool.inl>
318 
319 // ----------------------------------------------------------------------------
320 
321 #endif // DOXY_IGNORE_THIS
322 
323 //=============================================================================
324 } // namespace IO
325 } // namespace OpenMesh
326 //=============================================================================
327 #endif // OPENMESH_SR_BINARY_SPEC_HH defined
328 //=============================================================================
329 
STL namespace.
signed char int8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:85
unsigned short uint16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:86
unsigned int uint32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:90
unsigned char uint8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:85
int int32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:90
long long int64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:94
Add status information to a base class.
Definition: Status.hh:99
unsigned long long uint64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:94
short int16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:86
static Scalar max()
Return the maximum absolte value a scalar type can store.
Definition: NumLimitsT.hh:102
size_t size_of(const T &_v)
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: StoreRestore.hh:94
Temporary solution until std::numeric_limits is standard.
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64

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