File indexing completed on 2024-04-06 12:01:17
0001 #ifndef CommonTools_Utils_Exception_h
0002 #define CommonTools_Utils_Exception_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <sstream>
0024 #include "boost/spirit/include/classic_exceptions.hpp"
0025
0026
0027
0028
0029 namespace reco {
0030 namespace parser {
0031 enum SyntaxErrors { kSyntaxError, kMissingClosingParenthesis, kSpecialError };
0032
0033 typedef boost::spirit::classic::parser_error<reco::parser::SyntaxErrors> BaseException;
0034
0035
0036 inline const char* baseExceptionWhat(const BaseException& e) {
0037 switch (e.descriptor) {
0038 case kMissingClosingParenthesis:
0039 return "Missing close parenthesis.";
0040 case kSyntaxError:
0041 return "Syntax error.";
0042 case kSpecialError:
0043 default:
0044 break;
0045 }
0046 return e.what();
0047 }
0048 class Exception : public BaseException {
0049 public:
0050 Exception(const char* iIterator) : BaseException(iIterator, kSpecialError) {}
0051 Exception(const Exception& iOther) : BaseException(iOther) { ost_ << iOther.ost_.str(); }
0052 ~Exception() throw() override {}
0053
0054
0055 const char* what() const throw() override {
0056 what_ = ost_.str();
0057 return what_.c_str();
0058 }
0059
0060
0061
0062
0063
0064 template <class T>
0065 friend Exception& operator<<(Exception&, const T&);
0066 template <class T>
0067 friend Exception& operator<<(const Exception&, const T&);
0068 friend Exception& operator<<(Exception&, std::ostream& (*f)(std::ostream&));
0069 friend Exception& operator<<(const Exception&, std::ostream& (*f)(std::ostream&));
0070 friend Exception& operator<<(Exception&, std::ios_base& (*f)(std::ios_base&));
0071 friend Exception& operator<<(const Exception&, std::ios_base& (*f)(std::ios_base&));
0072
0073 private:
0074
0075
0076
0077 std::ostringstream ost_;
0078 mutable std::string what_;
0079 };
0080
0081 template <class T>
0082 inline Exception& operator<<(Exception& e, const T& iT) {
0083 e.ost_ << iT;
0084 return e;
0085 }
0086 template <class T>
0087 inline Exception& operator<<(const Exception& e, const T& iT) {
0088 return operator<<(const_cast<Exception&>(e), iT);
0089 }
0090 inline Exception& operator<<(Exception& e, std::ostream& (*f)(std::ostream&)) {
0091 f(e.ost_);
0092 return e;
0093 }
0094 inline Exception& operator<<(const Exception& e, std::ostream& (*f)(std::ostream&)) {
0095 f(const_cast<Exception&>(e).ost_);
0096 return const_cast<Exception&>(e);
0097 }
0098 inline Exception& operator<<(Exception& e, std::ios_base& (*f)(std::ios_base&)) {
0099 f(e.ost_);
0100 return e;
0101 }
0102 inline Exception& operator<<(const Exception& e, std::ios_base& (*f)(std::ios_base&)) {
0103 f(const_cast<Exception&>(e).ost_);
0104 return const_cast<Exception&>(e);
0105 }
0106 }
0107 }
0108
0109 #endif