-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.hpp
73 lines (61 loc) · 1.56 KB
/
error.hpp
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
73
//@ {"targets":[{"name":"error.hpp","type":"include"}]}
#ifndef TEMPLE_ERROR_HPP
#define TEMPLE_ERROR_HPP
#include <cassert>
#include <cstddef>
namespace Temple
{
class Error
{
public:
template<class T,class...U>
explicit constexpr Error(const T& first,const U&...next) noexcept:m_buffer{}
{write(m_buffer,m_buffer+capacity(),first,next...);}
const char* message() const noexcept
{return m_buffer;}
static constexpr size_t capacity() noexcept
{return s_capacity;}
private:
static constexpr void write(char* buffer,const char* buffer_end)
{*buffer='\0';}
template<class T,class...U>
static constexpr void write(char* buffer,const char* buffer_end,const T& first,const U&...next)
{
if(buffer!=buffer_end)
{
buffer=concat(buffer,buffer_end,first);
write(buffer,buffer_end,next...);
}
else
{*buffer='\0';}
}
static constexpr char* concat(char* buffer,const char* buffer_end,const char* value)
{
do
{
auto val=*value;
if(val=='\0')
{return buffer;}
*buffer=val;
++value;
++buffer;
}
while(buffer!=buffer_end);
return buffer;
}
static constexpr char* concat(char* buffer,const char* buffer_end,char value)
{
*buffer=value;
return buffer+1;
}
static constexpr size_t s_capacity=1023;
char m_buffer[s_capacity + 1];
};
template<class ExceptionHandler>
[[noreturn]] static void raise(const Error& msg,ExceptionHandler& eh)
{
eh(msg);
assert(0 && "Exception handler must not return to its caller.");
}
}
#endif