summaryrefslogtreecommitdiff
path: root/includes/Exceptions.h
blob: 729f523fc5ec0005c73a5df3aefd8623b18ece79 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once

#include <stdarg.h>
#include <typeinfo>
#include <cxxabi.h>
#include <BString.h>
#include <vector>

namespace Balau {

class ClassName {
  public:
      template<typename T> ClassName(T * ptr);
      ~ClassName() { free(m_demangled); }
    const char * c_str() const { return m_demangled; }
  private:
    char * m_demangled;
};

class GeneralException {
  public:
      GeneralException(const char * msg, const char * details = NULL, bool notrace = false) : m_msg(msg ? ::strdup(msg) : NULL) { setDetails(details); if (!notrace) genTrace(); }
      GeneralException(const String & msg, const char * details = NULL, bool notrace = false) : m_msg(msg.strdup()) { setDetails(details); if (!notrace) genTrace(); }
      GeneralException(const GeneralException & e) : m_msg(e.m_msg ? strdup(e.m_msg) : NULL), m_trace(e.m_trace) { setDetails(e.m_details); }
      GeneralException(GeneralException && e) : m_msg(e.m_msg), m_trace(e.m_trace), m_details(e.m_details) { e.m_msg = e.m_details = NULL; }
      // don't hate me, this is to generate typeinfo in the getMsg() case where there's no message.
      virtual ~GeneralException() { if (m_msg) free(m_msg); if (m_details) free(m_details); }
    const char * getMsg() const {
        if (!m_msg)
            m_msg = ::strdup(ClassName(this).c_str());
        return m_msg;
    }
    const char * getDetails() const { return m_details; }
    const std::vector<String> getTrace() const { return m_trace; }

  protected:
      explicit GeneralException() { }
    void setMsg(char * msg) { if (m_msg) free(m_msg); m_msg = msg; }
    void genTrace();

  private:
    mutable char * m_msg = NULL;
    char * m_details = NULL;
    std::vector<String> m_trace;

    void setDetails(const char * details) {
        if (details)
            m_details = ::strdup(details);
        else
            m_details = NULL;
    }
};

class RessourceException : public GeneralException {
  public:
      RessourceException(const String & msg, const char * details) : GeneralException(msg, details) { }
};

void ExitHelper(const String & msg, const char * fmt = NULL, ...) __attribute__((format(printf, 2, 3)));

static inline void * malloc(size_t size) {
    void * r = ::malloc(size);

    if (!r && size)
        ExitHelper("Failed to allocate memory", "%zu bytes", size);

    return r;
}

static inline void * calloc(size_t count, size_t size) {
    void * r = ::calloc(count, size);

    if (!r && ((count * size) != 0))
        ExitHelper("Failed to allocate memory", "%zu * %zu = %zu bytes", count, size, count * size);

    return r;
}

static inline void * realloc(void * previous, size_t size) {
    void * r = ::realloc(previous, size);

    if (!r && size)
        ExitHelper("Failed to re-allocate memory", "%zu bytes", size);

    return r;
}

static inline void AssertHelperInner(const String & msg, const char * details = NULL) throw (GeneralException) {
    throw GeneralException(msg, details);
}

static inline void AssertHelper(const String & msg, const char * fmt, ...) __attribute__((format(printf, 2, 3)));

static inline void AssertHelper(const String & msg, const char * fmt, ...) {
    String details;
    va_list ap;
    va_start(ap, fmt);
    details.set(fmt, ap);
    va_end(ap);
    AssertHelperInner(msg, details.to_charp());
}

static inline void AssertHelper(const String & msg) {
    AssertHelperInner(msg);
}

class TestException : public GeneralException {
  public:
      TestException(const String & msg) : GeneralException(msg) { }
};

static inline void TestHelper(const String & msg) throw (TestException) {
    throw TestException(msg);
}

template<typename T>
ClassName::ClassName(T * ptr) {
    int status;
    m_demangled = abi::__cxa_demangle(typeid(*ptr).name(), 0, 0, &status);
}

};

#define Failure(msg) Balau::AssertHelper(msg);
#define FailureDetails(msg, ...) Balau::AssertHelper(msg, __VA_ARGS__);

#define IAssert(c, ...) if (!__builtin_expect(!!(c), 0)) { \
    Balau::String msg; \
    msg.set("Internal Assertion " #c " failed at %s:%i", __FILE__, __LINE__); \
    Balau::AssertHelper(msg, __VA_ARGS__); \
}

#define AAssert(c, ...) if (!__builtin_expect(!!(c), 0)) { \
    Balau::String msg; \
    msg.set("API Assertion " #c " failed at %s:%i", __FILE__, __LINE__); \
    Balau::AssertHelper(msg, __VA_ARGS__); \
}

#define RAssert(c, ...) if (!__builtin_expect(!!(c), 0)) { \
    Balau::String msg; \
    msg.set("Ressource Assertion " #c " failed at %s:%i", __FILE__, __LINE__); \
    Balau::ExitHelper(msg, __VA_ARGS__); \
}

#define EAssert(c, ...) if (!__builtin_expect(!!(c), 0)) { \
    Balau::String msg; \
    msg.set("Execution Assertion " #c " failed at %s:%i", __FILE__, __LINE__); \
    Balau::AssertHelper(msg, __VA_ARGS__); \
}

#define TAssert(c) if (!__builtin_expect(!!(c), 0)) { \
    Balau::String msg; \
    msg.set("UnitTest Assert " #c " failed at %s:%i", __FILE__, __LINE__); \
    Balau::TestHelper(msg); \
}