OMToolkit  1.0
The polygonal mesh processing tool.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
OMStoreRestore.h
Go to the documentation of this file.
1 //==============================================================================
15 #ifndef _OM_STORE_RESTORE_H
16 #define _OM_STORE_RESTORE_H
17 
18 #include <OpenMesh\Core\IO\SR_store.hh>
19 #include <MDSTk/Module/mdsChannel.h>
20 
21 
22 namespace OpenMesh {
23 namespace IO {
24 
30 template < typename T > struct binaryExt : binary<T>
31 {
35  static size_t store( mds::mod::CChannel&, const value_type&, bool)
36  {
37  X;
38  return 0;
39  }
40 
44  static size_t restore( mds::mod::CChannel&, value_type&, bool)
45  {
46  X;
47  return 0;
48  }
49 };
50 
55 #define SIMPLE_BINARY_EXT( T ) \
56  template <> struct binaryExt< T > \
57  { \
58  typedef T value_type; \
59  static const bool is_streamable = true; \
60  \
61  static size_t size_of(const value_type&) { return sizeof(value_type); } \
62  static size_t size_of(void) { return sizeof(value_type); } \
63  static size_t store(std::ostream& _os, const value_type& _val, bool _swap=false) { \
64  value_type tmp = _val; \
65  if (_swap) reverse_byte_order(tmp); \
66  _os.write( (const char*)&tmp, sizeof(value_type) ); \
67  return _os.good() ? sizeof(value_type) : 0; \
68  } \
69  \
70  static size_t restore( std::istream& _is, value_type& _val, bool _swap=false) { \
71  _is.read( (char*)&_val, sizeof(value_type) ); \
72  if (_swap) reverse_byte_order(_val); \
73  return _is.good() ? sizeof(value_type) : 0; \
74  } \
75  \
76  static size_t store( mds::mod::CChannel& _os, const value_type& _val, bool _swap=false) { \
77  value_type tmp = _val; \
78  if (_swap) reverse_byte_order(tmp); \
79  _os.write( (const char*)&tmp, sizeof(value_type) ); \
80  return sizeof(value_type); \
81  } \
82  \
83  static size_t restore( mds::mod::CChannel& _is, value_type& _val, bool _swap=false) { \
84  _is.read( (char*)&_val, sizeof(value_type) ); \
85  if (_swap) reverse_byte_order(_val); \
86  return sizeof(value_type); \
87  } \
88 }
89 
93 SIMPLE_BINARY_EXT(bool);
97 SIMPLE_BINARY_EXT(float);
101 SIMPLE_BINARY_EXT(double);
105 SIMPLE_BINARY_EXT(long double);
109 SIMPLE_BINARY_EXT(int8_t);
113 SIMPLE_BINARY_EXT(int16_t);
117 SIMPLE_BINARY_EXT(int32_t);
121 SIMPLE_BINARY_EXT(int64_t);
125 SIMPLE_BINARY_EXT(uint8_t);
129 SIMPLE_BINARY_EXT(uint16_t);
133 SIMPLE_BINARY_EXT(uint32_t);
137 SIMPLE_BINARY_EXT(uint64_t);
138 
142 #define VECTORT_BINARY_EXT( T ) \
143  template <> struct binaryExt< T > { \
144  typedef T value_type; \
145  static const bool is_streamable = true; \
146  static size_t size_of(void) { return sizeof(value_type); } \
147  static size_t size_of(const value_type&) { return size_of(); } \
148  static size_t store( std::ostream& _os, const value_type& _val, bool _swap=false) { \
149  value_type tmp = _val; \
150  size_t i, b = size_of(_val), N = value_type::size_; \
151  if (_swap) for (i=0; i<N; ++i) \
152  reverse_byte_order( tmp[i] ); \
153  _os.write( (const char*)&tmp[0], b ); \
154  return _os.good() ? b : 0; \
155  } \
156  \
157  static size_t restore( std::istream& _is, value_type& _val, bool _swap=false) { \
158  size_t i, N=value_type::size_; \
159  size_t b = N * sizeof(value_type::value_type); \
160  _is.read( (char*)&_val[0], b ); \
161  if (_swap) for (i=0; i<N; ++i) \
162  reverse_byte_order( _val[i] ); \
163  return _is.good() ? b : 0; \
164  } \
165  \
166  static size_t store( mds::mod::CChannel& _os, const value_type& _val, bool _swap=false) { \
167  value_type tmp = _val; \
168  size_t i, b = size_of(_val), N = value_type::size_; \
169  if (_swap) for (i=0; i<N; ++i) \
170  reverse_byte_order( tmp[i] ); \
171  _os.write( (const char*)&tmp[0], b ); \
172  return b; \
173  } \
174  \
175  static size_t restore( mds::mod::CChannel& _is, value_type& _val, bool _swap=false) { \
176  size_t i, N=value_type::size_; \
177  size_t b = N * sizeof(value_type::value_type); \
178  _is.read( (char*)&_val[0], b ); \
179  if (_swap) for (i=0; i<N; ++i) \
180  reverse_byte_order( _val[i] ); \
181  return b; \
182  } \
183  }
184 
188 #define VECTORTS_BINARY_EXT( N ) \
189  VECTORT_BINARY_EXT( Vec##N##c ); \
190  VECTORT_BINARY_EXT( Vec##N##uc ); \
191  VECTORT_BINARY_EXT( Vec##N##s ); \
192  VECTORT_BINARY_EXT( Vec##N##us ); \
193  VECTORT_BINARY_EXT( Vec##N##i ); \
194  VECTORT_BINARY_EXT( Vec##N##ui ); \
195  VECTORT_BINARY_EXT( Vec##N##f ); \
196  VECTORT_BINARY_EXT( Vec##N##d );
197 
218 
219 
220 
221 
223 
226 template <> struct binaryExt< std::string >
227 {
228  typedef std::string value_type;
229  typedef uint16_t length_t;
230 
231  static const bool is_streamable = true;
235  static size_t size_of() { return UnknownSize; }
239  static size_t size_of(const value_type &_v) { return sizeof(length_t) + _v.size(); }
240 
248  static size_t store(mds::mod::CChannel& _os, const value_type& _v, bool _swap=false)
249  {
250  #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
251  if (_v.size() < Utils::NumLimitsT<length_t>::max() )
252  #else
253  if (_v.size() < std::numeric_limits<length_t>::max() )
254  #endif
255  {
256  length_t len = _v.size();
257  if (_swap) reverse_byte_order(len);
258 
259  size_t bytes = binaryExt<length_t>::store( _os, len, _swap );
260  _os.write( _v.data(), len );
261  return len+bytes;
262  }
263  throw std::runtime_error("Cannot store string longer than 64Kb");
264  }
265 
273  static size_t restore(mds::mod::CChannel& _is, value_type& _val, bool _swap=false)
274  {
275  length_t len;
276  size_t bytes = binaryExt<length_t>::restore( _is, len, _swap );
277  if (_swap)
278  reverse_byte_order(len);
279  _val.resize(len);
280  _is.read( const_cast<char*>(_val.data()), len );
281 
282  return len+bytes;
283  }
284 };
285 
287 
290 template <> struct binaryExt<OpenMesh::Attributes::StatusInfo>
291 {
292  typedef OpenMesh::Attributes::StatusInfo value_type;
293  typedef value_type::value_type status_t;
294 
295  static const bool is_streamable = true;
299  static size_t size_of() { return sizeof(status_t); }
303  static size_t size_of(const value_type&) { return size_of(); }
307  static size_t n_bytes(size_t _n_elem)
308  {
309  return _n_elem*sizeof(status_t);
310  }
311 
319  static size_t store(mds::mod::CChannel& _os, const value_type& _v, bool _swap=false)
320  {
321  status_t v=_v.bits();
322  return binaryExt<status_t>::store(_os, v, _swap);
323  }
324 
332  static size_t restore(mds::mod::CChannel& _is, value_type& _v, bool _swap=false)
333  {
334  status_t v;
335  size_t b = binaryExt<status_t>::restore(_is, v, _swap);
336  _v.set_bits(v);
337  return b;
338  }
339 };
340 
341 }// namespace IO
342 }// namespace OpenMesh
343 
344 #endif