LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
result.hpp
浏览该文件的文档.
1#pragma once
2
3#include <cstddef>
4#include <stdexcept>
5#include <string>
6#include <utility>
7#include <type_traits>
8#include <variant>
9
10namespace LMCAS {
11
30
31struct CasError {
33 std::string message;
34 std::string operation;
35};
36
37
38template <typename T>
39class [[nodiscard]] Result {
40public:
41 /** Allow direct return of a success value from a Result-returning function. */
42 Result(T value) : storage_(std::move(value)) {}
43
44 template <typename U = T,
45 std::enable_if_t<
46 std::is_constructible_v<U, std::nullptr_t>, int> = 0>
47 Result(std::nullptr_t) : storage_(T(nullptr)) {}
48 static Result success(T value) {
49 return Result(std::move(value));
50 }
51
52 static Result failure(CasErrc code, std::string message,
53 std::string operation = {}) {
54 return Result(CasError{code, std::move(message), std::move(operation)});
55 }
56
58 return Result(std::move(error));
59 }
60
61 bool has_value() const noexcept {
62 return std::holds_alternative<T>(storage_);
63 }
64
65 explicit operator bool() const noexcept { return has_value(); }
66
67 T& value() {
68 if (!has_value()) throw std::logic_error("Result does not contain a value");
69 return std::get<T>(storage_);
70 }
71
72 const T& value() const {
73 if (!has_value()) throw std::logic_error("Result does not contain a value");
74 return std::get<T>(storage_);
75 }
76
78 if (has_value()) throw std::logic_error("Result does not contain an error");
79 return std::get<CasError>(storage_);
80 }
81
82 const CasError& error() const {
83 if (has_value()) throw std::logic_error("Result does not contain an error");
84 return std::get<CasError>(storage_);
85 }
86
87private:
88 explicit Result(CasError error) : storage_(std::move(error)) {}
89
90 std::variant<T, CasError> storage_;
91};
92
93
94template <>
95class [[nodiscard]] Result<void> {
96public:
97 static Result success() { return Result(); }
98
99 static Result failure(CasErrc code, std::string message,
100 std::string operation = {}) {
101 return Result(CasError{code, std::move(message), std::move(operation)});
102 }
103
104 static Result failure(CasError error) { return Result(std::move(error)); }
105
106 bool has_value() const noexcept { return !has_error_; }
107 explicit operator bool() const noexcept { return has_value(); }
108
109 const CasError& error() const {
110 if (!has_error_) throw std::logic_error("Result does not contain an error");
111 return error_;
112 }
113
114private:
115 Result() = default;
116 explicit Result(CasError error) : has_error_(true), error_(std::move(error)) {}
117
118 bool has_error_ = false;
120};
121
123
124template <typename T>
130
131} // namespace LMCAS
const CasError & error() const
static Result failure(CasErrc code, std::string message, std::string operation={})
Result(CasError error)
bool has_value() const noexcept
static Result failure(CasError error)
static Result success()
const CasError & error() const
Result(std::nullptr_t)
static Result failure(CasError error)
Result(T value)
Allow direct return of a success value from a Result-returning function.
std::variant< Tribool, CasError > storage_
bool has_value() const noexcept
static Result failure(CasErrc code, std::string message, std::string operation={})
Result(CasError error)
static Result success(T value)
const T & value() const
Completeness completeness
std::string message
std::string operation