From 67d4cba342d524feeb8e863ee8360eaf194d0e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkjak@fjfi.cvut.cz> Date: Fri, 12 Apr 2019 22:19:58 +0200 Subject: [PATCH] Refactoring ArrayIO --- src/TNL/Containers/Algorithms/ArrayIO.h | 75 ++++++++++++++++--------- src/TNL/Containers/Array.hpp | 9 +-- src/TNL/Containers/ArrayView.hpp | 3 +- 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/TNL/Containers/Algorithms/ArrayIO.h b/src/TNL/Containers/Algorithms/ArrayIO.h index 77e82355f8..a97fc755fd 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 34fb38e629..d0cbf39651 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 cca4ceef5e..030f29e64a 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, -- GitLab