This repository was archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqexceptionstream.h
More file actions
65 lines (50 loc) · 1.34 KB
/
qexceptionstream.h
File metadata and controls
65 lines (50 loc) · 1.34 KB
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
#ifndef QEXCEPTIONSTREAM_H
#define QEXCEPTIONSTREAM_H
#include <exception>
#include <type_traits>
#include <QDebug>
template <typename TException>
class QExceptionStream : public QDebug
{
static_assert(std::is_base_of<std::exception, TException>::value, "TException must implement std::exception");
Q_DISABLE_COPY(QExceptionStream)
public:
QExceptionStream();
~QExceptionStream() noexcept(false);
QExceptionStream<TException> &allowThrow();
QExceptionStream<TException> &disallowThrow();
private:
QString _msg;
bool _canThrow = true;
};
#define qThrow(type) QExceptionStream<type>{}
template<typename TException>
QExceptionStream<TException>::QExceptionStream() :
QDebug{QtDebugMsg}
{
QDebug tmpDbg{&_msg};
QDebug::swap(tmpDbg); //because of initialization order
}
template<typename TException>
QExceptionStream<TException>::~QExceptionStream() noexcept(false)
{
{
QDebug tmpDbg{QtDebugMsg};
QDebug::swap(tmpDbg); // force flushing by "destroying" the internal debug early
}
if(_canThrow)
throw TException{std::move(_msg)};
}
template<typename TException>
QExceptionStream<TException> &QExceptionStream<TException>::allowThrow()
{
_canThrow = true;
return *this;
}
template<typename TException>
QExceptionStream<TException> &QExceptionStream<TException>::disallowThrow()
{
_canThrow = false;
return *this;
}
#endif // QEXCEPTIONSTREAM_H