OpenWalnut  1.3.1
WException.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WEXCEPTION_H
26 #define WEXCEPTION_H
27 
28 #include <exception>
29 #include <list>
30 #include <string>
31 #include <sstream>
32 
33 #include "WTerminalColor.h"
34 
35 
36 /**
37  * Basic exception handler.
38  */
39 class WException: public std::exception
40 {
41 /**
42  * Only UnitTests are allowed to be a friend of this class.
43  */
44 friend class WExceptionTest;
45 
46 public:
47  /**
48  * Default constructor.
49  * \param msg Exception description.
50  */
51  explicit WException( const std::string& msg = std::string() );
52 
53  /**
54  * Copy a std::exception and encapsulate it.
55  *
56  * \param e the exception.
57  */
58  explicit WException( const std::exception& e );
59 
60  /**
61  * Destructor.
62  */
63  virtual ~WException() throw();
64 
65  /**
66  * Returns the message string set on throw
67  * \return Message text
68  */
69  virtual const char* what() const throw();
70 
71  /**
72  * Prints the trace of the call chain which caused this exception.
73  * \return Calltrace as string
74  * \note Isn't this useless? Should be removed.
75  */
76  std::string getTrace( ) const;
77 
78  /**
79  * Returns a call stacktrace.
80  * \return The backtrace at the moment of "throw".
81  */
82  std::string getBacktrace() const;
83 
84  /**
85  * Function disables backtraces. Please note that the backtrace can not be reactivated to avoid people from dis/enabling them
86  * at will.
87  */
88  static void disableBacktrace();
89 
90 protected:
91  /**
92  * Message given during throw.
93  */
94  std::string m_msg;
95 
96  /**
97  * Stack trace for identifying the source where this exception came from.
98  * \note Isn't this useless? Should be removed.
99  */
100  std::list< std::string > m_trace;
101 
102  /**
103  * True if the backtrace should NOT be printed.
104  */
105  static bool noBacktrace;
106 private:
107  /**
108  * Color used for the "trace:" label.
109  */
111 
112  /**
113  * Color used for function name.
114  */
116 
117  /**
118  * Color used for symbols.
119  */
121 
122  /**
123  * Color used for exception headline.
124  */
126 };
127 
128 #endif // WEXCEPTION_H
129