Lunchbox  1.8.0
debug.h
1 
2 /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2013, Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef LUNCHBOX_DEBUG_H
20 #define LUNCHBOX_DEBUG_H
21 
22 #include <lunchbox/defines.h>
23 #include <lunchbox/log.h>
24 
25 #include <typeinfo>
26 
27 // assertions
28 // #define LB_RELEASE_ASSERT
29 
30 namespace lunchbox
31 {
36 LUNCHBOX_API void abort();
37 
42 LUNCHBOX_API void checkHeap();
43 
50 LUNCHBOX_API std::ostream& sysError( std::ostream& os );
51 
58 LUNCHBOX_API std::ostream& backtrace( std::ostream& os );
59 
60 LUNCHBOX_API std::string demangleTypeID( const char* mangled );
61 
62 #ifdef _WIN32
63 # pragma warning( disable: 4100 ) // VS Bug
64 #endif
65 
66 template< class T > inline std::string className( const T* object )
67  { return demangleTypeID( typeid( *object ).name( )); }
68 
70 template< class T > inline std::string className( const T& object )
71  { return demangleTypeID( typeid( object ).name( )); }
72 }
73 #ifdef _WIN32
74 # pragma warning( default: 4100 )
75 #endif
76 
77 #ifdef NDEBUG
78 # ifdef LB_RELEASE_ASSERT
79 # define LBASSERT(x) \
80  { \
81  if( !(x) ) \
82  LBERROR << "##### Assert: " << #x << " #####" << std::endl \
83  << lunchbox::forceFlush; \
84  lunchbox::checkHeap(); \
85  }
86 # define LBASSERTINFO(x, info) \
87  { \
88  if( !(x) ) \
89  LBERROR << "##### Assert: " << #x << " [" << info << "] #####" \
90  << std::endl << lunchbox::forceFlush; \
91  lunchbox::checkHeap(); \
92  }
93 # define LBCHECK(x) { const bool eqOk = x; LBASSERTINFO( eqOk, #x ) }
94 # else
95 # define LBASSERT(x)
96 # define LBASSERTINFO(x, info)
97 # define LBCHECK(x) { x; }
98 # endif
99 
100 # define LBUNIMPLEMENTED { LBERROR << "Unimplemented code" << std::endl \
101  << lunchbox::forceFlush; }
102 # define LBUNREACHABLE { LBERROR << "Unreachable code" << std::endl \
103  << lunchbox::forceFlush; }
104 # define LBDONTCALL \
105  { LBERROR << "Code is not supposed to be called in this context" \
106  << std::endl << lunchbox::forceFlush; }
107 # define LBABORT( info ) { \
108  LBERROR << "##### Abort: " << info << " #####" << std::endl \
109  << lunchbox::forceFlush; }
110 
111 #else // NDEBUG
112 
113 # define LBASSERT(x) \
114  { \
115  if( !(x) ) \
116  { \
117  LBERROR << "Assert: " << #x << " "; \
118  lunchbox::abort(); \
119  } \
120  lunchbox::checkHeap(); \
121  }
122 # define LBASSERTINFO(x, info) \
123  { \
124  if( !(x) ) \
125  { \
126  LBERROR << "Assert: " << #x << " [" << info << "] "; \
127  lunchbox::abort(); \
128  } \
129  lunchbox::checkHeap(); \
130  }
131 
132 # define LBUNIMPLEMENTED \
133  { LBERROR << "Unimplemented code in " << lunchbox::className( this ) \
134  << " "; \
135  lunchbox::abort(); }
136 # define LBUNREACHABLE \
137  { LBERROR << "Unreachable code in " << lunchbox::className( this ) \
138  << " "; \
139  lunchbox::abort(); }
140 # define LBDONTCALL \
141  { LBERROR << "Code is not supposed to be called in this context, type " \
142  << lunchbox::className( this ) << " " ; \
143  lunchbox::abort(); }
144 
145 # define LBCHECK(x) { const bool eqOk = x; LBASSERTINFO( eqOk, #x ) }
146 # define LBABORT( info ) { \
147  LBERROR << "Abort: " << info; \
148  lunchbox::abort(); }
149 #endif // NDEBUG
150 
151 #define LBSAFECAST( to, in ) static_cast< to >( in ); \
152  LBASSERT( in == 0 || dynamic_cast< to >( static_cast< to >( in )))
153 
154 #endif //LUNCHBOX_DEBUG_H