51 #ifndef OPENMESH_SR_BINARY_SPEC_HH
52 #define OPENMESH_SR_BINARY_SPEC_HH
56 #include <OpenMesh/Core/System/config.h>
60 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
69 #include <OpenMesh/Core/Geometry/VectorT.hh>
70 #include <OpenMesh/Core/Mesh/Status.hh>
71 #include <OpenMesh/Core/IO/SR_types.hh>
72 #include <OpenMesh/Core/IO/SR_rbo.hh>
73 #include <OpenMesh/Core/IO/SR_binary.hh>
76 #include <OpenMesh/Core/Utils/typename.hh>
88 #ifndef DOXY_IGNORE_THIS
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 std::string type_identifier(void) { return #T; } \
100 static size_t store( std::ostream& _os, const value_type& _val, \
101 bool _swap=false) { \
102 value_type tmp = _val; \
103 if (_swap) reverse_byte_order(tmp); \
104 _os.write( (const char*)&tmp, sizeof(value_type) ); \
105 return _os.good() ? sizeof(value_type) : 0; \
108 static size_t restore( std::istream& _is, value_type& _val, \
109 bool _swap=false) { \
110 _is.read( (char*)&_val, sizeof(value_type) ); \
111 if (_swap) reverse_byte_order(_val); \
112 return _is.good() ? sizeof(value_type) : 0; \
121 SIMPLE_BINARY(
float);
122 SIMPLE_BINARY(
double);
123 SIMPLE_BINARY(
long double);
136 SIMPLE_BINARY(FaceHandle);
137 SIMPLE_BINARY(EdgeHandle);
138 SIMPLE_BINARY(HalfedgeHandle);
139 SIMPLE_BINARY(VertexHandle);
140 SIMPLE_BINARY(MeshHandle);
152 #define SIMPLE_BINARY( T ) \
153 template <> struct binary< T > { \
154 typedef T value_type; \
155 static const bool is_streamable = true; \
156 static size_t size_of(const value_type&) { return sizeof(value_type); } \
157 static size_t size_of(void) { return sizeof(value_type); } \
158 static std::string type_identifier(void) { return #T; } \
159 static size_t store( std::ostream& _os, const value_type& _val, \
160 bool _swap=false) { \
161 value_type tmp = _val; \
162 if (_swap) reverse_byte_order(tmp); \
164 unsigned int t1 = static_cast<unsigned int>(tmp); \
165 _os.write( (const char*)&t1, sizeof(unsigned int) ); \
166 return _os.good() ? sizeof(unsigned int) : 0; \
169 static size_t restore( std::istream& _is, value_type& _val, \
170 bool _swap=false) { \
172 _is.read( (char*)&t1, sizeof(unsigned int) ); \
174 if (_swap) reverse_byte_order(_val); \
175 return _is.good() ? sizeof(unsigned int) : 0; \
179 SIMPLE_BINARY(
unsigned long);
183 #define VECTORT_BINARY( T ) \
184 template <> struct binary< T > { \
185 typedef T value_type; \
186 static const bool is_streamable = true; \
187 static size_t size_of(void) { return sizeof(value_type); } \
188 static size_t size_of(const value_type&) { return size_of(); } \
189 static std::string type_identifier(void) { return #T; } \
190 static size_t store( std::ostream& _os, const value_type& _val, \
191 bool _swap=false) { \
192 value_type tmp = _val; \
193 size_t i, b = size_of(_val), N = value_type::size_; \
195 for (i=0; i<N; ++i) \
196 reverse_byte_order( tmp[i] ); \
197 _os.write( (const char*)&tmp[0], b ); \
198 return _os.good() ? b : 0; \
201 static size_t restore( std::istream& _is, value_type& _val, \
202 bool _swap=false) { \
203 size_t i, N=value_type::size_; \
204 size_t b = N * sizeof(value_type::value_type); \
205 _is.read( (char*)&_val[0], b ); \
206 if (_swap) for (i=0; i<N; ++i) \
207 reverse_byte_order( _val[i] ); \
208 return _is.good() ? b : 0; \
212 #define VECTORTS_BINARY( N ) \
213 VECTORT_BINARY( Vec##N##c ); \
214 VECTORT_BINARY( Vec##N##uc ); \
215 VECTORT_BINARY( Vec##N##s ); \
216 VECTORT_BINARY( Vec##N##us ); \
217 VECTORT_BINARY( Vec##N##i ); \
218 VECTORT_BINARY( Vec##N##ui ); \
219 VECTORT_BINARY( Vec##N##f ); \
220 VECTORT_BINARY( Vec##N##d );
229 #undef VECTORTS_BINARY
230 #undef VECTORT_BINARY
232 template <>
struct binary< std::string > {
233 typedef std::string value_type;
238 static size_t size_of() {
return UnknownSize; }
239 static size_t size_of(
const value_type &_v)
240 {
return sizeof(length_t) + _v.size(); }
243 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
245 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
248 if (_v.size() < std::numeric_limits<length_t>::max() )
251 length_t len = length_t(_v.size());
254 _os.write( _v.data(), len );
255 return _os.good() ? len+bytes : 0;
257 throw std::runtime_error(
"Cannot store string longer than 64Kb");
261 size_t restore(std::istream& _is, value_type& _val,
bool _swap=
false)
266 _is.read(
const_cast<char*
>(_val.data()), len );
268 return _is.good() ? (len+bytes) : 0;
272 template <>
struct binary<
OpenMesh::Attributes::StatusInfo>
275 typedef value_type::value_type status_t;
279 static size_t size_of() {
return sizeof(status_t); }
283 static size_t n_bytes(
size_t _n_elem)
284 {
return _n_elem*
sizeof(status_t); }
287 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
289 status_t v=_v.bits();
294 size_t restore( std::istream& _os, value_type& _v,
bool _swap=
false)
307 template <
typename T>
308 struct FunctorStore {
309 FunctorStore( std::ostream& _os,
bool _swap) : os_(_os), swap_(_swap) { }
310 size_t operator () (
size_t _v1,
const T& _s2 )
318 template <
typename T>
319 struct FunctorRestore {
320 FunctorRestore( std::istream& _is,
bool _swap) : is_(_is), swap_(_swap) { }
321 size_t operator () (
size_t _v1, T& _s2 )
327 template <
typename T>
328 struct binary< std::vector< T >, typename std::enable_if<std::is_default_constructible<T>::value>::type > {
329 typedef std::vector< T > value_type;
330 typedef typename value_type::value_type elem_type;
333 static size_t size_of(
bool _store_size =
true)
334 {
return IO::UnknownSize; }
336 static size_t size_of(
const value_type& _v,
bool _store_size =
true)
340 unsigned int N =
static_cast<unsigned int>(_v.size());
358 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false,
bool _store_size =
true) {
362 unsigned int N =
static_cast<unsigned int>(_v.size());
366 bytes += std::accumulate( _v.begin(), _v.end(),
static_cast<size_t>(0),
367 FunctorStore<elem_type>(_os,_swap) );
371 if (elem_size != IO::UnknownSize && elem_size ==
sizeof(elem_type))
375 auto bytes_of_vec =
size_of(_v,
false);
376 bytes += bytes_of_vec;
378 _os.write(
reinterpret_cast<const char*
>(&_v[0]), bytes_of_vec);
383 for (
const auto& v : _v)
387 return _os.good() ? bytes : 0;
390 static size_t restore(std::istream& _is, value_type& _v,
bool _swap=
false,
bool _restore_size =
true) {
396 unsigned int size_of_vec;
398 _v.resize(size_of_vec);
402 bytes += std::accumulate( _v.begin(), _v.end(),
size_t(0),
403 FunctorRestore<elem_type>(_is, _swap) );
407 if (elem_size != IO::UnknownSize && elem_size ==
sizeof(elem_type))
411 auto bytes_of_vec =
size_of(_v,
false);
412 bytes += bytes_of_vec;
414 _is.read(
reinterpret_cast<char*
>(&_v[0]), bytes_of_vec );
423 return _is.good() ? bytes : 0;
427 #include <OpenMesh/Core/IO/SR_binary_vector_of_bool.inl>
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:59
signed char int8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:80
short int16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:81
unsigned long long uint64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:89
unsigned int uint32_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:81
int int32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:85
unsigned char uint8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:80
static size_t restore(std::istream &, value_type &, bool=false, bool=true)
Restore a value of T and return the number of bytes read.
Definition: SR_binary.hh:126
static const bool is_streamable
Can we store T? Set this to true in your specialization.
Definition: SR_binary.hh:106
static size_t store(std::ostream &, const value_type &, bool=false, bool=true)
Store a value of T and return the number of bytes written.
Definition: SR_binary.hh:118
static std::string type_identifier(void)
A string that identifies the type of T.
Definition: SR_binary.hh:114
static size_t size_of(void)
What's the size of T? If it depends on the actual value (e.g. for vectors) return UnknownSize.
Definition: SR_binary.hh:109
Add status information to a base class.
Definition: Status.hh:95
static Scalar max()
Return the maximum absolte value a scalar type can store.
Definition: NumLimitsT.hh:97