Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:32

0001 // -*- C++ -*-
0002 //
0003 // Exception.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef ThePEG_Exception_H
0010 #define ThePEG_Exception_H
0011 // This is the declaration of the Exception class.
0012 
0013 #include <exception>
0014 #include "ThePEG/Config/ThePEG.h"
0015 #include "Exception.fh"
0016 #include <string>
0017 #include <iosfwd>
0018 
0019 extern "C" {
0020   void breakThePEG();
0021 }
0022 
0023 namespace ThePEG {
0024 
0025 /**
0026  * <code>Exception</code> is the base class for all exceptions to be
0027  * used in ThePEG. It is derived from <code>std::exception</code> and
0028  * adds information about the severity of the exception to indicate to
0029  * the Repository and EventGenrator how to act on it.
0030  *
0031  * To throw an exception one should inherit from
0032  * <code>Exception</code> and add information in the constructor of
0033  * the base class. Alternatively one can use the <code>operator<<
0034  * </code> operator on a default constructed <code>Exception</code> to
0035  * add information as for a standard <code>ostream</code> object, in
0036  * which case one should always end with adding an enum of the type
0037  * <code>Exception::Severity</code> to indicate the severity of the
0038  * exception e.g.<br> <code>Exception() << "Something went wrong." <<
0039  * Exception::eventerror</code>.
0040  *
0041  * @see Repository
0042  * @see EventGenrator
0043  */
0044 class Exception: public exception {
0045 
0046 public:
0047 
0048   /**
0049    * The levels of severity.
0050    */
0051   enum Severity {
0052     unknown,      /**< Unknown severity */
0053     info,         /**< Not severe (but the user should be
0054            *   informed). */
0055     warning,      /**< Possibly severe, (the user should be
0056            *   warned). */
0057     setuperror,   /**< Command failed during setup phase,
0058            *   execution is continued. */
0059     eventerror,   /**< Possibly severe, (the event being
0060            *   generated should be discarded). */
0061     runerror,     /**< Severe error, (the run should be
0062            *   terminated). */
0063     maybeabort,   /**< Severe error, (the run should be
0064            *   terminated, possibly dumping core). */
0065     abortnow      /**< Severe error, (the run is aborted
0066            *   immediately, before the exception is
0067            *   thrown). */
0068   };
0069 
0070 public:
0071 
0072   /**
0073    * Standard constructor.
0074    * @param str an error message.
0075    * @param sev the severity.
0076    */
0077   Exception(const string & str, Severity sev);
0078 
0079   /**
0080    * Default constructor.
0081    */
0082   Exception() : handled(false), theSeverity(unknown) { breakThePEG(); }
0083 
0084   /**
0085    * The copy constructor.
0086    */
0087   Exception(const Exception & ex) 
0088     : std::exception(ex), theMessage(ex.message()), 
0089       handled(ex.handled), theSeverity(ex.severity()) 
0090   {
0091     ex.handle();
0092   }
0093 
0094   /**
0095    * The destructor
0096    */
0097   virtual ~Exception() noexcept;
0098 
0099 public:
0100 
0101   /**
0102    * Assignment.
0103    */
0104   const Exception & operator=(const Exception & ex) {
0105     handled = ex.handled;
0106     theMessage << ex.message();
0107     theSeverity = ex.severity();
0108     ex.handle();
0109     return *this;
0110   }
0111 
0112   /**
0113    * Comparison
0114    */
0115   bool operator==(const Exception & ex) const {
0116     return ( message() == ex.message() && severity() == ex.severity() );
0117   }
0118 
0119   /**
0120    * Compare severity. If equal compare error message
0121    * lexicographically.
0122    */
0123   bool operator<(const Exception & ex) const {
0124     return ( severity() == ex.severity() ? 
0125          ( message() < ex.message() ) :
0126          ( severity() < ex.severity() ) );
0127   }
0128 
0129 public:
0130 
0131   /**
0132    * Return the error message.
0133    */
0134   virtual const char* what() const noexcept {
0135     static string str;
0136     str = message();
0137     return str.c_str();
0138   }
0139 
0140   /**
0141    * Return the error message.
0142    */
0143   string message() const {
0144     string mess = theMessage.str();
0145     return mess.empty() ? string("Error message not provided.") : mess;
0146   }
0147 
0148   /**
0149    * Write the error message to a stream.
0150    */
0151   void writeMessage(ostream & os = *errstream) const;
0152 
0153   /**
0154    * Return the severity.
0155    */
0156   Severity severity() const { return theSeverity; }
0157 
0158   /**
0159    * Indicate that this exception has been taken care of.
0160    */
0161   void handle() const { handled = true; }
0162 
0163   /**
0164    * Add info to the exception message.
0165    */
0166   template <typename T>
0167   Exception & operator<<(const T & t) {
0168     theMessage << t;
0169     return *this;
0170   }
0171 
0172   /**
0173    * Set the severity for the exception.
0174    */
0175   Exception & operator<<(Severity sev) {
0176     severity(sev);
0177     return *this;
0178   }
0179 
0180 protected:
0181 
0182   /**
0183    * set the severity.
0184    */
0185   void severity(Severity);
0186 
0187   /**
0188    * Stream to write the error message to.
0189    */
0190   mutable ostringstream theMessage;
0191 
0192 private:
0193 
0194   /**
0195    * True if this exception has been taken care of.
0196    */
0197   mutable bool handled;
0198 
0199   /**
0200    * The severity.
0201    */
0202   Severity theSeverity;
0203 
0204   /**
0205    * The default stream to write the error message if unhandled.
0206    */
0207   static ostream * errstream;
0208 
0209 public:
0210 
0211   /**
0212    * If this flag is set, all abortnow and maybeabort severities will
0213    * be treated as runerror.
0214    */
0215   static bool noabort;
0216 
0217 };
0218 
0219 }
0220 
0221 #endif /* ThePEG_Exception_H */