Commit 67d4cba3 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Refactoring ArrayIO

parent 9aa442c4
Loading
Loading
Loading
Loading
+47 −28
Original line number Diff line number Diff line
@@ -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 ) )
      Index i;
      try
      {
            std::cerr << "I was not able to save " << i << "-th of " << elements << " elements." << std::endl;
            return false;
         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." );
      }
      return true;
   }

   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 ) )
      Index i = 0;
      try
      {
         for( i = 0; i < elements; i++ )
            data[ i ].load( file );
      }
      catch(...)
      {
            std::cerr << "I was not able to load " << i << "-th of " << elements << " elements." << std::endl;
            return false;
         throw Exceptions::FileDeserializationError( file.getFileName(), "unable to read the " + std::to_string(i) + "-th array element of " + std::to_string(elements) + " from the file." );
      }
      return true;
   }
};

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 )
   {
      if( elements == 0 )
         return;
      try
      {
         file.save< Value, Value, Device >( data, elements );
      return true;
      }
      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 )
   {
      if( elements == 0 )
         return;
      try
      {
         file.load< Value, Value, Device >( data, elements );
      return true;
      }

      catch(...)
      {
         throw Exceptions::FileDeserializationError( file.getFileName(), "unable to read " + std::to_string(elements) + " array elements from the file." );
      }
   }
};

} // namespace Algorithms
+3 −6
Original line number Diff line number Diff line
@@ -658,7 +658,6 @@ 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 );
}

@@ -675,7 +674,6 @@ 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 );
}

@@ -697,7 +695,6 @@ 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 );
}

+1 −2
Original line number Diff line number Diff line
@@ -349,7 +349,6 @@ 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 );
}