lsst.pex.exceptions  8.0.0.0-23-g68b96ed
include/lsst/pex/exceptions/Exception.h
Go to the documentation of this file.
00001 // -*- LSST-C++ -*-
00002 /*
00003  * LSST Data Management System
00004  * Copyright 2008-2014 LSST Corporation.
00005  *
00006  * This product includes software developed by the
00007  * LSST Project (http://www.lsst.org/).
00008  *
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the LSST License Statement and
00020  * the GNU General Public License along with this program.  If not,
00021  * see <http://www.lsstcorp.org/LegalNotices/>.
00022  */
00023 
00024 #ifndef LSST_PEX_EXCEPTIONS_EXCEPTION_H
00025 #define LSST_PEX_EXCEPTIONS_EXCEPTION_H
00026 
00027 #include <exception>
00028 #include <ostream>
00029 #include <string>
00030 #include <vector>
00031 
00032 #include "boost/current_function.hpp"
00033 
00034 namespace lsst {
00035 namespace pex {
00036 namespace exceptions {
00037 
00040 #define LSST_EXCEPT_HERE __FILE__, __LINE__, BOOST_CURRENT_FUNCTION
00041 
00046 #define LSST_EXCEPT(type, ...) type(LSST_EXCEPT_HERE, __VA_ARGS__)
00047 
00051 #define LSST_EXCEPT_ADD(e, m) e.addMessage(LSST_EXCEPT_HERE, m)
00052 
00055 #define LSST_EARGS_TYPED \
00056     char const* ex_file, int ex_line, char const* ex_func, \
00057     std::string const& ex_message
00058 
00061 #define LSST_EARGS_UNTYPED ex_file, ex_line, ex_func, ex_message
00062 
00068 #define LSST_EXCEPTION_TYPE(t, b, c) \
00069     class t : public b { \
00070     public: \
00071         t(LSST_EARGS_TYPED) : b(LSST_EARGS_UNTYPED) { }; \
00072         t(std::string const & message) : b(message) { }; \
00073         virtual char const* getType(void) const throw() { return #c " *"; }; \
00074         virtual lsst::pex::exceptions::Exception* clone(void) const { \
00075             return new t(*this); \
00076         }; \
00077     };
00078 
00080 struct Tracepoint {
00081 
00083     Tracepoint(void);
00084 
00092     Tracepoint(char const* file, int line, char const* func,
00093                std::string const & message);
00094 
00095     char const* _file; // Compiled strings only; does not need deletion
00096     int _line;
00097     char const* _func; // Compiled strings only; does not need deletion
00098     std::string _message;
00099 };
00100 typedef std::vector<Tracepoint> Traceback;
00101 
00102 class Exception : public std::exception {
00103 public:
00104 
00112     Exception(char const* file, int line, char const* func,
00113               std::string const& message); // Should use LSST_EARGS_TYPED, but that confuses doxygen.
00114 
00124     explicit Exception(std::string const & message);
00125 
00126     virtual ~Exception(void) throw();
00127 
00135     void addMessage(char const* file, int line, char const* func,
00136                     std::string const& message);
00137 
00139     Traceback const& getTraceback(void) const throw();
00140 
00147     virtual std::ostream& addToStream(std::ostream& stream) const;
00148 
00159     virtual char const* what(void) const throw();
00160 
00167     virtual char const* getType(void) const throw();
00168 
00174     virtual Exception* clone(void) const;
00175 
00176 private:
00177     std::string _message;
00178     Traceback _traceback;
00179 };
00180 
00186 std::ostream& operator<<(std::ostream& stream, Exception const& e);
00187 
00188 }}} // namespace lsst::pex::exceptions
00189 
00190 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines