Go to the documentation of this file.00001 #ifndef CommonTools_Utils_Exception_h
00002 #define CommonTools_Utils_Exception_h
00003
00004
00005
00006
00007
00016
00017
00018
00019
00020
00021
00022
00023 #include <sstream>
00024 #include "boost/spirit/include/classic_exceptions.hpp"
00025
00026
00027
00028
00029 namespace reco {
00030 namespace parser {
00031 enum SyntaxErrors {
00032 kSyntaxError,
00033 kMissingClosingParenthesis,
00034 kSpecialError
00035 };
00036
00037 typedef boost::spirit::classic::parser_error<reco::parser::SyntaxErrors> BaseException;
00038
00040 inline const char * baseExceptionWhat(const BaseException& e) {
00041 switch(e.descriptor) {
00042 case kMissingClosingParenthesis:
00043 return "Missing close parenthesis.";
00044 case kSyntaxError:
00045 return "Syntax error.";
00046 case kSpecialError:
00047 default:
00048 break;
00049 }
00050 return e.what();
00051 }
00052 class Exception : public BaseException {
00053
00054 public:
00055 Exception(const char* iIterator) : BaseException(iIterator,kSpecialError) {}
00056 Exception(const Exception& iOther) : BaseException(iOther) {
00057 ost_ << iOther.ost_.str();
00058 }
00059 ~Exception() throw() {}
00060
00061
00062 const char* what() const throw() { what_ = ost_.str(); return what_.c_str();}
00063
00064
00065
00066
00067
00068 template<class T>
00069 friend Exception& operator<<(Exception&, const T&);
00070 template<class T>
00071 friend Exception& operator<<(const Exception&, const T&);
00072 friend Exception& operator<<(Exception&, std::ostream&(*f)(std::ostream&));
00073 friend Exception& operator<<(const Exception&, std::ostream&(*f)(std::ostream&));
00074 friend Exception& operator<<(Exception&, std::ios_base&(*f)(std::ios_base&));
00075 friend Exception& operator<<(const Exception&, std::ios_base&(*f)(std::ios_base&));
00076 private:
00077
00078
00079
00080
00081 std::ostringstream ost_;
00082 mutable std::string what_;
00083 };
00084
00085 template<class T>
00086 inline Exception& operator<<(Exception& e, const T& iT) {
00087 e.ost_ << iT;
00088 return e;
00089 }
00090 template<class T>
00091 inline Exception& operator<<(const Exception& e, const T& iT) {
00092 return operator<<(const_cast<Exception&>(e), iT);
00093 }
00094 inline Exception& operator<<(Exception& e, std::ostream&(*f)(std::ostream&))
00095 {
00096 f(e.ost_);
00097 return e;
00098 }
00099 inline Exception& operator<<(const Exception& e, std::ostream&(*f)(std::ostream&))
00100 {
00101 f(const_cast<Exception&>(e).ost_);
00102 return const_cast<Exception&>(e);
00103 }
00104 inline Exception& operator<<(Exception& e, std::ios_base&(*f)(std::ios_base&)) {
00105 f(e.ost_);
00106 return e;
00107 }
00108 inline Exception& operator<<(const Exception& e, std::ios_base&(*f)(std::ios_base&)) {
00109 f(const_cast<Exception&>(e).ost_);
00110 return const_cast<Exception&>(e);
00111 }
00112 }
00113 }
00114
00115 #endif