Sese Framework
x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Result.h
浏览该文件的文档.
1
// Copyright 2024 libsese
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
19
20
#pragma once
21
22
#include "
sese/net/Socket.h
"
23
#include "
Util.h
"
24
#include "
ErrorCode.h
"
25
26
#include <variant>
27
28
namespace
sese
{
29
32
template
<
class
T>
33
class
Result
final {
34
std::variant<ErrorCode, T>
err_result
;
35
36
public
:
39
Result
(
ErrorCode
err
) :
err_result
(
err
) {}
// NOLINT
40
44
Result
(int32_t code,
const
std::string &msg) :
err_result
(
ErrorCode
{code, msg}) {}
45
48
Result
(T result) :
err_result
(std::forward<T>(result)) {}
// NOLINT
49
50
static
Result
fromLastError
();
51
52
static
Result
fromLastNetworkError
();
53
56
explicit
operator
bool() const noexcept {
57
return
std::holds_alternative<ErrorCode>(
err_result
);
58
}
59
62
[[nodiscard]]
ErrorCode
err
() const noexcept {
63
assert(std::holds_alternative<ErrorCode>(
err_result
));
64
return
std::get<ErrorCode>(
err_result
);
65
}
66
69
T &
get
() noexcept {
70
assert(std::holds_alternative<T>(
err_result
));
71
return
std::get<T>(
err_result
);
72
}
73
76
const
T &
get
() const noexcept {
77
assert(std::holds_alternative<T>(
err_result
));
78
return
std::get<T>(
err_result
);
79
}
80
};
81
82
template
<
class
T>
83
Result<T>
Result<T>::fromLastError
() {
84
return
{
getErrorCode
(),
getErrorString
()};
85
}
86
87
template
<
class
T>
88
Result<T>
Result<T>::fromLastNetworkError
() {
89
return
{
net::getNetworkError
(),
net::getNetworkErrorString
()};
90
}
91
92
}
// namespace sese
sese
util
Result.h
生成于 2024年 十一月 4日 星期一 09:58:03 , 为 Sese Framework使用
1.11.0