-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexception.h
72 lines (61 loc) · 1.26 KB
/
exception.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
// This file is part of the PiEMU Project
// Licensing information can be found in the LICENSE file
// (C) 2014 Nandor Licker. All rights reserved.
#ifndef __EXCEPTION_H__
#define __EXCEPTION_H__
/**
* Class supporting formatted error messages
*/
class Exception : public std::exception
{
public:
/**
* Constructs a new exception object
*/
Exception()
: os("")
{
}
/**
* Copy constructor
*/
Exception(const Exception& e)
: os(e.os.str())
{
}
/**
* Constructs a new exception object, including source information
* @param file Name of the throwing file
* @param line Line number
* @param func Name of the function
*/
Exception(const std::string& file, int line, const std::string& func);
/**
* Returns the error message
*/
const char *what() const throw()
{
return os.str().c_str();
}
/**
* Appends a message to the end of stream
* @param T type of data
* @param t stuff to be written
*/
template<typename T>
Exception& operator << (const T& t)
{
os << t;
return *this;
}
private:
/**
* Format stream
*/
std::ostringstream os;
};
/**
* Macro to throw nice exceptions
*/
#define EXCEPT throw Exception(__FILE__, __LINE__, __FUNCTION__)
#endif /*__EXCEPTION_H__*/