Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:13

0001 #ifndef CommonTools_Utils_Exception_h
0002 #define CommonTools_Utils_Exception_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     CommonTools/Utils
0006 // Class  :     Exception
0007 //
0008 /**\class Exception Exception.h CommonTools/Utils/interface/Exception.h
0009 
0010  Description: <one line class summary>
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Thu Aug 14 17:09:42 EDT 2008
0019 // $Id: Exception.h,v 1.2 2009/02/24 14:40:26 llista Exp $
0020 //
0021 
0022 // system include files
0023 #include <sstream>
0024 #include "boost/spirit/include/classic_exceptions.hpp"
0025 
0026 // user include files
0027 
0028 // forward declarations
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     ///returns the appropriate 'what' message for the exception
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       // ---------- const member functions ---------------------
0055       const char* what() const throw() override {
0056         what_ = ost_.str();
0057         return what_.c_str();
0058       }
0059 
0060       // ---------- static member functions --------------------
0061 
0062       // ---------- member functions ---------------------------
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       //const Exception& operator=(const Exception&); // stop default
0075 
0076       // ---------- member data --------------------------------
0077       std::ostringstream ost_;
0078       mutable std::string what_;  //needed since ost_.str() returns a temporary string
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   }  // namespace parser
0107 }  // namespace reco
0108 
0109 #endif