]> git.lyx.org Git - lyx.git/blob - src/support/lassert.h
Merge branch 'master' of git.lyx.org:lyx
[lyx.git] / src / support / lassert.h
1
2 /**
3  * \file support/lassert.h
4  *
5  * This file is part of LyX, the document processor.
6  * Licence details can be found in the file COPYING.
7  *
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef LASSERT_H
14 #define LASSERT_H
15
16 #ifdef __cplusplus
17
18 #include "support/strfwd.h"
19
20 namespace lyx {
21
22 /******************************************************************************
23
24 LyX has five different macros that can be used to make assertions. They behave
25 the same way in devel mode: They assert. The differences between them are how
26 they behave in release mode.
27
28 In order of increasing seriousness, they are:
29
30 LATTEST(expr)
31   This macro should be used when one just wants to test expr. If devel mode,
32   this will lead to an assertion. In release mode, we will simply continue. So
33   LATTEST should be used only if you know, in advance, that it will be safe to
34   continue with the usual program flow, but failure of expr still means that 
35   there is something that needs to be fixed.
36
37 LASSERT(expr, escape)
38   This macro should be used when a failure of expr is not compatible with 
39   continuing the ordinary program flow, but is something from which we can
40   recover. This might mean simply returning early from some routine; it might
41   mean resetting some variables to values known to be sane; it might mean
42   taking some other corrective action. 
43
44 LWARNIF(expr)
45   This macro should be used when a failure of expr indicates that the current
46         operation cannot safely be completed. In release mode, it will abort that
47         operation and print a warning message to the user.
48
49 LBUFERR(expr)
50   This macro should be used when a failure of expr indicates a problem with a
51         Buffer or its related objects, e.g., a Cursor. In release mode, it throws a
52         BufferException, which will typically result in an emergency save of that
53         particular Buffer.
54
55 LAPPERR(expr)
56   This macro should be used if a failure of expr is incompatible with LyX
57         continuing to operate at all. In release mode, this issues an ErrorException,
58   which typically results in an emergency shutdown.
59         
60 ******************************************************************************/
61
62
63 void doAssert(char const * expr, char const * file, long line);
64 void doWarnIf(char const * expr, char const * file, long line);
65 void doBufErr(char const * expr, char const * file, long line);
66 void doAppErr(char const * expr, char const * file, long line);
67
68 void doAssertWithCallstack(bool value);
69
70 /// Print demangled callstack to stderr
71 docstring printCallStack();
72
73
74 } // namespace lyx
75
76 #define LATTEST(expr) \
77         if (expr) {} else { lyx::doAssert(#expr, __FILE__, __LINE__); }
78
79 #define LASSERT(expr, escape) \
80         if (expr) {} else { lyx::doAssert(#expr, __FILE__, __LINE__); escape; }
81
82 #define LWARNIF(expr) \
83         if (expr) {} else { lyx::doWarnIf(#expr, __FILE__, __LINE__); }
84
85 #define LBUFERR(expr) \
86         if (expr) {} else { lyx::doBufErr(#expr, __FILE__, __LINE__); }
87
88 #define LAPPERR(expr) \
89         if (expr) {} else { lyx::doAppErr(#expr, __FILE__, __LINE__); }
90
91 #endif
92 #endif // LASSERT