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>
83 #ifndef DOXY_IGNORE_THIS
88 #define SIMPLE_BINARY( T ) \
89 template <> struct binary< T > { \
90 typedef T value_type; \
91 static const bool is_streamable = true; \
92 static size_t size_of(const value_type&) { return sizeof(value_type); } \
93 static size_t size_of(void) { return sizeof(value_type); } \
94 static size_t store( std::ostream& _os, const value_type& _val, \
96 value_type tmp = _val; \
97 if (_swap) reverse_byte_order(tmp); \
98 _os.write( (const char*)&tmp, sizeof(value_type) ); \
99 return _os.good() ? sizeof(value_type) : 0; \
102 static size_t restore( std::istream& _is, value_type& _val, \
103 bool _swap=false) { \
104 _is.read( (char*)&_val, sizeof(value_type) ); \
105 if (_swap) reverse_byte_order(_val); \
106 return _is.good() ? sizeof(value_type) : 0; \
115 SIMPLE_BINARY(
float);
116 SIMPLE_BINARY(
double);
117 SIMPLE_BINARY(
long double);
139 #define SIMPLE_BINARY( T ) \
140 template <> struct binary< T > { \
141 typedef T value_type; \
142 static const bool is_streamable = true; \
143 static size_t size_of(const value_type&) { return sizeof(value_type); } \
144 static size_t size_of(void) { return sizeof(value_type); } \
145 static size_t store( std::ostream& _os, const value_type& _val, \
146 bool _swap=false) { \
147 value_type tmp = _val; \
148 if (_swap) reverse_byte_order(tmp); \
150 unsigned int t1 = static_cast<unsigned int>(tmp); \
151 _os.write( (const char*)&t1, sizeof(unsigned int) ); \
152 return _os.good() ? sizeof(unsigned int) : 0; \
155 static size_t restore( std::istream& _is, value_type& _val, \
156 bool _swap=false) { \
158 _is.read( (char*)&t1, sizeof(unsigned int) ); \
160 if (_swap) reverse_byte_order(_val); \
161 return _is.good() ? sizeof(unsigned int) : 0; \
165 SIMPLE_BINARY(
unsigned long);
169 #define VECTORT_BINARY( T ) \
170 template <> struct binary< T > { \
171 typedef T value_type; \
172 static const bool is_streamable = true; \
173 static size_t size_of(void) { return sizeof(value_type); } \
174 static size_t size_of(const value_type&) { return size_of(); } \
175 static size_t store( std::ostream& _os, const value_type& _val, \
176 bool _swap=false) { \
177 value_type tmp = _val; \
178 size_t i, b = size_of(_val), N = value_type::size_; \
179 if (_swap) for (i=0; i<N; ++i) \
180 reverse_byte_order( tmp[i] ); \
181 _os.write( (const char*)&tmp[0], b ); \
182 return _os.good() ? b : 0; \
185 static size_t restore( std::istream& _is, value_type& _val, \
186 bool _swap=false) { \
187 size_t i, N=value_type::size_; \
188 size_t b = N * sizeof(value_type::value_type); \
189 _is.read( (char*)&_val[0], b ); \
190 if (_swap) for (i=0; i<N; ++i) \
191 reverse_byte_order( _val[i] ); \
192 return _is.good() ? b : 0; \
196 #define VECTORTS_BINARY( N ) \
197 VECTORT_BINARY( Vec##N##c ); \
198 VECTORT_BINARY( Vec##N##uc ); \
199 VECTORT_BINARY( Vec##N##s ); \
200 VECTORT_BINARY( Vec##N##us ); \
201 VECTORT_BINARY( Vec##N##i ); \
202 VECTORT_BINARY( Vec##N##ui ); \
203 VECTORT_BINARY( Vec##N##f ); \
204 VECTORT_BINARY( Vec##N##d );
212 #undef VECTORTS_BINARY
213 #undef VECTORT_BINARY
215 template <>
struct binary< std::string > {
216 typedef std::string value_type;
219 static const bool is_streamable =
true;
221 static size_t size_of() {
return UnknownSize; }
222 static size_t size_of(
const value_type &_v)
223 {
return sizeof(length_t) + _v.size(); }
226 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
228 #if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
231 if (_v.size() < std::numeric_limits<length_t>::max() )
234 length_t len = length_t(_v.size());
236 size_t bytes = binary<length_t>::store( _os, len, _swap );
237 _os.write( _v.data(), len );
238 return _os.good() ? len+bytes : 0;
240 throw std::runtime_error(
"Cannot store string longer than 64Kb");
244 size_t restore(std::istream& _is, value_type& _val,
bool _swap=
false)
247 size_t bytes = binary<length_t>::restore( _is, len, _swap );
249 _is.read(
const_cast<char*
>(_val.data()), len );
251 return _is.good() ? (len+bytes) : 0;
256 template <>
struct binary<
OpenMesh::Attributes::StatusInfo>
259 typedef value_type::value_type status_t;
263 static size_t size_of() {
return sizeof(status_t); }
264 static size_t size_of(
const value_type&) {
return size_of(); }
266 static size_t n_bytes(
size_t _n_elem)
267 {
return _n_elem*
sizeof(status_t); }
270 size_t store(std::ostream& _os,
const value_type& _v,
bool _swap=
false)
272 status_t v=_v.bits();
273 return binary<status_t>::store(_os, v, _swap);
277 size_t restore( std::istream& _os, value_type& _v,
bool _swap=
false)
280 size_t b = binary<status_t>::restore(_os, v, _swap);
290 template <
typename T>
291 struct FunctorStore {
292 FunctorStore( std::ostream& _os,
bool _swap) : os_(_os), swap_(_swap) { }
293 size_t operator () (
size_t _v1,
const T& _s2 )
294 {
return _v1+binary<T>::store(os_, _s2, swap_ ); }
301 template <
typename T>
302 struct FunctorRestore {
303 FunctorRestore( std::istream& _is,
bool _swap) : is_(_is), swap_(_swap) { }
304 size_t operator () (
size_t _v1, T& _s2 )
305 {
return _v1+binary<T>::restore(is_, _s2, swap_ ); }
310 #include <OpenMesh/Core/IO/SR_binary_vector_of_fundamentals.inl>
311 #include <OpenMesh/Core/IO/SR_binary_vector_of_string.inl>
312 #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
long long int64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:89
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
bool is_streamable(void)
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: StoreRestore.hh:81
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