MarlinMT  0.1.0
Exceptions.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std headers
4 #include <exception>
5 #include <string>
6 
7 // -- high level exception macros
8 #define MARLINMT_THROW( message ) throw marlinmt::Exception( __LINE__, __PRETTY_FUNCTION__, __FILE__, message )
9 #define MARLINMT_THROW_T( ExceptionType, message ) throw ExceptionType( __LINE__, __PRETTY_FUNCTION__, __FILE__, message )
10 #define MARLINMT_RETHROW( orig, message ) throw marlinmt::Exception( orig, __LINE__, __PRETTY_FUNCTION__, __FILE__, message )
11 #define MARLINMT_RETHROW_T( ExceptionType, orig, message ) throw ExceptionType( orig, __LINE__, __PRETTY_FUNCTION__, __FILE__, message )
12 
13 // -- specific marlinmt exception
14 #define MARLINMT_SKIP_EVENT( proc ) MARLINMT_THROW_T( marlinmt::SkipEventException, proc->name() )
15 #define MARLINMT_STOP_PROCESSING( proc ) MARLINMT_THROW_T( marlinmt::StopProcessingException, proc->name() )
16 
17 // -- custom exception class definition macro
18 #define MARLINMT_DEFINE_EXCEPTION( ClassName ) \
19  class ClassName : public marlinmt::Exception { \
20  public: \
21  ClassName() = delete ; \
22  ClassName( const ClassName & ) = default ; \
23  ~ClassName() = default ; \
24  inline ClassName( const std::string &message ) : \
25  marlinmt::Exception( std::string( #ClassName ) + " - " + message ) {} \
26  inline ClassName( unsigned int line, const std::string &func, const std::string &fname, const std::string &message ) : \
27  marlinmt::Exception( line, func, fname, std::string( #ClassName ) + " - " + message ) {} \
28  template <typename T> \
29  inline ClassName( const T &rhs, unsigned int line, const std::string &func, const std::string &fname, const std::string &message ) : \
30  marlinmt::Exception( rhs, line, func, fname, std::string( #ClassName ) + " - " + message ) {} \
31  }
32 
33 namespace marlinmt {
34 
60  class Exception : public std::exception {
61  public:
62  Exception() = delete ;
63  Exception( const Exception & ) = default ;
64  virtual ~Exception() = default ;
65 
71  Exception( const std::string &message ) ;
72 
81  Exception( unsigned int line, const std::string &func, const std::string &fname, const std::string &message ) ;
82 
92  template <typename T>
93  Exception( const T &rhs, unsigned int line, const std::string &func, const std::string &fname, const std::string &message ) ;
94 
98  const char* what() const noexcept override ;
99 
100  protected:
109  std::string createMessage( unsigned int line, const std::string &func, const std::string &fname, const std::string &message ) const ;
110 
120  std::string createMessage( const std::string &previous, unsigned int line, const std::string &func, const std::string &fname, const std::string &message ) const ;
121 
122  protected:
124  const std::string _message {} ;
125  };
126 
127  //--------------------------------------------------------------------------
128  //--------------------------------------------------------------------------
129 
130  template <typename T>
131  inline Exception::Exception( const T &rhs, unsigned int line, const std::string &func, const std::string &fname, const std::string &message ) :
132  _message( createMessage(rhs.what(), line, func, fname, message) ) {
133  /* nop */
134  }
135 
137  MARLINMT_DEFINE_EXCEPTION( SkipEventException ) ;
138  MARLINMT_DEFINE_EXCEPTION( StopProcessingException ) ;
139  MARLINMT_DEFINE_EXCEPTION( ParseException ) ;
140  MARLINMT_DEFINE_EXCEPTION( BookStoreException );
141 
142 } // end namespace
const char * what() const noexcept override
Get the full exception message.
Definition: Exceptions.cc:19
std::string createMessage(unsigned int line, const std::string &func, const std::string &fname, const std::string &message) const
Helper function creating the full exception message.
Definition: Exceptions.cc:25
MARLINMT_DEFINE_EXCEPTION(SkipEventException)
Definition of Marlin exceptions.
const std::string _message
< The full exception message
Definition: Exceptions.h:124
virtual ~Exception()=default
Exception class.
Definition: Exceptions.h:60