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
28namespace sese {
29
32template<class T>
33class Result final {
34 std::variant<ErrorCode, T> err_result;
35
36public:
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
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
82template<class T>
86
87template<class T>
91
92} // namespace sese