tlx
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 <tb@panthema.net>
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
17namespace tlx {
18
19/******************************************************************************/
20
21static std::atomic<bool> s_die_with_exception {
22#if TLX_DIE_WITH_EXCEPTION
23 true
24#else
25 false
26#endif
27};
28
29void 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
39void 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
45void 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
49DieException::DieException(const std::string& message)
50 : std::runtime_error(message) { }
51
53 return s_die_with_exception.exchange(b);
54}
55
56/******************************************************************************/
57/** \page tlx_die die() - Simple Invariant Testing
58
59tlx contains a set of macros called `die_...` for simple invariant testing. They
60test some condition and fail with nice output containing both the condition, and
61file/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
74Furthermore, some additional assert macros are also available. These are only
75active 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
80tlx die macros can also be modified to throw a DieException instead of calling
81std::terminate. Either call `set_die_with_exception(true)` to define
82TLX_DIE_WITH_EXCEPTION=1 using the preprocessor.
83
84 */
85
86} // namespace tlx
87
88/******************************************************************************/
Exception thrown by die_with_message() if.
Definition: core.hpp:51
DieException(const std::string &message)
Definition: core.cpp:49
STL namespace.
void die_with_message(const std::string &msg)
die with message - either throw an exception or die via std::terminate()
Definition: core.cpp:29
static std::atomic< bool > s_die_with_exception
Definition: core.cpp:21
bool set_die_with_exception(bool b)
Switch between dying via std::terminate() and throwing an exception.
Definition: core.cpp:52