Commit b6d5bf7c authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

New way of (de)serialization of objects into TNL::File using operator<< and...

New way of (de)serialization of objects into TNL::File using operator<< and operator>> (starting with string)

This is nicer and safer (thanks to exceptions) than save/load functions.
Moreover, it breaks the last cyclic dependency between TNL classes.
parent 216d8284
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -10,11 +10,6 @@ namespace py = pybind11;

void export_String( py::module & m )
{
    // function pointers for overloaded methods
//    const char* (TNL::String::* _String_getString)() const = &TNL::String::getString;
    bool (TNL::String::* _String_save)(TNL::File &) const = &TNL::String::save;
    bool (TNL::String::* _String_load)(TNL::File &)       = &TNL::String::load;

    py::class_<TNL::String>(m, "String")
        .def(py::init<const char*>())
        .def(py::init<const char*, int>())
@@ -35,7 +30,7 @@ void export_String( py::module & m )
        .def("__len__", &TNL::String::getLength)
        // FIXME
//        .def("replace", &TNL::String::replace)
        .def("save", _String_save)
        .def("load", _String_load)
        .def("save", []( const TNL::String& str, TNL::File& file ){ file << str; } )
        .def("load", []( TNL::String& str, TNL::File& file ){ file >> str; } )
    ;
}
+31 −0
Original line number Diff line number Diff line
/***************************************************************************
                          FileDeserializationError.h  -  description
                             -------------------
    begin                : Nov 17, 2018
    copyright            : (C) 2018 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

// Implemented by: Jakub Klinkovsky

#pragma once

#include <string>
#include <stdexcept>

namespace TNL {
namespace Exceptions {

class FileDeserializationError
   : public std::runtime_error
{
public:
   FileDeserializationError( const std::string& objectType, const std::string& fileName )
   : std::runtime_error( "Failed to deserialize object of type '" + objectType + "' from file '" + fileName + "'." )
   {}
};

} // namespace Exceptions
} // namespace TNL
+31 −0
Original line number Diff line number Diff line
/***************************************************************************
                          FileSerializationError.h  -  description
                             -------------------
    begin                : Nov 17, 2018
    copyright            : (C) 2018 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

// Implemented by: Jakub Klinkovsky

#pragma once

#include <string>
#include <stdexcept>

namespace TNL {
namespace Exceptions {

class FileSerializationError
   : public std::runtime_error
{
public:
   FileSerializationError( const std::string& objectType, const std::string& fileName )
   : std::runtime_error( "Failed to serialize object of type '" + objectType + "' into file '" + fileName + "'." )
   {}
};

} // namespace Exceptions
} // namespace TNL
+6 −0
Original line number Diff line number Diff line
@@ -151,6 +151,12 @@ protected:
/// \param fileName Name of the file that user wants to find in the PC.
bool fileExists( const String& fileName );

// serialization of strings
File& operator<<( File& file, const std::string& str );

// deserialization of strings
File& operator>>( File& file, std::string& str );

} // namespace TNL

#include <TNL/File_impl.h>
+27 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#include <TNL/Assert.h>
#include <TNL/Exceptions/CudaSupportMissing.h>
#include <TNL/Exceptions/MICSupportMissing.h>
#include <TNL/Exceptions/FileSerializationError.h>
#include <TNL/Exceptions/FileDeserializationError.h>

namespace TNL {

@@ -247,4 +249,29 @@ inline bool fileExists( const String& fileName )
   return ! file.fail();
}


// serialization of strings
inline File& operator<<( File& file, const std::string& str )
{
   const int len = str.size();
   if( ! file.write( &len ) )
      throw Exceptions::FileSerializationError( getType< int >(), file.getFileName() );
   if( ! file.write( str.c_str(), len ) )
      throw Exceptions::FileSerializationError( "String", file.getFileName() );
   return file;
}

// deserialization of strings
inline File& operator>>( File& file, std::string& str )
{
   int length;
   if( ! file.read( &length ) )
      throw Exceptions::FileDeserializationError( getType< int >(), file.getFileName() );
   char buffer[ length ];
   if( length && ! file.read( buffer, length ) )
      throw Exceptions::FileDeserializationError( "String", file.getFileName() );
   str.assign( buffer, length );
   return file;
}

} // namespace TNL
Loading