ThePEG  1.8.0
Exception.h
1 // -*- C++ -*-
2 //
3 // Exception.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_Exception_H
10 #define ThePEG_Exception_H
11 // This is the declaration of the Exception class.
12 
13 #include <exception>
14 #include "ThePEG/Config/ThePEG.h"
15 #include "Exception.fh"
16 #include <string>
17 #include <iosfwd>
18 
19 extern "C" {
20  void breakThePEG();
21 }
22 
23 namespace ThePEG {
24 
44 class Exception: public exception {
45 
46 public:
47 
51  enum Severity {
53  info,
68  };
69 
70 public:
71 
77  Exception(const string & str, Severity sev);
78 
82  Exception() : handled(false), theSeverity(unknown) { breakThePEG(); }
83 
87  Exception(const Exception & ex)
88  : std::exception(ex), theMessage(ex.message()),
90  {
91  ex.handle();
92  }
93 
97  virtual ~Exception() throw();
98 
99 public:
100 
104  const Exception & operator=(const Exception & ex) {
105  handled = ex.handled;
106  theMessage << ex.message();
107  theSeverity = ex.severity();
108  ex.handle();
109  return *this;
110  }
111 
115  bool operator==(const Exception & ex) const {
116  return ( message() == ex.message() && severity() == ex.severity() );
117  }
118 
123  bool operator<(const Exception & ex) const {
124  return ( severity() == ex.severity() ?
125  ( message() < ex.message() ) :
126  ( severity() < ex.severity() ) );
127  }
128 
129 public:
130 
134  virtual const char* what() const throw() {
135  static string str;
136  str = message();
137  return str.c_str();
138  }
139 
143  string message() const {
144  string mess = theMessage.str();
145  return mess.empty() ? string("Error message not provided.") : mess;
146  }
147 
151  void writeMessage(ostream & os = *errstream) const;
152 
156  Severity severity() const { return theSeverity; }
157 
161  void handle() const { handled = true; }
162 
166  template <typename T>
167  Exception & operator<<(const T & t) {
168  theMessage << t;
169  return *this;
170  }
171 
176  severity(sev);
177  return *this;
178  }
179 
180 protected:
181 
185  void severity(Severity);
186 
190  mutable ostringstream theMessage;
191 
192 private:
193 
197  mutable bool handled;
198 
203 
207  static ostream * errstream;
208 
209 public:
210 
215  static bool noabort;
216 
217 };
218 
219 }
220 
221 #endif /* ThePEG_Exception_H */