OpenWalnut 1.3.1
WException.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WEXCEPTION_H
00026 #define WEXCEPTION_H
00027 
00028 #include <exception>
00029 #include <list>
00030 #include <string>
00031 #include <sstream>
00032 
00033 #include "WTerminalColor.h"
00034 
00035 
00036 /**
00037  * Basic exception handler.
00038  */
00039 class WException: public std::exception
00040 {
00041 /**
00042  * Only UnitTests are allowed to be a friend of this class.
00043  */
00044 friend class WExceptionTest;
00045 
00046 public:
00047     /**
00048      * Default constructor.
00049      * \param msg Exception description.
00050      */
00051     explicit WException( const std::string& msg = std::string() );
00052 
00053     /**
00054      * Copy a std::exception and encapsulate it.
00055      *
00056      * \param e the exception.
00057      */
00058     explicit WException( const std::exception& e );
00059 
00060     /**
00061      * Destructor.
00062      */
00063     virtual ~WException() throw();
00064 
00065     /**
00066      * Returns the message string set on throw
00067      * \return Message text
00068      */
00069     virtual const char* what() const throw();
00070 
00071     /**
00072      * Prints the trace of the call chain which caused this exception.
00073      * \return Calltrace as string
00074      * \note Isn't this useless? Should be removed.
00075      */
00076     std::string getTrace( ) const;
00077 
00078     /**
00079      * Returns a call stacktrace.
00080      * \return The backtrace at the moment of "throw".
00081      */
00082     std::string getBacktrace() const;
00083 
00084     /**
00085      * Function disables backtraces. Please note that the backtrace can not be reactivated to avoid people from dis/enabling them
00086      * at will.
00087      */
00088     static void disableBacktrace();
00089 
00090 protected:
00091     /**
00092      * Message given during throw.
00093      */
00094     std::string m_msg;
00095 
00096     /**
00097      * Stack trace for identifying the source where this exception came from.
00098      * \note Isn't this useless? Should be removed.
00099      */
00100     std::list< std::string > m_trace;
00101 
00102     /**
00103      * True if the backtrace should NOT be printed.
00104      */
00105     static bool noBacktrace;
00106 private:
00107     /**
00108      * Color used for the "trace:" label.
00109      */
00110     WTerminalColor m_labelColor;
00111 
00112     /**
00113      * Color used for function name.
00114      */
00115     WTerminalColor m_functionColor;
00116 
00117     /**
00118      * Color used for symbols.
00119      */
00120     WTerminalColor m_symbolColor;
00121 
00122     /**
00123      * Color used for exception headline.
00124      */
00125     WTerminalColor m_headlineColor;
00126 };
00127 
00128 #endif  // WEXCEPTION_H
00129