-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.h
65 lines (54 loc) · 1.31 KB
/
error.h
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
// include this file after defining ERROR_PREFIX
// to expose error reading functions to the abi
// example: #define ERROR_PREFIX pegggml
#include "common.h"
#include "util.h"
#define ERROR_PREFIX peggml
#ifndef ERROR_PREFIX
#error "ERROR_PREFIX must be defined"
#endif
class glue(ERROR_PREFIX, ErrorManager)
{
public:
std::string m_error;
bool m_error_occurred = false;
void _error(const std::string& message)
{
m_error = message;
m_error_occurred = true;
}
void _clear_error()
{
m_error = "";
m_error_occurred = false;
}
};
namespace
{
glue(ERROR_PREFIX, ErrorManager) gError;
}
#define set_error(e) gError._error(e)
#define clear_error(e) gError._clear_error(e)
#define error(v, ...) [&](){gError._error(strprintf(__VA_ARGS__)); return v;}()
// returns 1 if an error occurred, 0 otherwise.
external ty_real glue(ERROR_PREFIX, _error)()
{
return gError.m_error_occurred ? 0.0 : 1.0;
}
// returns error string for most recent error
external ty_string glue(ERROR_PREFIX, _error_str)()
{
return gError.m_error.c_str();
}
// sets error string
external ty_real glue(ERROR_PREFIX, _set_error)(ty_string s)
{
gError._error(s);
return 0;
}
// resets error
external ty_real glue(ERROR_PREFIX, _clear_error)()
{
gError._clear_error();
return 0;
}