Upstream tarball 10013
[amule.git] / src / libs / common / MuleDebug.h
blob588fb6ee793aac4af51195e5bf00dc19840ef4e4
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2008 Mikkel Schubert ( xaignar@users.sourceforge.net )
5 // Copyright (c) 2005-2008 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.
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.
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
26 #ifndef MULEDEBUG_H
27 #define MULEDEBUG_H
29 #include <wx/string.h>
31 /**
32 * Installs an exception handler that can handle CMuleExceptions.
34 void InstallMuleExceptionHandler();
36 /**
39 void OnUnhandledException();
42 //! Print a backtrace, skipping the first n frames.
43 void print_backtrace(unsigned n);
45 //! Returns a backtrace, skipping the first n frames.
46 wxString get_backtrace(unsigned n);
49 /**
50 * This exception should be used to implement other
51 * types of exceptions. It should never be caught,
52 * instead catch the subtypes.
54 class CMuleException
56 public:
57 CMuleException(const wxString& type, const wxString& desc)
58 : m_what(type + wxT(": ") + desc) {}
59 virtual ~CMuleException() throw() {}
60 virtual const wxString& what() const throw() { return m_what; }
62 private:
63 wxString m_what;
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.
72 struct CRunTimeException : public CMuleException
74 CRunTimeException(const wxString& type, const wxString& desc)
75 : CMuleException(wxT("CRunTimeException::") + type, desc) {}
80 /**
81 * This exception is to be thrown if invalid parameters are passed to a function.
83 struct CInvalidParamsEx : public CRunTimeException
85 CInvalidParamsEx(const wxString& desc)
86 : CRunTimeException(wxT("CInvalidArgsException"), desc) {}
90 /**
91 * This exception is to be thrown if an object is used in an invalid state.
93 struct CInvalidStateEx : public CRunTimeException
95 CInvalidStateEx(const wxString& desc)
96 : CRunTimeException(wxT("CInvalidStateException"), desc) {}
99 /**
100 * This exception is thrown on wrong packets or tags.
102 struct CInvalidPacket : public CMuleException
104 CInvalidPacket(const wxString& desc)
105 : CMuleException(wxT("CInvalidPacket"), desc) {}
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); \
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); \
125 } while (false)
126 #endif
130 #define MULE_CHECK_THROW(cond, cls, msg) \
131 _MULE_THROW((cond), cls, (msg))
133 #define MULE_VALIDATE_STATE(cond, msg) \
134 MULE_CHECK_THROW((cond), CInvalidStateEx, (msg))
136 #define MULE_VALIDATE_PARAMS(cond, msg) \
137 MULE_CHECK_THROW((cond), CInvalidParamsEx, (msg))
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))
148 #endif
149 // File_checked_for_headers