This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResult.h
80 lines (65 loc) · 2.74 KB
/
Result.h
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
/*
This file is part of duckOS.
duckOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
duckOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with duckOS. If not, see <https://www.gnu.org/licenses/>.
Copyright (c) Byteduck 2016-2021. All rights reserved.
*/
#pragma once
#include <cassert>
#include <optional>
#include <cstring>
#define TRY(expr) \
({ \
auto res = (expr); \
if (res.is_error()) \
return res.result(); \
res.value(); \
})
namespace Duck {
class Result {
public:
Result(int code): m_code(code) {}
Result(int code, std::string message): m_code(code), m_message(std::move(message)) {}
Result(std::string message): m_code(FAILURE.m_code), m_message(std::move(message)) {}
[[nodiscard]] bool is_success() const { return m_code == 0; }
[[nodiscard]] bool is_error() const { return m_code; }
[[nodiscard]] int code() const { return m_code; }
[[nodiscard]] const char* strerror() const { return has_message() ? m_message.c_str() : ::strerror(m_code); }
[[nodiscard]] bool has_message() const { return !m_message.empty(); }
[[nodiscard]] std::string message() const { return has_message() ? m_message.c_str() : ::strerror(m_code); }
operator int() const { return m_code; }
static const Result SUCCESS;
static const Result FAILURE;
private:
int m_code;
std::string m_message;
};
template<typename T>
class ResultRet {
public:
ResultRet(Result error): m_ret(std::nullopt), m_result(error) {};
ResultRet(std::optional<T> ret): m_ret(std::move(ret)), m_result(0) {};
ResultRet(T ret): m_ret(std::move(ret)), m_result(0) {};
[[nodiscard]] bool is_error() const { return m_result.is_error(); }
[[nodiscard]] bool has_value() const { return m_ret.has_value(); }
[[nodiscard]] int code() const { return m_result.code(); }
[[nodiscard]] Result result() const { return m_result; }
[[nodiscard]] const char* strerror() const { return m_result.strerror(); }
[[nodiscard]] std::string message() const { return m_result.message(); }
[[nodiscard]] bool has_message() const { return m_result.has_message(); }
[[nodiscard]] T& value() { return m_ret.value(); };
[[nodiscard]] constexpr T value_or(T&& alt) && { return m_ret.value_or(std::forward<T>(alt)); }
operator T&() { return m_ret.value(); }
private:
std::optional<T> m_ret;
Result m_result;
};
}