Exception

SyntaxErrors

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#ifndef CommonTools_Utils_Exception_h
#define CommonTools_Utils_Exception_h
// -*- C++ -*-
//
// Package:     CommonTools/Utils
// Class  :     Exception
//
/**\class Exception Exception.h CommonTools/Utils/interface/Exception.h

 Description: <one line class summary>

 Usage:
    <usage>

*/
//
// Original Author:  Chris Jones
//         Created:  Thu Aug 14 17:09:42 EDT 2008
// $Id: Exception.h,v 1.2 2009/02/24 14:40:26 llista Exp $
//

// system include files
#include <sstream>
#include "boost/spirit/include/classic_exceptions.hpp"

// user include files

// forward declarations
namespace reco {
  namespace parser {
    enum SyntaxErrors { kSyntaxError, kMissingClosingParenthesis, kSpecialError };

    typedef boost::spirit::classic::parser_error<reco::parser::SyntaxErrors> BaseException;

    ///returns the appropriate 'what' message for the exception
    inline const char* baseExceptionWhat(const BaseException& e) {
      switch (e.descriptor) {
        case kMissingClosingParenthesis:
          return "Missing close parenthesis.";
        case kSyntaxError:
          return "Syntax error.";
        case kSpecialError:
        default:
          break;
      }
      return e.what();
    }
    class Exception : public BaseException {
    public:
      Exception(const char* iIterator) : BaseException(iIterator, kSpecialError) {}
      Exception(const Exception& iOther) : BaseException(iOther) { ost_ << iOther.ost_.str(); }
      ~Exception() throw() override {}

      // ---------- const member functions ---------------------
      const char* what() const throw() override {
        what_ = ost_.str();
        return what_.c_str();
      }

      // ---------- static member functions --------------------

      // ---------- member functions ---------------------------

      template <class T>
      friend Exception& operator<<(Exception&, const T&);
      template <class T>
      friend Exception& operator<<(const Exception&, const T&);
      friend Exception& operator<<(Exception&, std::ostream& (*f)(std::ostream&));
      friend Exception& operator<<(const Exception&, std::ostream& (*f)(std::ostream&));
      friend Exception& operator<<(Exception&, std::ios_base& (*f)(std::ios_base&));
      friend Exception& operator<<(const Exception&, std::ios_base& (*f)(std::ios_base&));

    private:
      //const Exception& operator=(const Exception&); // stop default

      // ---------- member data --------------------------------
      std::ostringstream ost_;
      mutable std::string what_;  //needed since ost_.str() returns a temporary string
    };

    template <class T>
    inline Exception& operator<<(Exception& e, const T& iT) {
      e.ost_ << iT;
      return e;
    }
    template <class T>
    inline Exception& operator<<(const Exception& e, const T& iT) {
      return operator<<(const_cast<Exception&>(e), iT);
    }
    inline Exception& operator<<(Exception& e, std::ostream& (*f)(std::ostream&)) {
      f(e.ost_);
      return e;
    }
    inline Exception& operator<<(const Exception& e, std::ostream& (*f)(std::ostream&)) {
      f(const_cast<Exception&>(e).ost_);
      return const_cast<Exception&>(e);
    }
    inline Exception& operator<<(Exception& e, std::ios_base& (*f)(std::ios_base&)) {
      f(e.ost_);
      return e;
    }
    inline Exception& operator<<(const Exception& e, std::ios_base& (*f)(std::ios_base&)) {
      f(const_cast<Exception&>(e).ost_);
      return const_cast<Exception&>(e);
    }
  }  // namespace parser
}  // namespace reco

#endif