49 #ifndef OPENMESH_SR_BINARY_SPEC_HH
50 #define OPENMESH_SR_BINARY_SPEC_HH
54 #include <OpenMesh/Core/System/config.h>
58 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
67 #include <OpenMesh/Core/Geometry/VectorT.hh>
68 #include <OpenMesh/Core/Mesh/Status.hh>
69 #include <OpenMesh/Core/IO/SR_types.hh>
70 #include <OpenMesh/Core/IO/SR_rbo.hh>
71 #include <OpenMesh/Core/IO/SR_binary.hh>
81 #ifndef DOXY_IGNORE_THIS
86 #define SIMPLE_BINARY( T ) \
87 template <> struct binary< T > { \
88 typedef T value_type; \
89 static const bool is_streamable = true; \
90 static size_t size_of(const value_type&) { return sizeof(value_type); } \
91 static size_t size_of(void) { return sizeof(value_type); } \
92 static size_t store( std::ostream& _os, const value_type& _val, \
94 value_type tmp = _val; \
95 if (_swap) reverse_byte_order(tmp); \
96 _os.write( (const char*)&tmp, sizeof(value_type) ); \
97 return _os.good() ? sizeof(value_type) : 0; \
100 static size_t restore( std::istream& _is, value_type& _val, \
101 bool _swap=false) { \
102 _is.read( (char*)&_val, sizeof(value_type) ); \
103 if (_swap) reverse_byte_order(_val); \
104 return _is.good() ? sizeof(value_type) : 0; \
113 SIMPLE_BINARY(
float);
114 SIMPLE_BINARY(
double);
115 SIMPLE_BINARY(
long double);
136 #define SIMPLE_BINARY( T ) \
137 template <> struct binary< T > { \
138 typedef T value_type; \
139 static const bool is_streamable = true; \
140 static size_t size_of(const value_type&) { return sizeof(value_type); } \
141 static size_t size_of(void) { return sizeof(value_type); } \
142 static size_t store( std::ostream& _os, const value_type& _val, \
143 bool _swap=false) { \
144 value_type tmp = _val; \
145 if (_swap) reverse_byte_order(tmp); \
147 unsigned int t1 = static_cast<unsigned int>(tmp); \
148 _os.write( (const char*)&t1, sizeof(unsigned int) ); \
149 return _os.good() ? sizeof(unsigned int) : 0; \
152 static size_t restore( std::istream& _is, value_type& _val, \
153 bool _swap=false) { \
155 _is.read( (char*)&t1, sizeof(unsigned int) ); \
157 if (_swap) reverse_byte_order(_val); \
158 return _is.good() ? sizeof(unsigned int) : 0; \
162 SIMPLE_BINARY(
unsigned long);
166 #define VECTORT_BINARY( T ) \
167 template <> struct binary< T > { \
168 typedef T value_type; \
169 static const bool is_streamable = true; \
170 static size_t size_of(void) { return sizeof(value_type); } \
171 static size_t size_of(const value_type&) { return size_of(); } \
172 static size_t store( std::ostream& _os, const value_type& _val, \
173 bool _swap=false) { \
174 value_type tmp = _val; \
175 size_t i, b = size_of(_val), N = value_type::size_; \
176 if (_swap) for (i=0; i<N; ++i) \
177 reverse_byte_order( tmp[i] ); \
178 _os.write( (const char*)&tmp[0], b ); \
179 return _os.good() ? b : 0; \
182 static size_t restore( std::istream& _is, value_type& _val, \
183 bool _swap=false) { \
184 size_t i, N=value_type::size_; \
185 size_t b = N * sizeof(value_type::value_type); \
186 _is.read( (char*)&_val[0], b ); \
187 if (_swap) for (i=0; i<N; ++i) \
188 reverse_byte_order( _val[i] ); \
189 return _is.good() ? b : 0; \
193 #define VECTORTS_BINARY( N ) \
194 VECTORT_BINARY( Vec##N##c ); \
195 VECTORT_BINARY( Vec##N##uc ); \
196 VECTORT_BINARY( Vec##N##s ); \
197 VECTORT_BINARY( Vec##N##us ); \
198 VECTORT_BINARY( Vec##N##i ); \
199 VECTORT_BINARY( Vec##N##ui ); \
200 VECTORT_BINARY( Vec##N##f ); \
201 VECTORT_BINARY( Vec##N##d );
209 #undef VECTORTS_BINARY
210 #undef VECTORT_BINARY
212 template <>
struct binary< std::string > {
213 typedef std::string value_type;
216 static const bool is_streamable =
true;
218 static size_t size_of() {
return UnknownSize; }
219 static size_t size_of(
const value_type &_v)
220 {
return sizeof(length_t) + _v.size(); }
223 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
225 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
226 if (_v.size() < Utils::NumLimitsT<length_t>::max() )
228 if (_v.size() < std::numeric_limits<length_t>::max() )
231 length_t len = _v.size();
235 size_t bytes = binary<length_t>::store( _os, len, _swap );
236 _os.write( _v.data(), len );
237 return _os.good() ? len+bytes : 0;
239 throw std::runtime_error(
"Cannot store string longer than 64Kb");
243 size_t restore(std::istream& _is, value_type& _val,
bool _swap=
false)
246 size_t bytes = binary<length_t>::restore( _is, len, _swap );
250 _is.read( const_cast<char*>(_val.data()), len );
252 return _is.good() ? (len+bytes) : 0;
257 template <>
struct binary<OpenMesh::Attributes::StatusInfo>
260 typedef value_type::value_type status_t;
262 static const bool is_streamable =
true;
264 static size_t size_of() {
return sizeof(status_t); }
265 static size_t size_of(
const value_type&) {
return size_of(); }
267 static size_t n_bytes(
size_t _n_elem)
268 {
return _n_elem*
sizeof(status_t); }
271 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
273 status_t v=_v.bits();
274 return binary<status_t>::store(_os, v, _swap);
278 size_t restore( std::istream& _os, value_type& _v,
bool _swap=
false)
281 size_t b = binary<status_t>::restore(_os, v, _swap);
291 template <
typename T>
292 struct FunctorStore {
293 FunctorStore( std::ostream& _os,
bool _swap) : os_(_os), swap_(_swap) { }
294 size_t operator () (
size_t _v1,
const T& _s2 )
295 {
return _v1+binary<T>::store(os_, _s2, swap_ ); }
302 template <
typename T>
303 struct FunctorRestore {
304 FunctorRestore( std::istream& _is,
bool _swap) : is_(_is), swap_(_swap) { }
305 size_t operator () (
size_t _v1, T& _s2 )
306 {
return _v1+binary<T>::restore(is_, _s2, swap_ ); }
311 #include <OpenMesh/Core/IO/SR_binary_vector_of_fundamentals.inl>
312 #include <OpenMesh/Core/IO/SR_binary_vector_of_string.inl>
313 #include <OpenMesh/Core/IO/SR_binary_vector_of_bool.inl>
317 #endif // DOXY_IGNORE_THIS
323 #endif // OPENMESH_SR_BINARY_SPEC_HH defined