LMCAS
Lamina Computer Algebra System
Toggle main menu visibility
载入中...
搜索中...
未找到
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
10
namespace
LMCAS
{
11
12
enum class
CasErrc
{
13
InvalidArgument
,
14
ParseError
,
15
UnboundSymbol
,
16
DomainError
,
17
DimensionMismatch
,
18
UnitInvalid
,
19
UnitStripTypeMismatch
,
20
SetElementTypeMismatch
,
21
SetOperandTypeMismatch
,
22
SetElementNotHashable
,
23
UnsupportedExpression
,
24
Inconclusive
,
25
ResourceLimit
,
26
Cancelled
,
27
NumericFailure
,
28
InternalInvariant
29
};
30
31
struct
CasError
{
32
CasErrc
code
=
CasErrc::InternalInvariant
;
33
std::string
message
;
34
std::string
operation
;
35
};
36
37
38
template
<
typename
T>
39
class
[[nodiscard]]
Result
{
40
public
:
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
57
static
Result
failure
(
CasError
error
) {
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
77
CasError
&
error
() {
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
87
private
:
88
explicit
Result
(
CasError
error
) :
storage_
(std::move(
error
)) {}
89
90
std::variant<T, CasError>
storage_
;
91
};
92
93
94
template
<>
95
class
[[nodiscard]]
Result
<void> {
96
public
:
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
114
private
:
115
Result
() =
default
;
116
explicit
Result
(
CasError
error
) :
has_error_
(true),
error_
(std::move(
error
)) {}
117
118
bool
has_error_
=
false
;
119
CasError
error_
{};
120
};
121
122
enum class
Completeness
{
Complete
,
Inconclusive
};
123
124
template
<
typename
T>
125
struct
MathResult
{
126
T
value
;
127
Completeness
completeness
=
Completeness::Complete
;
128
std::string
reason
;
129
};
130
131
}
// namespace LMCAS
LMCAS::Result< void >::error_
CasError error_
定义
result.hpp:119
LMCAS::Result< void >::Result
Result()=default
LMCAS::Result< void >::error
const CasError & error() const
定义
result.hpp:109
LMCAS::Result< void >::has_error_
bool has_error_
定义
result.hpp:118
LMCAS::Result< void >::failure
static Result failure(CasErrc code, std::string message, std::string operation={})
定义
result.hpp:99
LMCAS::Result< void >::Result
Result(CasError error)
定义
result.hpp:116
LMCAS::Result< void >::has_value
bool has_value() const noexcept
定义
result.hpp:106
LMCAS::Result< void >::failure
static Result failure(CasError error)
定义
result.hpp:104
LMCAS::Result< void >::success
static Result success()
定义
result.hpp:97
LMCAS::Result
定义
result.hpp:39
LMCAS::Result::error
const CasError & error() const
定义
result.hpp:82
LMCAS::Result< Tribool >::value
Tribool & value()
定义
result.hpp:67
LMCAS::Result::Result
Result(std::nullptr_t)
定义
result.hpp:47
LMCAS::Result::failure
static Result failure(CasError error)
定义
result.hpp:57
LMCAS::Result::Result
Result(T value)
Allow direct return of a success value from a Result-returning function.
定义
result.hpp:42
LMCAS::Result< Tribool >::error
CasError & error()
定义
result.hpp:77
LMCAS::Result< Tribool >::storage_
std::variant< Tribool, CasError > storage_
定义
result.hpp:90
LMCAS::Result::has_value
bool has_value() const noexcept
定义
result.hpp:61
LMCAS::Result::failure
static Result failure(CasErrc code, std::string message, std::string operation={})
定义
result.hpp:52
LMCAS::Result::Result
Result(CasError error)
定义
result.hpp:88
LMCAS::Result::success
static Result success(T value)
定义
result.hpp:48
LMCAS::Result::value
const T & value() const
定义
result.hpp:72
LMCAS
定义
assumption.hpp:11
LMCAS::Completeness
Completeness
定义
result.hpp:122
LMCAS::Completeness::Complete
@ Complete
定义
result.hpp:122
LMCAS::MathResult::completeness
Completeness completeness
定义
result.hpp:127
LMCAS::CasErrc
CasErrc
定义
result.hpp:12
LMCAS::CasErrc::Inconclusive
@ Inconclusive
定义
result.hpp:24
LMCAS::CasErrc::UnboundSymbol
@ UnboundSymbol
定义
result.hpp:15
LMCAS::CasErrc::InvalidArgument
@ InvalidArgument
定义
result.hpp:13
LMCAS::CasErrc::DimensionMismatch
@ DimensionMismatch
定义
result.hpp:17
LMCAS::CasErrc::SetOperandTypeMismatch
@ SetOperandTypeMismatch
定义
result.hpp:21
LMCAS::CasErrc::UnsupportedExpression
@ UnsupportedExpression
定义
result.hpp:23
LMCAS::CasErrc::InternalInvariant
@ InternalInvariant
定义
result.hpp:28
LMCAS::CasErrc::ResourceLimit
@ ResourceLimit
定义
result.hpp:25
LMCAS::CasErrc::SetElementNotHashable
@ SetElementNotHashable
定义
result.hpp:22
LMCAS::CasErrc::NumericFailure
@ NumericFailure
定义
result.hpp:27
LMCAS::CasErrc::DomainError
@ DomainError
定义
result.hpp:16
LMCAS::CasErrc::Cancelled
@ Cancelled
定义
result.hpp:26
LMCAS::CasErrc::UnitInvalid
@ UnitInvalid
定义
result.hpp:18
LMCAS::CasErrc::SetElementTypeMismatch
@ SetElementTypeMismatch
定义
result.hpp:20
LMCAS::CasErrc::ParseError
@ ParseError
定义
result.hpp:14
LMCAS::CasErrc::UnitStripTypeMismatch
@ UnitStripTypeMismatch
定义
result.hpp:19
LMCAS::MathResult::value
T value
定义
result.hpp:126
LMCAS::CasError::message
std::string message
定义
result.hpp:33
LMCAS::MathResult::reason
std::string reason
定义
result.hpp:128
LMCAS::CasError::operation
std::string operation
定义
result.hpp:34
LMCAS::CasError::code
CasErrc code
定义
result.hpp:32
LMCAS::MathResult
定义
result.hpp:125
LMCAS::CasError
定义
result.hpp:31
include
result.hpp
生成于
, 为 LMCAS使用
1.18.0