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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* This source code is released into the public domain.
*/
module;
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <system_error>
#include <variant>
module nihil.error;
import nihil.match;
namespace nihil {
auto to_string(error const &self) -> std::string
{
auto ret = self.str();
auto cause = self.cause();
while (cause) {
ret += ": " + cause->str();
cause = cause->cause();
}
return ret;
}
error::error()
{
}
error::~error() = default;
error::error(std::string_view what, error cause)
: m_error(std::string(what))
, m_cause(std::make_shared<error>(std::move(cause)))
{
}
error::error(std::string_view what)
: m_error(std::string(what))
{
}
error::error(std::error_condition what, error cause)
: m_error(what)
, m_cause(std::make_shared<error>(std::move(cause)))
{
}
error::error(std::error_condition what)
: m_error(what)
{
}
error::error(std::error_code what, error cause)
: m_error(what)
, m_cause(std::make_shared<error>(std::move(cause)))
{
}
error::error(std::error_code what)
: m_error(what)
{
}
error::error(error const &) = default;
error::error(error &&) noexcept = default;
auto error::operator=(this error &, error const &) -> error & = default;
auto error::operator=(this error &, error &&) noexcept -> error & = default;
auto error::cause(this error const &self) -> std::shared_ptr<error>
{
if (self.m_cause)
return self.m_cause;
return {};
}
auto error::root_cause(this error const &self) -> error const &
{
if (self.m_cause)
return self.m_cause->root_cause();
return self;
}
auto error::str(this error const &self) -> std::string
{
return self.m_error | match {
[] (std::monostate) -> std::string {
return "No error";
},
[] (std::error_code const &m) {
return m.message();
},
[] (std::error_condition const &m) {
return m.message();
},
[] (std::string const &m) {
return m;
}
};
}
auto error::code(this error const &self) -> std::optional<std::error_code>
{
auto const *code = std::get_if<std::error_code>(&self.m_error);
if (code)
return {*code};
return {};
}
auto error::condition(this error const &self)
-> std::optional<std::error_condition>
{
auto const *condition = std::get_if<std::error_condition>(&self.m_error);
if (condition)
return {*condition};
return {};
}
auto error::what() const noexcept -> char const *
{
if (!m_what)
m_what = to_string(*this);
return m_what->c_str();
}
auto operator==(error const &lhs, error const &rhs) -> bool
{
return lhs.m_error == rhs.m_error;
}
auto operator<=>(error const &lhs, error const &rhs) -> std::strong_ordering
{
return lhs.m_error <=> rhs.m_error;
}
auto operator==(error const &lhs, std::error_code const &rhs) -> bool
{
return lhs.code() == rhs;
}
// Compare an error to an std::error_condition.
auto operator==(error const &lhs, std::error_condition const &rhs) -> bool
{
return lhs.condition() == rhs;
}
auto operator<<(std::ostream &strm, error const &e) -> std::ostream &
{
return strm << to_string(e);
}
} // namespace nihil
|