-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorHandler.h
More file actions
43 lines (38 loc) · 1.4 KB
/
Copy pathErrorHandler.h
File metadata and controls
43 lines (38 loc) · 1.4 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
#pragma once
#include <iostream>
#include <sstream>
namespace ErrorHandler
{
template<typename TExceptionType, typename ...TArgs>
concept TemplatedTypesConstraints = requires(std::string s, std::ostringstream oss, TArgs... args)
{
TExceptionType(s); // TExceptionType must be constructible using a std::string
(oss << ... << args); // All args must be streamable
};
class BasicException : public std::exception
{
protected:
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, typename ...TArgs>
requires TemplatedTypesConstraints<TExceptionType, TArgs...>
void raise_error(const TArgs & ...args)
{
std::ostringstream oss;
(oss << ... << args);
const std::string error_str = oss.str();
std::cerr << error_str << std::endl;
throw TExceptionType(error_str);
}
template<typename TExceptionType = BasicException, typename ...TArgs>
requires TemplatedTypesConstraints<TExceptionType, TArgs...>
void assert_p(bool predicate, const TArgs & ...args)
{
if (!predicate)
raise_error<TExceptionType>(args...);
}
};