56 #ifndef OPENMESH_SR_BINARY_SPEC_HH
57 #define OPENMESH_SR_BINARY_SPEC_HH
61 #include <OpenMesh/Core/System/config.h>
65 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
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>
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 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; \
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; \
120 SIMPLE_BINARY(
float);
121 SIMPLE_BINARY(
double);
122 SIMPLE_BINARY(
long double);
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); \
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; \
160 static size_t restore( std::istream& _is, value_type& _val, \
161 bool _swap=false) { \
163 _is.read( (char*)&t1, sizeof(unsigned int) ); \
165 if (_swap) reverse_byte_order(_val); \
166 return _is.good() ? sizeof(unsigned int) : 0; \
170 SIMPLE_BINARY(
unsigned long);
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; \
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; \
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 );
217 #undef VECTORTS_BINARY
218 #undef VECTORT_BINARY
220 template <>
struct binary<
std::string > {
221 typedef std::string value_type;
224 static const bool is_streamable =
true;
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(); }
231 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
233 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
236 if (_v.size() < std::numeric_limits<length_t>::max() )
239 length_t len = length_t(_v.size());
241 size_t bytes = binary<length_t>::store( _os, len, _swap );
242 _os.write( _v.data(), len );
243 return _os.good() ? len+bytes : 0;
245 throw std::runtime_error(
"Cannot store string longer than 64Kb");
249 size_t restore(std::istream& _is, value_type& _val,
bool _swap=
false)
252 size_t bytes = binary<length_t>::restore( _is, len, _swap );
254 _is.read( const_cast<char*>(_val.data()), len );
256 return _is.good() ? (len+bytes) : 0;
261 template <>
struct binary<
OpenMesh::Attributes::StatusInfo>
264 typedef value_type::value_type status_t;
266 static const bool is_streamable =
true;
268 static size_t size_of() {
return sizeof(status_t); }
269 static size_t size_of(
const value_type&) {
return size_of(); }
271 static size_t n_bytes(
size_t _n_elem)
272 {
return _n_elem*
sizeof(status_t); }
275 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
277 status_t v=_v.bits();
278 return binary<status_t>::store(_os, v, _swap);
282 size_t restore( std::istream& _os, value_type& _v,
bool _swap=
false)
285 size_t b = binary<status_t>::restore(_os, v, _swap);
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_ ); }
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_ ); }
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>
321 #endif // DOXY_IGNORE_THIS
327 #endif // OPENMESH_SR_BINARY_SPEC_HH defined
unsigned short uint16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:86
signed char int8_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:85
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
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
unsigned int uint32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:90
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
int int32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:90