-
Notifications
You must be signed in to change notification settings - Fork 23
/
except.h
148 lines (123 loc) · 2.72 KB
/
except.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
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
/*
* This file is part of the Advance project.
*
* Copyright (C) 2002 Andrea Mazzoleni
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __EXCEPT_H
#define __EXCEPT_H
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
class error {
std::string function;
std::string file;
unsigned line;
std::string desc;
public:
error() : line(0)
{
}
error(const char* Afunction, const char* Afile, unsigned Aline) : function(Afunction), file(Afile), line(Aline)
{
}
const std::string& desc_get() const
{
return desc;
}
const std::string& function_get() const
{
return function;
}
const std::string& file_get() const
{
return file;
}
unsigned line_get() const
{
return line;
}
error& operator<<(const char* A)
{
desc += A;
return *this;
}
error& operator<<(const std::string& A)
{
desc += A;
return *this;
}
error& operator<<(const unsigned A)
{
std::ostringstream s;
s << A /* << " (" << std::hex << A << "h)" */ ;
desc += s.str();
return *this;
}
};
class error_invalid : public error {
public:
error_invalid() : error()
{
}
error_invalid& operator<<(const char* A)
{
error::operator<<(A);
return *this;
}
error_invalid& operator<<(const std::string& A)
{
error::operator<<(A);
return *this;
}
error_invalid& operator<<(const unsigned A)
{
error::operator<<(A);
return *this;
}
};
class error_unsupported : public error {
public:
error_unsupported() : error()
{
}
error_unsupported& operator<<(const char* A)
{
error::operator<<(A);
return *this;
}
error_unsupported& operator<<(const std::string& A)
{
error::operator<<(A);
return *this;
}
error_unsupported& operator<<(const unsigned A)
{
error::operator<<(A);
return *this;
}
};
#define error() \
error(__PRETTY_FUNCTION__, __FILE__, __LINE__)
static inline std::ostream& operator<<(std::ostream& os, const error& e)
{
os << e.desc_get();
if (e.function_get().length() || e.file_get().length() || e.line_get())
os << " [at " << e.function_get() << ":" << e.file_get() << ":" << e.line_get() << "]";
return os;
}
#endif