EUDAQ
 All Classes Namespaces Files Functions Variables Pages
Exception.hh
1 #ifndef EUDAQ_INCLUDED_Exception
2 #define EUDAQ_INCLUDED_Exception
3 
4 #include "eudaq/Utils.hh"
5 #include <exception>
6 #include <string>
7 #include "Platform.hh"
8 
9 #ifndef EUDAQ_FUNC
10 #define EUDAQ_FUNC ""
11 #endif
12 
13 #define EUDAQ_THROWX(exc, msg) \
14  throw ::eudaq::InitException(exc(msg), __FILE__, __LINE__, EUDAQ_FUNC)
15 #define EUDAQ_THROW(msg) EUDAQ_THROWX(::eudaq::LoggedException, (msg))
16 #define EUDAQ_THROW_NOLOG(msg) EUDAQ_THROWX(::eudaq::Exception, (msg))
17 
18 #define EUDAQ_EXCEPTIONX(name, base) \
19  class name : public base { \
20  public: \
21  name(const std::string &msg) : base(msg) {} \
22  }
23 
24 #define EUDAQ_EXCEPTION(name) EUDAQ_EXCEPTIONX(name, ::eudaq::Exception)
25 
26 #ifdef PF_WIN32
27 #ifdef _MSC_VER
28 #pragma warning(push)
29 #pragma warning(disable: 4275)
30 #endif
31 #endif
32 
33 namespace eudaq {
34 
35  class DLLEXPORT Exception : public std::exception {
36  public:
37  Exception(const std::string &msg);
38  const char *what() const throw() {
39  if (m_text.length() == 0)
40  make_text();
41  return m_text.c_str();
42  }
43  // This shouldn't really be const, but it must be callable on temporary
44  // objects...
45  const Exception &SetLocation(const std::string &file = "",
46  unsigned line = 0,
47  const std::string &func = "") const;
48  virtual ~Exception() throw() {}
49 
50  protected:
51  std::string m_msg;
52 
53  private:
54  void make_text() const;
55  mutable std::string m_text;
56  mutable std::string m_file, m_func;
57  mutable unsigned m_line;
58  };
59 
60  class DLLEXPORT LoggedException : public Exception {
61  public:
62  LoggedException(const std::string &msg);
63  void Log() const;
64  virtual ~LoggedException() throw();
65 
66  private:
67  mutable bool m_logged;
68  };
69 
70  namespace {
71  void do_log(const Exception &) {}
72  void do_log(const LoggedException &e) { e.Log(); }
73  }
74 
75  template <typename T>
76  const T &InitException(const T &e, const std::string &file, int line = 0,
77  const std::string func = "") {
78  e.SetLocation(file, line, func);
79  do_log(e); // If it is a LoggedException, send it to be logged already
80  return e;
81  }
82 
83  // Some useful predefined exceptions
84  EUDAQ_EXCEPTION(FileNotFoundException);
85  EUDAQ_EXCEPTION(FileExistsException);
86  EUDAQ_EXCEPTION(FileNotWritableException);
87  EUDAQ_EXCEPTION(FileReadException);
88  EUDAQ_EXCEPTION(FileWriteException);
89  EUDAQ_EXCEPTION(FileFormatException);
90  EUDAQ_EXCEPTION(SyncException);
91  EUDAQ_EXCEPTION(CommunicationException);
92  EUDAQ_EXCEPTIONX(BusError, CommunicationException);
93 }
94 
95 #ifdef PF_WIN32
96 #ifdef _MSC_VER
97 #pragma warning(pop)
98 #endif
99 #endif
100 
101 #endif // EUDAQ_INCLUDED_Exception
Definition: Exception.hh:35
Definition: Exception.hh:60