-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex.hh
52 lines (46 loc) · 1.6 KB
/
Ex.hh
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
#pragma once
#include <cstdint>
#include <iostream>
#include <string>
/*
Represent an exception class that outputs a numeric error code into
a binary stream.
To keep things simple for now, we are just writing to text. This is
slower, but we are also writing filenames, and it's not clear how we
could do that with a binary file so for now this is it.
Be sure Errcode is defined for your application
*/
enum class Errcode;
class Ex {
public:
const char* filename; // the filename in which the error occurred
const uint32_t lineNum; // the linenumber of the file
const std::string name; // a string message, if any (nullptr if not)
int param; // an integer parameter, if any
Errcode e;
static const char* errNames[];
public:
Ex(const char filename[], uint32_t lineNum, Errcode e,
const std::string& name = "") :
filename(filename), lineNum(lineNum), e(e), name(name) {}
friend std::ostream& operator <<(std::ostream& s, const Ex& e) {
s << e.filename << ": " << e.lineNum << errNames[int(e.e)] << '\t';
if (e.name != "") {
s << e.name;
}
s << '\n';
return s;
}
};
class FatalEx : public Ex {
private:
public:
FatalEx(const char filename[], int lineNum, Errcode e,
const std::string& name = "") :
Ex(filename, lineNum, e, name) {}
};
// define macros that inject source file and line number into the exception
#define Ex1(e) Ex(__FILE__, __LINE__, e)
#define FatalEx1(e) FatalEx(__FILE__, __LINE__, e)
#define Ex2(e, stringparam) Ex(__FILE__, __LINE__, e, stringparam)
#define FatalEx2(e, stringparam) FatalEx(__FILE__, __LINE__, e, stringparam)