diff --git a/src/OpenMesh/Core/IO/SR_binary_spec.hh b/src/OpenMesh/Core/IO/SR_binary_spec.hh index 6fbdf996..e3cb5938 100644 --- a/src/OpenMesh/Core/IO/SR_binary_spec.hh +++ b/src/OpenMesh/Core/IO/SR_binary_spec.hh @@ -75,6 +75,8 @@ #include +#include + //== NAMESPACES =============================================================== namespace OpenMesh { @@ -321,7 +323,84 @@ struct FunctorRestore { bool swap_; }; -#include +template +struct binary< std::vector< T > > { + typedef std::vector< T > value_type; + typedef typename value_type::value_type elem_type; + + static const bool is_streamable = binary::is_streamable; + static size_t size_of(bool _store_size = true) + { return IO::UnknownSize; } + + static size_t size_of(const value_type& _v, bool _store_size = true) + { + if(binary::size_of() != IO::UnknownSize) + { + unsigned int N = _v.size(); + auto res = sizeof(elem_type)*_v.size() + (_store_size? sizeof(decltype(N)) : 0); + return res; + } + else + { + size_t size = 0; + for(auto v : _v) + { + size += binary::size_of(v); + if(_store_size) + { + unsigned int N = _v.size(); + size += binary::size_of(); + } + } + + return size; + } + } + + static std::string string_for_value_type(void) { return get_string_for_type(value_type()); } + static + size_t store(std::ostream& _os, const value_type& _v, bool _swap=false, bool _store_size = true) { + size_t bytes=0; + if(_store_size) + { + unsigned int N = _v.size(); + bytes += binary::store( _os, N, _swap ); + } + if (_swap) + bytes += std::accumulate( _v.begin(), _v.end(), static_cast(0), + FunctorStore(_os,_swap) ); + else { + auto bytes_of_vec = size_of(_v, false); + bytes += bytes_of_vec; + _os.write( reinterpret_cast(&_v[0]), bytes_of_vec); + } + return _os.good() ? bytes : 0; + } + + static size_t restore(std::istream& _is, value_type& _v, bool _swap=false, bool _restore_size = true) { + + size_t bytes=0; + + if(_restore_size) + { + unsigned int size_of_vec; + bytes += binary::restore(_is, size_of_vec, _swap); + _v.resize(size_of_vec); + } + + if ( _swap) + bytes += std::accumulate( _v.begin(), _v.end(), size_t(0), + FunctorRestore(_is, _swap) ); + else + { + auto bytes_of_vec = size_of(_v, false); + bytes += bytes_of_vec; + _is.read( reinterpret_cast(&_v[0]), bytes_of_vec ); + } + return _is.good() ? bytes : 0; + } +}; + #include #include diff --git a/src/OpenMesh/Core/IO/SR_binary_vector_of_bool.inl b/src/OpenMesh/Core/IO/SR_binary_vector_of_bool.inl index f5f9d19c..8362cd93 100644 --- a/src/OpenMesh/Core/IO/SR_binary_vector_of_bool.inl +++ b/src/OpenMesh/Core/IO/SR_binary_vector_of_bool.inl @@ -7,14 +7,14 @@ template <> struct binary< std::vector > static const bool is_streamable = true; - static size_t size_of(void) { return UnknownSize; } - static size_t size_of(const value_type& _v) + static size_t size_of(bool _store_size = true) { return UnknownSize; } + static size_t size_of(const value_type& _v, bool _store_size = true) { return _v.size() / 8 + ((_v.size() % 8)!=0); } static std::string string_for_value_type(void) { return get_string_for_type(value_type()); } static - size_t store( std::ostream& _ostr, const value_type& _v, bool ) + size_t store( std::ostream& _ostr, const value_type& _v, bool, bool _store_size = true ) { size_t bytes = 0; @@ -61,7 +61,7 @@ template <> struct binary< std::vector > } static - size_t restore( std::istream& _istr, value_type& _v, bool ) + size_t restore( std::istream& _istr, value_type& _v, bool, bool _restore_size = true ) { size_t bytes = 0; diff --git a/src/OpenMesh/Core/IO/SR_binary_vector_of_fundamentals.inl b/src/OpenMesh/Core/IO/SR_binary_vector_of_fundamentals.inl deleted file mode 100644 index ba995e0f..00000000 --- a/src/OpenMesh/Core/IO/SR_binary_vector_of_fundamentals.inl +++ /dev/null @@ -1,53 +0,0 @@ - -#define BINARY_VECTOR( T ) \ -template <> struct binary< std::vector< T > > { \ - typedef std::vector< T > value_type; \ - typedef value_type::value_type elem_type; \ - \ - static const bool is_streamable = true; \ - \ - static size_t size_of(void) \ - { return IO::UnknownSize; } \ - \ - static size_t size_of(const value_type& _v) \ - { return sizeof(elem_type)*_v.size(); } \ - static std::string string_for_value_type(void) { return get_string_for_type(value_type()); }\ - static \ - size_t store(std::ostream& _os, const value_type& _v, bool _swap=false) { \ - size_t bytes=0; \ - \ - if (_swap) \ - bytes = std::accumulate( _v.begin(), _v.end(), bytes, \ - FunctorStore(_os,_swap) ); \ - else { \ - bytes = size_of(_v); \ - _os.write( reinterpret_cast(&_v[0]), bytes ); \ - } \ - return _os.good() ? bytes : 0; \ - } \ - \ - static size_t restore(std::istream& _is, value_type& _v, bool _swap=false) { \ - size_t bytes=0; \ - \ - if ( _swap) \ - bytes = std::accumulate( _v.begin(), _v.end(), size_t(0), \ - FunctorRestore(_is, _swap) ); \ - else \ - { \ - bytes = size_of(_v); \ - _is.read( reinterpret_cast(&_v[0]), bytes ); \ - } \ - return _is.good() ? bytes : 0; \ - } \ -} - -BINARY_VECTOR( short ); -BINARY_VECTOR( unsigned short ); -BINARY_VECTOR( int ); -BINARY_VECTOR( unsigned int ); -BINARY_VECTOR( long ); -BINARY_VECTOR( unsigned long ); -BINARY_VECTOR( float ); -BINARY_VECTOR( double ); - -#undef BINARY_VECTOR diff --git a/src/OpenMesh/Core/IO/SR_binary_vector_of_string.inl b/src/OpenMesh/Core/IO/SR_binary_vector_of_string.inl index d420b30a..43d3f0bc 100644 --- a/src/OpenMesh/Core/IO/SR_binary_vector_of_string.inl +++ b/src/OpenMesh/Core/IO/SR_binary_vector_of_string.inl @@ -18,21 +18,21 @@ template <> struct binary< std::vector< std::string > > // struct binary interface - static size_t size_of(void) { return UnknownSize; } + static size_t size_of(bool _store_size = true) { return UnknownSize; } - static size_t size_of(const value_type& _v) + static size_t size_of(const value_type& _v, bool _store_size = true) { return std::accumulate( _v.begin(), _v.end(), size_t(0), Sum() ); } static std::string string_for_value_type(void) { return get_string_for_type(value_type()); } static - size_t store(std::ostream& _os, const value_type& _v, bool _swap=false) + size_t store(std::ostream& _os, const value_type& _v, bool _swap=false, bool _store_size = true) { return std::accumulate( _v.begin(), _v.end(), size_t(0), FunctorStore(_os, _swap) ); } static - size_t restore(std::istream& _is, value_type& _v, bool _swap=false) + size_t restore(std::istream& _is, value_type& _v, bool _swap=false, bool _restore_size = true) { return std::accumulate( _v.begin(), _v.end(), size_t(0), FunctorRestore(_is, _swap) );