summaryrefslogtreecommitdiff
path: root/includes/Exceptions.h
blob: 799bfb6d602cfac86c824050e0778ef5716752d3 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once

#include <stdarg.h>
#include <typeinfo>
#include <functional>

#ifndef _MSC_VER
#include <cxxabi.h>
#else
#include <intrin.h>
#endif

#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, ...) printfwarning(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) {
#if defined(_MSC_VER) && defined(DEBUG)
    __debugbreak();
#endif
    throw GeneralException(msg, details);
}

static inline void AssertHelper(const String & msg, const char * fmt, ...) printfwarning(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) {
#ifdef _MSC_VER
	m_demangled = strdup(typeid(*ptr).name());
#else
	int status;
	m_demangled = abi::__cxa_demangle(typeid(*ptr).name(), 0, 0, &status);
#endif
}

};

#ifdef _MSC_VER
#define likely(expr)    (expr)
#define unlikely(expr)  (expr)
#else
#define likely(expr)    __builtin_expect((expr), !0)
#define unlikely(expr)  __builtin_expect((expr), 0)
#endif

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

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

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

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

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

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

class ScopedLambda {
  public:
      ScopedLambda(std::function<void()> l) : m_l(l) { }
      ~ScopedLambda() { m_l(); }
  private:
    std::function<void()> m_l;
};