Commit 35b53218 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

getObjectType is void and it throws exceptions.

parent adbe8e81
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
/***************************************************************************
                          NotTNLFile.h  -  description
                             -------------------
    begin                : Mar 4, 2019
    copyright            : (C) 2019 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

// Implemented by: Tomas Oberhuber

#pragma once

#include <string>
#include <stdexcept>

namespace TNL {
namespace Exceptions {

class NotTNLFile
   : public std::runtime_error
{
public:
   NotTNLFile()
   : std::runtime_error( "Wring magic number found in a binary file. It is not TNL compatible file." )
   {}
};

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

/* See Copyright Notice in tnl/Copyright */

// Implemented by: Tomas Oberhuber

#pragma once

#include <string>
#include <stdexcept>

namespace TNL {
namespace Exceptions {

class ObjectTypeDetectionFailure
   : public std::runtime_error
{
public:
   ObjectTypeDetectionFailure( const String& fileName, const String& objectType )
   : std::runtime_error( "Failed to detect " + objectType + " in file " + fileName + "." )
   {}
};

} // namespace Exceptions
} // namespace TNL
+8 −3
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@

#include <TNL/String.h>
#include <TNL/Object.h>
#include <TNL/Exceptions/ObjectTypeDetectionFailure.h>
#include <TNL/Meshes/Readers/EntityShape.h>

namespace TNL {
@@ -28,9 +29,13 @@ public:
      this->fileName = fileName;

      String objectType;
      if( ! getObjectType( fileName, objectType ) ) {
         std::cerr << "Failed to detect the mesh type from the file " << fileName << "." << std::endl;
         return EXIT_FAILURE;
      try
      {
         getObjectType( fileName, objectType );
      }
      catch( ... )
      {
         throw Exceptions::ObjectTypeDetectionFailure( fileName, "mesh" );
      }

      const std::vector< String > parsedMeshType = parseObjectType( objectType );
+18 −3
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@ namespace TNL {
 * Objects like numerical meshes, matrices large vectors etc. are inherited by 
 * this class. This class introduces virtual method \ref getType which is 
 * supposed to tell the object type in a C++ style.
 * 
 * Since the virtual destructor is not defined as \ref __cuda_callable__, 
 * objects inherited from Object should not be created in CUDA kernels.
 */
class Object
{
@@ -118,15 +121,27 @@ class Object
       */
      bool boundLoad( const String& fileName );
      
      /// Destructor.
      /**
       * \brief Destructor.
       * 
       * Since it is not defined as \ref __cuda_callable__, objects inherited
       * from Object should not be created in CUDA kernels.
       */
#ifndef HAVE_MIC
      virtual ~Object(){};
#endif
};

bool getObjectType( File& file, String& type );
/**
 * \brief Extracts object type from a binary file.
 * 
 * @param file
 * @param type
 * @return 
 */
void getObjectType( File& file, String& type );

bool getObjectType( const String& file_name, String& type );
void getObjectType( const String& file_name, String& type );

std::vector< String >
parseObjectType( const String& objectType );
+17 −26
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#include <cstring>

#include <TNL/Object.h>
#include <TNL/Exceptions/NotTNLFile.h>

namespace TNL {

@@ -51,8 +52,7 @@ inline bool Object :: save( File& file ) const
inline bool Object::load( File& file )
{
   String objectType;
   if( ! getObjectType( file, objectType ) )
      return false;
   getObjectType( file, objectType );
   if( objectType != this->getSerializationTypeVirtual() )
   {
      std::cerr << "Given file contains instance of " << objectType << " but " << getSerializationTypeVirtual() << " is expected." << std::endl;
@@ -99,29 +99,20 @@ inline bool Object :: boundLoad( const String& fileName )
   return this->boundLoad( file );
}


inline bool getObjectType( File& file, String& type )
inline void getObjectType( File& file, String& type )
{
   char mn[ 10 ];
   file.read( mn, strlen( magic_number ) );
   if( strncmp( mn, magic_number, 5 ) != 0 )
   {
       std::cout << "Not a TNL file (wrong magic number)." << std::endl;
       return false;
   }
      throw Exceptions::NotTNLFile();
   file >> type;
   return true;
}

inline bool getObjectType( const String& fileName, String& type )
inline void getObjectType( const String& fileName, String& type )
{
   File binaryFile;
   if( ! binaryFile.open( fileName, IOMode::read ) )
   {
      std::cerr << "I am not able to open the file " << fileName << " for detecting the object inside!" << std::endl;
      return false;
   }
   return getObjectType( binaryFile, type );
   binaryFile.open( fileName, IOMode::read );
   getObjectType( binaryFile, type );
}

inline std::vector< String >
Loading