|
|
|||
File indexing completed on 2026-08-06 09:38:32
0001 // -*- C++ -*- 0002 // 0003 // Throw.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_Throw_H 0010 #define ThePEG_Throw_H 0011 // This is the declaration of the Throw class. 0012 0013 #include "ThePEG/Config/ThePEG.h" 0014 #include "ThePEG/Utilities/Exception.h" 0015 #include "ThePEG/Repository/CurrentGenerator.h" 0016 #include "ThePEG/Repository/Repository.h" 0017 0018 0019 namespace ThePEG { 0020 /** 0021 * Helper function to make it easier to throw exceptions. The template 0022 * argument should be a class inheriting from Exception. In the 0023 * constructor, an object of this Exception class is created and 0024 * afterwards a message may be added using ostream-like output 0025 * (<<). If a Exception::Severity value is output with << 0026 * the Exception object is assumed to be complete and the exception is 0027 * actually thrown, except if the Exception::Severity value 0028 * Exception::warning was specified, in which case the Exception 0029 * object is treated as a warning which is logged with the current 0030 * EventGenerator. If no current EventGenerator is present the warning 0031 * message is instead written to std::cerr. If no Exception::Severity 0032 * is specified, the Exception object is treated as a warning 0033 * when the Throw object is destroyed. 0034 * 0035 * Assuming you have an Exception class called MyEx the Throw class is 0036 * used as follows:<br><code>Throw<MyEx>>() << "My error 0037 * message" << Exception::eventerror;</code><br> This will throw 0038 * an exception and the current event will be discarded. Changing 0039 * <code>Exception::eventerror</code> to 0040 * <code>Exception::warning</code> will write out a warning, but no 0041 * proper exception is thrown. 0042 */ 0043 template <typename Ex> 0044 struct Throw { 0045 public: 0046 /** 0047 * Standard constructor creating an internal Exception object. 0048 */ 0049 Throw(): ex(Ex()), handled(false) {} 0050 0051 /** 0052 * Add information to the current Exception object. 0053 */ 0054 template <typename T> Throw & operator<<(const T & t) { 0055 ex << t; 0056 return *this; 0057 } 0058 0059 private: 0060 /** 0061 * Write warning messages to the current EventGenerator. If no 0062 * current EventGenerator is present, the warning message is instead 0063 * written to std::cerr. 0064 */ 0065 void writeWarning() { 0066 if ( CurrentGenerator::isVoid() ) { 0067 Repository::clog() << ex.message() << endl; 0068 ex.handle(); 0069 } else { 0070 CurrentGenerator::current().logWarning(ex); 0071 } 0072 } 0073 0074 public: 0075 /** 0076 * Specify the Exception::Severity of the exception. If this is 0077 * Exception::warning, the exception will not be thown, instead it 0078 * will be logged with writeWarning(). All other seveities will cause 0079 * the exception to be thrown immediately. 0080 */ 0081 void operator<<(Exception::Severity sev) { 0082 handled = true; 0083 ex << sev; 0084 if ( sev != Exception::warning && sev != Exception::info ) { 0085 throw ex; 0086 } else { 0087 writeWarning(); 0088 } 0089 } 0090 0091 /** 0092 * The destructor will throw the exception if it has not been handled. 0093 */ 0094 ~Throw() { 0095 if ( !handled ) { 0096 ex << Exception::warning; 0097 writeWarning(); 0098 } 0099 } 0100 0101 /** 0102 * The ExceptionObject to be thrown. 0103 */ 0104 Ex ex; 0105 0106 /** 0107 * If true, the exception has been handled and should not be thrown 0108 * in the destructor. 0109 */ 0110 bool handled; 0111 }; 0112 0113 0114 } 0115 0116 #endif /* ThePEG_Throw_H */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|