Thrill  0.1
core.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/die/core.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2016-2018 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #include <tlx/die/core.hpp>
12 
13 #include <atomic>
14 #include <iostream>
15 #include <sstream>
16 
17 namespace tlx {
18 
19 /******************************************************************************/
20 
21 static std::atomic<bool> s_die_with_exception {
22 #if TLX_DIE_WITH_EXCEPTION
23  true
24 #else
25  false
26 #endif
27 };
28 
29 void die_with_message(const std::string& msg) {
31  throw DieException(msg);
32  }
33  else {
34  std::cerr << msg << std::endl;
35  std::terminate();
36  }
37 }
38 
39 void die_with_message(const char* msg, const char* file, size_t line) {
40  std::ostringstream oss;
41  oss << msg << " @ " << file << ':' << line;
42  die_with_message(oss.str());
43 }
44 
45 void die_with_message(const std::string& msg, const char* file, size_t line) {
46  return die_with_message(msg.c_str(), file, line);
47 }
48 
50  : std::runtime_error(message) { }
51 
52 bool set_die_with_exception(bool b) {
53  return s_die_with_exception.exchange(b);
54 }
55 
56 /******************************************************************************/
57 /** \page tlx_die die() - Simple Invariant Testing
58 
59 tlx contains a set of macros called `die_...` for simple invariant testing. They
60 test some condition and fail with nice output containing both the condition, and
61 file/line information where it occurred.
62 
63 - `die(message)` - always terminates with given message.
64 - `die_unless(condition)` - terminates if condition is false
65 - `die_if(condition)` - terminates if condition is true
66 - `die_verbose_unless(condition,message)` - terminates if condition is false
67 - `die_verbose_if(condition,message)` - terminates if condition is true
68 
69 - `die_unequal(a,b)` - terminates unless a == b.
70 - `die_unequal_eps6(a,b)` - terminates unless abs(a - b) < 1e-6 for approximate equality.
71 - `die_equal(a,b)` - terminates if a == b.
72 - `die_unless_throws(code,exception)` - terminate if code does not throw the exception
73 
74 Furthermore, some additional assert macros are also available. These are only
75 active in Debug mode, if NDEBUG is defined they are compiled out.
76 
77 - `assert_equal(a,b)` - checks if a == b.
78 - `assert_unequal(a,b)` - checks if a != b.
79 
80 tlx die macros can also be modified to throw a DieException instead of calling
81 std::terminate. Either call `set_die_with_exception(true)` to define
82 TLX_DIE_WITH_EXCEPTION=1 using the preprocessor.
83 
84  */
85 
86 } // namespace tlx
87 
88 /******************************************************************************/
bool set_die_with_exception(bool b)
Definition: core.cpp:52
STL namespace.
DieException(const std::string &message)
Definition: core.cpp:49
void die_with_message(const std::string &msg)
die with message - either throw an exception or die via std::terminate()
Definition: core.cpp:29
Exception thrown by die_with_message() if.
Definition: core.hpp:50
static std::atomic< bool > s_die_with_exception
Definition: core.cpp:21
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220