Skip to content

Commit 5a6874b

Browse files
committed
Added exception macros.
1 parent 803f214 commit 5a6874b

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

src/Macros.hpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
/**
8585
* @brief Abort execution if @p EXP is true.
8686
* @param EXP The expression to check.
87-
* @param MSG The message to associate with the error, can be anything streamable to std::cout.
87+
* @param MSG The message to associate with the error, can be anything streamable to a std::ostream.
8888
* @note This macro can be used in both host and device code.
8989
* @note Tries to provide as much information about the location of the error
9090
* as possible. On host this should result in the file and line of the error
@@ -117,15 +117,15 @@
117117

118118
/**
119119
* @brief Abort execution.
120-
* @param MSG The message to associate with the error, can be anything streamable to std::cout.
120+
* @param MSG The message to associate with the error, can be anything streamable to a std::ostream.
121121
*/
122122
#define LVARRAY_ERROR( MSG ) LVARRAY_ERROR_IF( true, MSG )
123123

124124
/**
125125
* @brief Abort execution if @p EXP is false but only when
126126
* NDEBUG is not defined..
127127
* @param EXP The expression to check.
128-
* @param MSG The message to associate with the error, can be anything streamable to std::cout.
128+
* @param MSG The message to associate with the error, can be anything streamable to a std::ostream.
129129
* @note This macro can be used in both host and device code.
130130
* @note Tries to provide as much information about the location of the error
131131
* as possible. On host this should result in the file and line of the error
@@ -138,13 +138,28 @@
138138
#define LVARRAY_ASSERT_MSG( EXP, MSG ) ((void) 0)
139139
#endif
140140

141+
#define LVARRAY_THROW_IF( EXP, MSG, TYPE ) \
142+
do \
143+
{ \
144+
if( EXP ) \
145+
{ \
146+
std::ostringstream __oss; \
147+
__oss << "\n"; \
148+
__oss << "***** LOCATION: " LOCATION "\n"; \
149+
__oss << "***** Controlling expression (should be false): " STRINGIZE( EXP ) "\n"; \
150+
__oss << MSG << "\n"; \
151+
__oss << LvArray::system::stackTrace( true ); \
152+
throw TYPE( __oss.str() ); \
153+
} \
154+
} while( false )
155+
141156
/// Assert @p EXP is true with no message.
142157
#define LVARRAY_ASSERT( EXP ) LVARRAY_ASSERT_MSG( EXP, "" )
143158

144159
/**
145160
* @brief Print a warning if @p EXP is true.
146161
* @param EXP The expression to check.
147-
* @param MSG The message to associate with the warning, can be anything streamable to std::cout.
162+
* @param MSG The message to associate with the warning, can be anything streamable to a std::ostream.
148163
*/
149164
#define LVARRAY_WARNING_IF( EXP, MSG ) \
150165
do \
@@ -206,6 +221,13 @@
206221
" " << #lhs << " = " << lhs << "\n" << \
207222
" " << #rhs << " = " << rhs << "\n" )
208223

224+
#define LVARRAY_THROW_IF_OP_MSG( lhs, OP, NOP, rhs, msg, TYPE ) \
225+
LVARRAY_THROW_IF( lhs OP rhs, \
226+
msg << "\n" << \
227+
"Expected " << #lhs << " " << #NOP << " " << #rhs << "\n" << \
228+
" " << #lhs << " = " << lhs << "\n" << \
229+
" " << #rhs << " = " << rhs << "\n", TYPE )
230+
209231
/**
210232
* @brief Raise a hard error if two values are equal.
211233
* @param lhs expression to be evaluated and used as left-hand side in comparison
@@ -214,13 +236,17 @@
214236
*/
215237
#define LVARRAY_ERROR_IF_EQ_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, ==, !=, rhs, msg )
216238

239+
#define LVARRAY_THROW_IF_EQ_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, ==, !=, rhs, msg, TYPE )
240+
217241
/**
218242
* @brief Raise a hard error if two values are equal.
219243
* @param lhs expression to be evaluated and used as left-hand side in comparison
220244
* @param rhs expression to be evaluated and used as right-hand side in comparison
221245
*/
222246
#define LVARRAY_ERROR_IF_EQ( lhs, rhs ) LVARRAY_ERROR_IF_EQ_MSG( lhs, rhs, "" )
223247

248+
#define LVARRAY_THROW_IF_EQ( lhs, rhs, TYPE ) LVARRAY_THROW_IF_EQ_MSG( lhs, rhs, "", TYPE )
249+
224250
/**
225251
* @brief Raise a hard error if two values are not equal.
226252
* @param lhs expression to be evaluated and used as left-hand side in comparison
@@ -229,13 +255,17 @@
229255
*/
230256
#define LVARRAY_ERROR_IF_NE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, !=, ==, rhs, msg )
231257

258+
#define LVARRAY_THROW_IF_NE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, !=, ==, rhs, msg, TYPE )
259+
232260
/**
233261
* @brief Raise a hard error if two values are not equal.
234262
* @param lhs expression to be evaluated and used as left-hand side in comparison
235263
* @param rhs expression to be evaluated and used as right-hand side in comparison
236264
*/
237265
#define LVARRAY_ERROR_IF_NE( lhs, rhs ) LVARRAY_ERROR_IF_NE_MSG( lhs, rhs, "" )
238266

267+
#define LVARRAY_THROW_IF_NE( lhs, rhs, TYPE ) LVARRAY_THROW_IF_NE_MSG( lhs, rhs, "", TYPE )
268+
239269
/**
240270
* @brief Raise a hard error if one value compares greater than the other.
241271
* @param lhs expression to be evaluated and used as left-hand side in comparison
@@ -244,13 +274,17 @@
244274
*/
245275
#define LVARRAY_ERROR_IF_GT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, >, <=, rhs, msg )
246276

277+
#define LVARRAY_THROW_IF_GT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, >, <=, rhs, msg, TYPE )
278+
247279
/**
248280
* @brief Raise a hard error if one value compares greater than the other.
249281
* @param lhs expression to be evaluated and used as left-hand side in comparison
250282
* @param rhs expression to be evaluated and used as right-hand side in comparison
251283
*/
252284
#define LVARRAY_ERROR_IF_GT( lhs, rhs ) LVARRAY_ERROR_IF_GT_MSG( lhs, rhs, "" )
253285

286+
#define LVARRAY_THROW_IF_GT( lhs, rhs, TYPE ) LVARRAY_THROW_IF_GT_MSG( lhs, rhs, "", TYPE )
287+
254288
/**
255289
* @brief Raise a hard error if one value compares greater than or equal to the other.
256290
* @param lhs expression to be evaluated and used as left-hand side in comparison
@@ -259,13 +293,18 @@
259293
*/
260294
#define LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, >=, <, rhs, msg )
261295

296+
#define LVARRAY_THROW_IF_GE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, >=, <, rhs, msg, TYPE )
297+
262298
/**
263299
* @brief Raise a hard error if one value compares greater than or equal to the other.
264300
* @param lhs expression to be evaluated and used as left-hand side in comparison
265301
* @param rhs expression to be evaluated and used as right-hand side in comparison
266302
*/
267303
#define LVARRAY_ERROR_IF_GE( lhs, rhs ) LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, "" )
268304

305+
#define LVARRAY_THROW_IF_GE( lhs, rhs, TYPE ) LVARRAY_THROW_IF_GE_MSG( lhs, rhs, "", TYPE )
306+
307+
269308
/**
270309
* @brief Raise a hard error if one value compares less than the other.
271310
* @param lhs expression to be evaluated and used as left-hand side in comparison
@@ -274,13 +313,17 @@
274313
*/
275314
#define LVARRAY_ERROR_IF_LT_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, <, >=, rhs, msg )
276315

316+
#define LVARRAY_THROW_IF_LT_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, <, >=, rhs, msg, TYPE )
317+
277318
/**
278319
* @brief Raise a hard error if one value compares less than the other.
279320
* @param lhs expression to be evaluated and used as left-hand side in comparison
280321
* @param rhs expression to be evaluated and used as right-hand side in comparison
281322
*/
282323
#define LVARRAY_ERROR_IF_LT( lhs, rhs ) LVARRAY_ERROR_IF_LT_MSG( lhs, rhs, "" )
283324

325+
#define LVARRAY_THROW_IF_LT( lhs, rhs, TYPE ) LVARRAY_THROW_IF_LT_MSG( lhs, rhs, "", TYPE )
326+
284327
/**
285328
* @brief Raise a hard error if one value compares less than or equal to the other.
286329
* @param lhs expression to be evaluated and used as left-hand side in comparison
@@ -289,13 +332,17 @@
289332
*/
290333
#define LVARRAY_ERROR_IF_LE_MSG( lhs, rhs, msg ) LVARRAY_ERROR_IF_OP_MSG( lhs, <=, >, rhs, msg )
291334

335+
#define LVARRAY_THROW_IF_LE_MSG( lhs, rhs, msg, TYPE ) LVARRAY_THROW_IF_OP_MSG( lhs, <=, >, rhs, msg, TYPE )
336+
292337
/**
293338
* @brief Raise a hard error if one value compares less than or equal to the other.
294339
* @param lhs expression to be evaluated and used as left-hand side in comparison
295340
* @param rhs expression to be evaluated and used as right-hand side in comparison
296341
*/
297342
#define LVARRAY_ERROR_IF_LE( lhs, rhs ) LVARRAY_ERROR_IF_GE_MSG( lhs, rhs, "" )
298343

344+
#define LVARRAY_THROW_IF_LE( lhs, rhs, TYPE ) LVARRAY_THROW_IF_GE_MSG( lhs, rhs, "", TYPE )
345+
299346
/**
300347
* @brief Abort execution if @p lhs @p OP @p rhs is false.
301348
* @param lhs The left side of the operation.

src/system.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,17 @@ static std::string getFunctionNameFromFrame( void const * const address )
111111
{
112112
Dl_info dli;
113113
const bool dl_ok = dladdr( address, &dli );
114-
if( dl_ok && dli.dli_sname )
114+
if( dl_ok )
115115
{
116-
return LvArray::system::demangle( dli.dli_sname );
116+
if( dli.dli_sname )
117+
{
118+
return LvArray::system::demangle( dli.dli_sname );
119+
}
120+
121+
return dli.dli_fname;
117122
}
118123

119-
return "";
124+
return "Unknown";
120125
}
121126

122127
#if defined( LVARRAY_ADDR2LINE_EXEC )

src/system.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string>
1717
#include <typeinfo>
1818
#include <functional>
19+
#include <dlfcn.h>
1920

2021
namespace LvArray
2122
{
@@ -52,11 +53,12 @@ inline std::string demangleType()
5253

5354
/**
5455
* @return A demangled type name corresponding to the type @tparam T.
55-
* @tparam T The type to demangle.
56+
* @tparam T The type of the object to demangle.
57+
* @param var The variable to demangle the type of.
5658
*/
5759
template< class T >
58-
inline std::string demangleType( T const & )
59-
{ return demangle( typeid( T ).name() ); }
60+
inline std::string demangleType( T const & var )
61+
{ return demangle( typeid( var ).name() ); }
6062

6163
/**
6264
* @brief Set the error handler called by LVARRAY_ERROR and others.

0 commit comments

Comments
 (0)