1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2011 Mikkel Schubert ( xaignar@users.sourceforge.net )
5 // Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
10 //
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
24 //
25 
26 #ifndef MULEDEBUG_H
27 #define MULEDEBUG_H
28 
29 #include <wx/string.h>
30 
31 /**
32  * Installs an exception handler that can handle CMuleExceptions.
33  */
34 void InstallMuleExceptionHandler();
35 
36 /**
37  *
38  */
39 void OnUnhandledException();
40 
41 
42 //! Print a backtrace, skipping the first n frames.
43 void print_backtrace(unsigned n);
44 
45 //! Returns a backtrace, skipping the first n frames.
46 wxString get_backtrace(unsigned n);
47 
48 
49 /**
50  * This exception should be used to implement other
51  * types of exceptions. It should never be caught,
52  * instead catch the subtypes.
53  */
54 class CMuleException
55 {
56 public:
CMuleException(const wxString & type,const wxString & desc)57 	CMuleException(const wxString& type, const wxString& desc)
58 		: m_what(type + wxT(": ") + desc) {}
~CMuleException()59 	virtual ~CMuleException() throw() {}
what()60 	virtual const wxString& what() const throw() { return m_what; }
61 
62 private:
63 	wxString m_what;
64 };
65 
66 
67 /**
68  * This exception type is used to represent exceptions that are
69  * caused by invalid operations. Exceptions of this type should
70  * not be caught as they are the result of bugs.
71  */
72 struct CRunTimeException : public CMuleException
73 {
CRunTimeExceptionCRunTimeException74 	CRunTimeException(const wxString& type, const wxString& desc)
75 		: CMuleException(wxT("CRunTimeException::") + type, desc) {}
76 };
77 
78 
79 
80 /**
81  * This exception is to be thrown if invalid parameters are passed to a function.
82  */
83 struct CInvalidParamsEx : public CRunTimeException
84 {
CInvalidParamsExCInvalidParamsEx85 	CInvalidParamsEx(const wxString& desc)
86 		: CRunTimeException(wxT("CInvalidArgsException"), desc) {}
87 };
88 
89 
90 /**
91  * This exception is to be thrown if an object is used in an invalid state.
92  */
93 struct CInvalidStateEx : public CRunTimeException
94 {
CInvalidStateExCInvalidStateEx95 	CInvalidStateEx(const wxString& desc)
96 		: CRunTimeException(wxT("CInvalidStateException"), desc) {}
97 };
98 
99 /**
100  * This exception is thrown on wrong packets or tags.
101  */
102 struct CInvalidPacket : public CMuleException
103 {
CInvalidPacketCInvalidPacket104 	CInvalidPacket(const wxString& desc)
105 		: CMuleException(wxT("CInvalidPacket"), desc) {}
106 };
107 
108 
109 // This ifdef ensures that we wont get assertions while
110 // unittesting, which would otherwise impede the tests.
111 #ifdef MULEUNIT
112 	#define _MULE_THROW(cond, cls, msg) \
113 		do { \
114 			if (!(cond)) { \
115 				throw cls(msg); \
116 			} \
117 		} while (false)
118 #else
119 	#define _MULE_THROW(cond, cls, msg) \
120 		do { \
121 			if (!(cond)) { \
122 				wxFAIL_MSG(wxT(#cond)); \
123 				throw cls(msg); \
124 			} \
125 		} while (false)
126 #endif
127 
128 
129 
130 #define MULE_CHECK_THROW(cond, cls, msg) \
131 	_MULE_THROW((cond), cls, (msg))
132 
133 #define MULE_VALIDATE_STATE(cond, msg) \
134 	MULE_CHECK_THROW((cond), CInvalidStateEx, (msg))
135 
136 #define MULE_VALIDATE_PARAMS(cond, msg) \
137 	MULE_CHECK_THROW((cond), CInvalidParamsEx, (msg))
138 
139 
140 #define MULE_ASSERT(cond)               wxASSERT((cond))
141 #define MULE_ASSERT_MSG(cond, msg)      wxASSERT_MSG((cond), msg)
142 #define MULE_FAIL()                     wxFAIL()
143 #define MULE_FAIL_MSG(msg)              wxFAIL_MSG(msg)
144 #define MULE_CHECK(cond, retValue)      wxCHECK((cond), (retValue))
145 #define MULE_CHECK_MSG(cond, ret, msg)  wxCHECK_MSG((cond), (ret), (msg))
146 #define MULE_CHECK_RET(cond, msg)       wxCHECK_RET((cond), (msg))
147 
148 #endif
149 // File_checked_for_headers
150