-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.h
More file actions
59 lines (49 loc) · 1.86 KB
/
ErrorHandler.h
File metadata and controls
59 lines (49 loc) · 1.86 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
#pragma once
#include <iostream>
#include <ostream>
#include <exception>
#include <utility>
class ErrorHandler
{
template<typename TExceptionType, typename THead>
static void raise_error_recursion(std::ostringstream & error_string_stream, const THead & arg_head)
{
error_string_stream << arg_head;
const std::string current_error_str = error_string_stream.str();
std::cerr << current_error_str << std::endl;
throw TExceptionType(current_error_str);
}
template<typename TExceptionType, typename THead, typename ...TTail>
static void raise_error_recursion(std::ostringstream & error_string_stream, const THead & arg_head, const TTail & ...arg_tail)
{
error_string_stream << arg_head;
raise_error_recursion<TExceptionType>(error_string_stream, arg_tail...);
}
public:
class BasicException : std::exception
{
std::string m_what;
public:
BasicException(const std::string & what): m_what(what) {}
BasicException(std::string && what): m_what(std::forward<std::string>(what)) {}
const char * what() const noexcept override { return m_what.c_str(); };
};
template<typename TExceptionType = BasicException>
static void raise_error()
{
std::ostringstream error_string_stream;
raise_error_recursion<TExceptionType>(error_string_stream, "<Unknown error>");
}
template<typename TExceptionType = BasicException, typename ...TArgs>
static void raise_error(const TArgs & ...args)
{
std::ostringstream error_string_stream;
raise_error_recursion<TExceptionType>(error_string_stream, args...);
}
template<typename TExceptionType = BasicException, typename ...TArgs>
static void assert(bool predicate, const TArgs & ...args)
{
if (!predicate)
raise_error<TExceptionType>(args...);
}
};