diff --git a/src/TNL/Containers/Algorithms/ArrayIO.h b/src/TNL/Containers/Algorithms/ArrayIO.h index 77e82355f82d414dc77224633ed5bc79a6f4fb08..a97fc755fd6184ac8d7868c878768d827d7e4339 100644 --- a/src/TNL/Containers/Algorithms/ArrayIO.h +++ b/src/TNL/Containers/Algorithms/ArrayIO.h @@ -23,66 +23,85 @@ template< typename Value, typename Device, typename Index, bool Elementwise = std::is_base_of< Object, Value >::value > -class ArrayIO +struct ArrayIO {}; template< typename Value, typename Device, typename Index > -class ArrayIO< Value, Device, Index, true > +struct ArrayIO< Value, Device, Index, true > { public: - static bool save( File& file, + static void save( File& file, const Value* data, const Index elements ) { - for( Index i = 0; i < elements; i++ ) - if( ! data[ i ].save( file ) ) - { - std::cerr << "I was not able to save " << i << "-th of " << elements << " elements." << std::endl; - return false; - } - return true; + Index i; + try + { + for( i = 0; i < elements; i++ ) + data[ i ].save( file ); + } + catch(...) + { + throw Exceptions::FileSerializationError( file.getFileName(), "unable to write the " + std::to_string(i) + "-th array element of " + std::to_string(elements) + " into the file." ); + } } - static bool load( File& file, + static void load( File& file, Value* data, const Index elements ) { - for( Index i = 0; i < elements; i++ ) - if( ! data[ i ].load( file ) ) - { - std::cerr << "I was not able to load " << i << "-th of " << elements << " elements." << std::endl; - return false; - } - return true; + Index i = 0; + try + { + for( i = 0; i < elements; i++ ) + data[ i ].load( file ); + } + catch(...) + { + throw Exceptions::FileDeserializationError( file.getFileName(), "unable to read the " + std::to_string(i) + "-th array element of " + std::to_string(elements) + " from the file." ); + } } }; template< typename Value, typename Device, typename Index > -class ArrayIO< Value, Device, Index, false > +struct ArrayIO< Value, Device, Index, false > { - public: - - static bool save( File& file, + static void save( File& file, const Value* data, const Index elements ) { - file.save< Value, Value, Device >( data, elements ); - return true; + if( elements == 0 ) + return; + try + { + file.save< Value, Value, Device >( data, elements ); + } + catch(...) + { + throw Exceptions::FileSerializationError( file.getFileName(), "unable to write " + std::to_string(elements) + " array elements into the file." ); + } } - static bool load( File& file, + static void load( File& file, Value* data, const Index elements ) { - file.load< Value, Value, Device >( data, elements ); - return true; + if( elements == 0 ) + return; + try + { + file.load< Value, Value, Device >( data, elements ); + } + catch(...) + { + throw Exceptions::FileDeserializationError( file.getFileName(), "unable to read " + std::to_string(elements) + " array elements from the file." ); + } } - }; } // namespace Algorithms diff --git a/src/TNL/Containers/Array.hpp b/src/TNL/Containers/Array.hpp index 34fb38e6296fdb96993cf46e3a24f749826c5072..d0cbf39651fce64c2d6013c931d43633b53eced3 100644 --- a/src/TNL/Containers/Array.hpp +++ b/src/TNL/Containers/Array.hpp @@ -658,8 +658,7 @@ void Array< Value, Device, Index >::save( File& file ) const { Object::save( file ); file.save( &this->size ); - if( this->size != 0 ) - Algorithms::ArrayIO< Value, Device, Index >::save( file, this->data, this->size ); + Algorithms::ArrayIO< Value, Device, Index >::save( file, this->data, this->size ); } template< typename Value, @@ -675,8 +674,7 @@ load( File& file ) if( _size < 0 ) throw Exceptions::FileDeserializationError( file.getFileName(), "invalid array size: " + std::to_string(_size) ); setSize( _size ); - if( _size ) - Algorithms::ArrayIO< Value, Device, Index >::load( file, this->data, this->size ); + Algorithms::ArrayIO< Value, Device, Index >::load( file, this->data, this->size ); } template< typename Value, @@ -697,8 +695,7 @@ boundLoad( File& file ) throw Exceptions::FileDeserializationError( file.getFileName(), "invalid array size: " + std::to_string(_size) + " (expected " + std::to_string( this->getSize() ) + ")." ); } else setSize( _size ); - if( _size ) - Algorithms::ArrayIO< Value, Device, Index >::load( file, this->data, this->size ); + Algorithms::ArrayIO< Value, Device, Index >::load( file, this->data, this->size ); } template< typename Value, diff --git a/src/TNL/Containers/ArrayView.hpp b/src/TNL/Containers/ArrayView.hpp index cca4ceef5ec9da608ec1f5e630ab74dc2ad70e95..030f29e64aa61abbfea6046abcd2529f3cab3afd 100644 --- a/src/TNL/Containers/ArrayView.hpp +++ b/src/TNL/Containers/ArrayView.hpp @@ -349,8 +349,7 @@ void ArrayView< Value, Device, Index >::save( File& file ) const { saveHeader( file, SerializationType::getType() ); file.save( &this->size ); - if( this->size != 0 ) - Algorithms::ArrayIO< Value, Device, Index >::save( file, this->data, this->size ); + Algorithms::ArrayIO< Value, Device, Index >::save( file, this->data, this->size ); } template< typename Value,