Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
die.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/die.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2016-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_DIE_HEADER
12 #define TLX_DIE_HEADER
13 
14 #include <cstring>
15 #include <sstream>
16 #include <string>
17 
18 namespace tlx {
19 
20 /******************************************************************************/
21 // die macros
22 
23 //! die with message - either throw an exception or die via abort()
24 void die_with_message(const std::string& msg);
25 
26 //! die with message - either throw an exception or die via abort()
27 void die_with_message(const char* msg, const char* file, size_t line);
28 
29 //! die with message - either throw an exception or die via abort()
30 void die_with_message(const std::string& msg, const char* file, size_t line);
31 
32 //! Instead of abort(), throw the output the message via an exception.
33 #define die_with_sstream(msg) \
34  do { \
35  std::ostringstream oss__; \
36  oss__ << msg << " @ " << __FILE__ << ':' << __LINE__; \
37  ::tlx::die_with_message(oss__.str()); \
38  std::terminate(); \
39  } while (false)
40 
41 //! Instead of abort(), throw the output the message via an exception.
42 #define die(msg) \
43  do { \
44  die_with_sstream("DIE: " << msg); \
45  } while (false)
46 
47 /******************************************************************************/
48 // die_unless() and die_if()
49 
50 //! Check condition X and die miserably if false. Same as assert() except this
51 //! is also active in Release mode.
52 #define die_unless(X) \
53  do { \
54  if (!(X)) { \
55  ::tlx::die_with_message( \
56  "DIE: Assertion \"" #X "\" failed!", __FILE__, __LINE__); \
57  } \
58  } while (false)
59 
60 //! Check condition X and die miserably if true. Opposite of assert() except
61 //! this is also active in Release mode.
62 #define die_if(X) \
63  do { \
64  if (X) { \
65  ::tlx::die_with_message( \
66  "DIE: Assertion \"" #X "\" succeeded!", __FILE__, __LINE__); \
67  } \
68  } while (false)
69 
70 /******************************************************************************/
71 // die_unequal()
72 
73 //! helper method to compare two values in die_unequal()
74 template <typename TypeA, typename TypeB>
75 inline bool die_equal_compare(TypeA a, TypeB b) {
76  return a == b;
77 }
78 
79 template <>
80 inline bool die_equal_compare(const char* a, const char* b) {
81  // compare string contents
82  return std::strcmp(a, b) == 0;
83 }
84 
85 template <>
86 inline bool die_equal_compare(float a, float b) {
87  // special case for NAN
88  return a != a ? b != b : a == b;
89 }
90 
91 template <>
92 inline bool die_equal_compare(double a, double b) {
93  // special case for NAN
94  return a != a ? b != b : a == b;
95 }
96 
97 //! Check that X == Y or die miserably, but output the values of X and Y for
98 //! better debugging.
99 #define die_unequal(X, Y) \
100  do { \
101  auto x__ = (X); /* NOLINT */ \
102  auto y__ = (Y); /* NOLINT */ \
103  if (!::tlx::die_equal_compare(x__, y__)) \
104  die_with_sstream("DIE-UNEQUAL: " #X " != " #Y " : " \
105  "\"" << x__ << "\" != \"" << y__ << "\""); \
106  } while (false)
107 
108 //! Check that X == Y or die miserably, but output the values of X and Y for
109 //! better debugging. Only active if NDEBUG is not defined.
110 #ifdef NDEBUG
111 #define assert_equal(X, Y)
112 #else
113 #define assert_equal(X, Y) die_unequal(X, Y)
114 #endif
115 
116 /******************************************************************************/
117 // die_unless_throws()
118 
119 //! Define to check that [code] throws and exception of given type
120 #define die_unless_throws(code, exception_type) \
121  do { \
122  try { \
123  code; \
124  } \
125  catch (const exception_type&) { \
126  break; \
127  } \
128  ::tlx::die_with_message( \
129  "DIE-UNLESS-THROWS: " #code " - NO EXCEPTION " #exception_type, \
130  __FILE__, __LINE__); \
131  } while (false)
132 
133 } // namespace tlx
134 
135 #endif // !TLX_DIE_HEADER
136 
137 /******************************************************************************/
bool die_equal_compare(TypeA a, TypeB b)
helper method to compare two values in die_unequal()
Definition: die.hpp:75
void die_with_message(const std::string &msg)
die with message - either throw an exception or die via abort()
Definition: die.cpp:18
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220