-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.h
52 lines (43 loc) · 1021 Bytes
/
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
#pragma once
#include <exception>
#include <sstream>
#include <string>
class foo_discogs_exception : public std::exception
{
protected:
mutable std::string _mwhat;
mutable std::ostringstream *_mstream;
public:
foo_discogs_exception() : _mstream(nullptr) {}
foo_discogs_exception(const char* msg) : _mwhat(msg), _mstream(nullptr) {}
foo_discogs_exception(const foo_discogs_exception &that) {
if (that._mstream != nullptr) {
_mwhat = that._mstream->str();
}
else {
_mwhat = that._mwhat;
}
_mstream = nullptr;
}
~foo_discogs_exception() {
if (_mstream != nullptr) {
delete _mstream;
}
}
virtual const char *what() const override {
if (_mstream != nullptr && _mstream->str().size()) {
_mwhat = _mstream->str();
_mstream->str("");
}
return _mwhat.c_str();
}
template<typename T>
foo_discogs_exception& operator<<(const T& t) {
if (_mstream == nullptr) {
_mstream = new std::ostringstream();
(*_mstream) << _mwhat;
}
(*_mstream) << t;
return (*this);
}
};