File: Assert.cpp

package info (click to toggle)
pentobi 18.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,912 kB
  • sloc: cpp: 26,729; javascript: 907; xml: 49; sh: 35; makefile: 21; java: 11
file content (72 lines) | stat: -rw-r--r-- 1,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Assert.cpp
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#include "Assert.h"

#include <list>

#ifdef LIBBOARDGAME_DEBUG
#include <algorithm>
#include <functional>
#include <sstream>
#include <string>
#include <vector>
#include "Log.h"
#endif

namespace libboardgame_base {

using namespace std;

//-----------------------------------------------------------------------------

namespace {

list<AssertionHandler*>& get_all_handlers()
{
    static list<AssertionHandler*> all_handlers;
    return all_handlers;
}

} // namespace

//----------------------------------------------------------------------------

AssertionHandler::AssertionHandler()
{
    get_all_handlers().push_back(this);
}

AssertionHandler::~AssertionHandler()
{
    get_all_handlers().remove(this);
}

//----------------------------------------------------------------------------

#ifdef LIBBOARDGAME_DEBUG

void handle_assertion(
        [[maybe_unused]] const char* expression,
        [[maybe_unused]] const char* file, [[maybe_unused]] int line)
{
    static bool is_during_handle_assertion = false;
    LIBBOARDGAME_LOG(file, ":", line, ": Assertion '", expression, "' failed");
    flush_log();
    if (! is_during_handle_assertion)
    {
        is_during_handle_assertion = true;
        for_each(get_all_handlers().begin(), get_all_handlers().end(),
                 mem_fn(&AssertionHandler::run));
    }
    abort();
}

#endif

//-----------------------------------------------------------------------------

} // namespace libboardgame_base