-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy patherror_code.cpp
More file actions
193 lines (173 loc) · 4.97 KB
/
error_code.cpp
File metadata and controls
193 lines (173 loc) · 4.97 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <fmt/core.h>
#include <system_error>
#include <span>
#include "tl-expected.hpp"
#include <fcntl.h>
// 今日主题:现代 C++ 中的错误处理
// int : [INT_MIN, INT_MAX]
// optional<int> : [INT_MIN, INT_MAX] | {nullopt}
// variant<int, error_code> : [INT_MIN, INT_MAX] | {error_code}
// expected<int, error_code> : [INT_MIN, INT_MAX] | {error_code}
template <>
struct tl::bad_expected_access<std::error_code> : std::system_error {
explicit bad_expected_access(std::error_code e) : std::system_error(std::move(e)) {}
};
namespace mybuss {
enum class login_errc {
success = 0,
not_valid_pass,
not_login,
};
auto const &login_category() {
static const struct : std::error_category {
virtual std::string message(int val) const override {
switch ((login_errc)val) {
case login_errc::success:
return "登录成功!";
case login_errc::not_valid_pass:
return "密码不正确!";
case login_errc::not_login:
return "用户未登录!";
default:
return "未知错误!";
};
}
virtual const char *name() const noexcept override {
return "login";
}
} instance;
return instance;
}
std::error_code make_error_code(login_errc ec) {
return std::error_code((int)ec, login_category());
}
}
tl::expected<int, std::error_code> sqrt(int x) {
// 假装这是一个关于网站登录的业务函数
if (x < 0) {
return tl::unexpected{make_error_code(std::errc::argument_out_of_domain)};
}
if (x == 3) {
return tl::unexpected{make_error_code(mybuss::login_errc::not_valid_pass)};
}
if (x == 4) {
return tl::unexpected{make_error_code(mybuss::login_errc::not_login)};
}
for (int i = 0;; i++) {
if (i * i >= x) {
return i;
}
}
}
tl::expected<int, std::error_code> sqrfloor(int x) {
if (x < 1) {
return tl::unexpected{make_error_code(std::errc::invalid_argument)};
}
auto ret = sqrt(x * x);
return ret.map([&] (int x) { fmt::println("x * 2"); return x * 2; });
// 等价于:
// if (!ret.has_value()) {
// return tl::unexpected{ret.error()};
// }
// x = ret.value();
// x *= 2;
// return x;
// return ret.map_error([&] (std::error_code ec) {
// if (ec == make_error_code(mybuss::login_errc::not_login)) {
// ec = make_error_code(mybuss::login_errc::not_valid_pass);
// }
// return ec;
// });
// 等价于:
// if (!ret.has_value()) {
// if (ret.error() == make_error_code(mybuss::login_errc::not_login)) {
// ret.error() = make_error_code(mybuss::login_errc::not_valid_pass);
// }
// }
}
tl::expected<int, std::error_code> expectedStdError(int ret) {
if (ret == -1) {
return tl::unexpected{std::error_code(errno, std::generic_category())};
}
return ret;
}
int checkStdError(int ret) {
if (ret == -1) {
throw std::system_error(std::error_code(errno, std::system_category()));
}
return ret;
}
// tl::expected<void, std::error_code> expectedCudaError(int ret) {
// if (ret != 0) {
// return tl::unexpected{std::error_code(ret, cuda_category())};
// }
// return {};
// }
// template <class T>
// using expected = tl::expected<T, std::error_code>;
struct RAIIFile {
int fd;
tl::expected<size_t, std::error_code> write(std::span<const char> buf) {
return expectedStdError(::write(fd, buf.data(), buf.size()));
}
};
// tl::expected<void *, std::error_code> cppCudaMalloc(size_t size) {
// expectedCudaError(cudaMalloc(p));
// }
int main() {
RAIIFile file{expectedStdError(::open("/tmp/test.log", O_WRONLY)).value()};
std::string s = "asasasas";
file.write(s).value();
return 0;
}
// namespace screenshot1 {
//
// namespace std {
// using namespace ::tl;
// using namespace ::std;
// }
//
// std::expected<int, std::error_code> expectedStdError(int ret) {
// if (ret == -1) {
// return std::unexpected{std::error_code(errno, std::generic_category())};
// }
// return ret;
// }
//
// struct File {
// int fd;
//
// explicit File(const char *path, int flags) {
// fd = expectedStdError(::open(path, flags)).value();
// }
//
// tl::expected<size_t, std::error_code> write(std::span<const char> buf) {
// return expectedStdError(::write(fd, buf.data(), buf.size()));
// }
// };
//
// std::expected<int, std::error_code> sqrt(int x) {
// if (x < 0)
// return std::unexpected{make_error_code(std::errc::argument_out_of_domain)};
//
// for (int i = 0;; i++)
// if (i * i >= x)
// return i;
// }
//
// }
//
// namespace screenshot2 {
//
// int sqrt(int x) {
// if (x < 0) {
// errno = EDOM;
// return -1;
// }
//
// for (int i = 0;; i++)
// if (i * i >= x)
// return i;
// }
//
// }