载入中...
搜索中...
未找到
Status.h
浏览该文件的文档.
1#pragma once
2
8#include "common/Diagnostic.h"
9#include "common/Export.h"
10
11#include <cstdint>
12#include <iosfwd>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <utility>
17#include <vector>
18
19namespace eve {
20
28enum class StatusCode : uint8_t {
29 Ok,
30 Applied,
31 NoOp,
32 Pending,
38 Failed,
39};
40
46[[nodiscard]] constexpr std::string_view statusCodeName(StatusCode code) noexcept {
47 switch (code) {
48 case StatusCode::Ok: return "ok";
49 case StatusCode::Applied: return "applied";
50 case StatusCode::NoOp: return "no_op";
51 case StatusCode::Pending: return "pending";
52 case StatusCode::Rejected: return "rejected";
53 case StatusCode::Conflict: return "conflict";
54 case StatusCode::NotFound: return "not_found";
55 case StatusCode::Unsupported: return "unsupported";
56 case StatusCode::Cancelled: return "cancelled";
57 case StatusCode::Failed: return "failed";
58 }
59 return "unknown";
60}
61
63EVENGINE_API_FOUNDATION std::ostream& operator<<(std::ostream& stream, StatusCode code);
64
70class [[nodiscard("Status must be inspected or explicitly passed onward")]] EVENGINE_API_FOUNDATION Status {
71public:
73 Status() noexcept;
75 ~Status() noexcept;
77 Status(const Status& other);
79 Status& operator=(const Status& other);
81 Status(Status&& other) noexcept;
83 Status& operator=(Status&& other) noexcept;
84
90 Status(StatusCode code, std::vector<Diagnostic> diagnostics);
91
93 static Status success(StatusCode code = StatusCode::Ok);
94
96 static Status failure(StatusCode code, Diagnostic diagnostic);
97
99 static Status failure(Diagnostic diagnostic);
100
102 StatusCode code() const noexcept { return code_; }
103
105 bool isSuccess() const noexcept {
106 switch (code_) {
107 case StatusCode::Ok:
108 case StatusCode::Applied:
109 case StatusCode::NoOp:
110 case StatusCode::Pending: return true;
111 case StatusCode::Rejected:
112 case StatusCode::Conflict:
113 case StatusCode::NotFound:
114 case StatusCode::Unsupported:
115 case StatusCode::Cancelled:
116 case StatusCode::Failed: return false;
117 }
118 return false;
119 }
120
122 bool isFailure() const noexcept { return !isSuccess(); }
123
125 bool hasDiagnostics() const noexcept { return diagnostics_ && !diagnostics_->empty(); }
126
128 const std::vector<Diagnostic>& diagnostics() const noexcept {
129 return diagnostics_ ? *diagnostics_ : emptyDiagnostics();
130 }
131
141 const Diagnostic* primaryDiagnostic() const noexcept { return hasDiagnostics() ? &diagnostics_->front() : nullptr; }
142
147 std::string describe() const;
148
149private:
150 static StatusCode statusCodeFor(DiagnosticCode code) noexcept {
151 switch (code) {
152 case DiagnosticCode::InvalidArgument:
153 case DiagnosticCode::PreconditionViolation: return StatusCode::Rejected;
154 case DiagnosticCode::NotFound: return StatusCode::NotFound;
155 case DiagnosticCode::AlreadyExists:
156 case DiagnosticCode::Conflict: return StatusCode::Conflict;
157 case DiagnosticCode::Unsupported: return StatusCode::Unsupported;
158 case DiagnosticCode::Cancelled: return StatusCode::Cancelled;
159 case DiagnosticCode::StaleHandle: return StatusCode::Rejected;
160 case DiagnosticCode::TypeMismatch: return StatusCode::Rejected;
161 case DiagnosticCode::ParseError:
162 case DiagnosticCode::SerializationError:
163 case DiagnosticCode::InvariantViolation:
164 case DiagnosticCode::Failed:
165 case DiagnosticCode::HashMismatch:
166 case DiagnosticCode::UnknownVersion:
167 case DiagnosticCode::CallbackFailure:
168 case DiagnosticCode::DialogueNotWaitingForChoice:
169 case DiagnosticCode::DialogueNotWaitingForCommand:
170 case DiagnosticCode::DialogueConditionRejected:
171 case DiagnosticCode::ProcgenGroupDataInvalid:
172 case DiagnosticCode::None: return StatusCode::Failed;
173 case DiagnosticCode::DialogueRouteNotFound: return StatusCode::NotFound;
174 }
175 return StatusCode::Failed;
176 }
177
178 StatusCode code_ = StatusCode::Ok;
179 std::shared_ptr<const std::vector<Diagnostic>> diagnostics_;
180 static const std::vector<Diagnostic>& emptyDiagnostics() noexcept;
181};
182
183} // namespace eve
Stable, structured diagnostics shared by engine modules.
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
DiagnosticCode code
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
Structured status and zero or more diagnostics for an operation.
Definition Status.h:70
Status() noexcept
Construct a successful Ok status with no diagnostics.
bool isSuccess() const noexcept
Whether the operation completed without a failure outcome.
Definition Status.h:105
const Diagnostic * primaryDiagnostic() const noexcept
First diagnostic, or null when no diagnostic was supplied.
Definition Status.h:141
const std::vector< Diagnostic > & diagnostics() const noexcept
All diagnostics in stable insertion order.
Definition Status.h:128
bool isFailure() const noexcept
Whether the operation has a failure outcome.
Definition Status.h:122
bool hasDiagnostics() const noexcept
True when this status has at least one diagnostic.
Definition Status.h:125
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
std::ostream & operator<<(std::ostream &stream, Severity severity)
Writes a severity for diagnostics and test output.
Definition Diagnostic.cpp:7
StatusCode
Stable outcome category for an operation.
Definition Status.h:28
constexpr std::string_view statusCodeName(StatusCode code) noexcept
Return the stable protocol spelling of a status code.
Definition Status.h:46