forked from CaravelGames/drod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssert.h
More file actions
192 lines (168 loc) · 5.92 KB
/
Copy pathAssert.h
File metadata and controls
192 lines (168 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// $Id$
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Deadly Rooms of Death.
*
* The Initial Developer of the Original Code is
* Caravel Software.
* Portions created by the Initial Developer are Copyright (C) 1995, 1996,
* 1997, 2000, 2001, 2002, 2005 Caravel Software. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
//Assert.h
//Assertion checking and debugging info.
#ifndef ASSERT_H
#define ASSERT_H
#ifdef WIN32
# pragma warning(disable:4786)
#endif
#include <string>
#include <cstring>
using std::string;
#ifndef DONT_RELEASE_WITH_DEBUG_INFO
# define RELEASE_WITH_DEBUG_INFO //Uncomment for assert messages in release builds.
#endif
#undef USE_LOGCONTEXT
#ifdef RELEASE_WITH_DEBUG_INFO
# ifndef _DEBUG
# define _DEBUG
# endif
#endif
#ifdef ENABLE_BREAKPOINTS // don't enable this for release builds!
# ifdef _MSC_VER
# include <intrin.h>
# define MY_BREAKPOINT() __debugbreak()
# else
# include <signal.h>
# define MY_BREAKPOINT() raise(SIGTRAP)
// (gcc has a breakpoint intrinsic, but we don't use it because it's treated as
// never-returns by the optimizer for some bizarre reason)
# endif
#else
# define MY_BREAKPOINT() (void)0
#endif
//Assertion-reporting macros. Avoid simply calling ASSERT(false) because after changes to source code,
//the file and line# may not be sufficient to track the location in current source of the assertion. Use
//either ASSERT(expression) or ASSERT(!"Description of error.").
#if defined(CARAVELBUILD)
# if defined(WIN32)
# define __FILENAME__ (strrchr("\\" __FILE__, '\\') + 1) //strip path
# else
# define __FILENAME__ (strrchr("/" __FILE__, '/') + 1)
# endif
#else
# define __FILENAME__ (__FILE__)
#endif
#define _ASSERT_VOID_CAST(exp) static_cast<void>(exp)
#ifdef _DEBUG
# if defined(WIN32) && !defined(__GNUC__)
# define ASSERT(exp) _ASSERT_VOID_CAST( (exp) ? void() : AssertErr(__FILENAME__,__LINE__,#exp) )
# define ASSERTP(exp, desc) _ASSERT_VOID_CAST( (exp) ? void() : AssertErr(__FILENAME__,__LINE__,(desc)) )
# define ASSERT_DETAIL(exp, desc, info) _ASSERT_VOID_CAST( (exp) ? void() : AssertDetailedErr(__FILENAME__,__LINE__,(desc), (info)) )
# else
# define ASSERT(exp) do { if (!(exp)) { AssertErr(__FILENAME__,__LINE__,#exp); MY_BREAKPOINT(); }} while (0)
# define ASSERTP(exp, desc) do { if (!(exp)) { AssertErr(__FILENAME__,__LINE__,(desc)); MY_BREAKPOINT(); }} while (0)
# define ASSERT_DETAIL(exp, desc, info) do { if (!(exp)) { AssertDetailedErr(__FILENAME__,__LINE__,(desc), (info)); MY_BREAKPOINT(); }} while (0)
# endif
#else
# define ASSERT(exp)
# define ASSERTP(exp,desc)
# define ASSERT_DETAIL(exp, desc, info)
#endif
//Log context is applied to error logging to show a history of what happened before an error.
//When CLogText() is is instanced, error logging will show "BEGIN The task", and when it goes out
//of scope, error logging will show "END The task". These messages are only shown when an error
//occurs.
#if defined(USE_LOGCONTEXT) && defined(_DEBUG)
# define LOGCONTEXT(desc) CLogContext _LogContext((desc))
class CLogContext
{
public:
string strDesc;
CLogContext(const char *pszDesc);
~CLogContext();
};
#else
# define LOGCONTEXT(desc)
#endif
#ifdef _DEBUG
# define DYN_CAST(totype,fromtype,source) \
DynCastAssert<totype,fromtype>(source, __FILENAME__, __LINE__)
#else
# define DYN_CAST(totype,fromtype,source) \
dynamic_cast<totype>(source)
#endif
//Use to assert on a statement that should execute in retail as well as debug builds.
#ifdef _DEBUG
# define VERIFY(exp) \
do { if (!(exp)) { AssertErr(__FILENAME__, __LINE__,#exp); MY_BREAKPOINT(); }} while (0)
#else
# define VERIFY(exp) exp
#endif
//Log an error to the error log.
#ifdef _DEBUG
# define LOGERR(x) LogErr(x)
#else
# define LOGERR(x)
#endif
//Show a message in debug output.
#ifdef _DEBUG
# define DEBUGPRINT(x) DebugPrint(x)
#else
# define DEBUGPRINT(x)
#endif
#ifdef _DEBUG
# define SETLOGERRORS(x) SetLogErrors(x)
#else
# define SETLOGERRORS(x)
#endif
//Prototypes.
//Debug-mode.
#ifdef _DEBUG
void AssertErr(const char *pszFile, int nLine, const char *pszDesc);
void AssertDetailedErr(const char *pszFile, int nLine, const char *pszDesc, const char* pszInfo);
void AssertDetailedErr(const char *pszFile, int nLine, const char *pszDesc, const unsigned int wInfo);
void LogErr(const char *pszMessage);
void DebugPrint(const char *pszMessage);
template<typename To_p, typename From_p>
inline To_p DynCastAssert(From_p pSource, const char *pszFile, int nLine)
{
To_p pTarget = dynamic_cast<To_p>(pSource);
if (pSource && !pTarget) AssertErr(pszFile, nLine, "Dynamic cast failed.");
return pTarget;
}
#endif
void SetLogErrors(const bool bVal);
//Prevent accidental assigns and copies by declaring private methods in a class for
//copy constructor and assign operator.
#define PREVENT_DEFAULT_COPY(ClassName) \
private: \
ClassName (const ClassName &Src); \
ClassName &operator= (const ClassName &Src)
//Limited unique-like pointer (we can't rely on C++11 support yet)
template <typename T, void(*deleter)(T*)>
struct UniquePtr
{
UniquePtr (T* ptr) : ptr(ptr) {}
~UniquePtr () { if (ptr) deleter(ptr); }
void reset (T* newptr) { if (ptr) deleter(ptr); ptr = newptr; }
T* release () { T* result = ptr; ptr = NULL; return result; }
T* get () const { return ptr; }
private:
T* ptr;
PREVENT_DEFAULT_COPY(UniquePtr);
};
#endif //...#ifndef ASSERT_H