contur2
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <cassert>
12#include <cstdint>
13#include <string_view>
14#include <utility>
15#include <variant>
16
17namespace contur {
18
44
46 [[nodiscard]] constexpr std::string_view errorCodeToString(ErrorCode code) noexcept
47 {
48 switch (code)
49 {
50 case ErrorCode::Ok:
51 return "Ok";
53 return "OutOfMemory";
55 return "InvalidPid";
57 return "InvalidAddress";
59 return "DivisionByZero";
61 return "InvalidInstruction";
63 return "SegmentationFault";
65 return "DeviceError";
67 return "ProcessNotFound";
69 return "PermissionDenied";
71 return "Timeout";
73 return "DeadlockDetected";
75 return "InvalidState";
77 return "InvalidArgument";
79 return "ResourceBusy";
81 return "NotFound";
83 return "AlreadyExists";
85 return "BufferFull";
87 return "BufferEmpty";
89 return "EndOfFile";
91 return "NotImplemented";
92 }
93 return "Unknown";
94 }
95
103 template <typename T> class [[nodiscard]] Result
104 {
105 public:
107 [[nodiscard]] static Result ok(T value)
108 {
109 return Result(std::move(value));
110 }
111
113 [[nodiscard]] static Result error(ErrorCode code)
114 {
115 assert(code != ErrorCode::Ok && "Cannot create error Result with Ok code");
116 return Result(code);
117 }
118
120 [[nodiscard]] bool isOk() const noexcept
121 {
122 return std::holds_alternative<T>(storage_);
123 }
124
126 [[nodiscard]] bool isError() const noexcept
127 {
128 return std::holds_alternative<ErrorCode>(storage_);
129 }
130
133 [[nodiscard]] const T &value() const &
134 {
135 assert(isOk() && "Accessing value of error Result");
136 return std::get<T>(storage_);
137 }
138
141 [[nodiscard]] T &&value() &&
142 {
143 assert(isOk() && "Accessing value of error Result");
144 return std::get<T>(std::move(storage_));
145 }
146
149 [[nodiscard]] T &value() &
150 {
151 assert(isOk() && "Accessing value of error Result");
152 return std::get<T>(storage_);
153 }
154
157 [[nodiscard]] ErrorCode errorCode() const noexcept
158 {
159 assert(isError() && "Accessing errorCode of ok Result");
160 return std::get<ErrorCode>(storage_);
161 }
162
164 [[nodiscard]] T valueOr(T defaultValue) const &
165 {
166 if (isOk())
167 {
168 return std::get<T>(storage_);
169 }
170 return defaultValue;
171 }
172
173 private:
174 explicit Result(T value)
175 : storage_(std::move(value))
176 {}
177 explicit Result(ErrorCode code)
178 : storage_(code)
179 {}
180
181 std::variant<T, ErrorCode> storage_;
182 };
183
185 template <> class [[nodiscard]] Result<void>
186 {
187 public:
189 [[nodiscard]] static Result ok()
190 {
191 return Result(ErrorCode::Ok);
192 }
193
195 [[nodiscard]] static Result error(ErrorCode code)
196 {
197 assert(code != ErrorCode::Ok && "Cannot create error Result with Ok code");
198 return Result(code);
199 }
200
202 [[nodiscard]] bool isOk() const noexcept
203 {
204 return code_ == ErrorCode::Ok;
205 }
206
208 [[nodiscard]] bool isError() const noexcept
209 {
210 return code_ != ErrorCode::Ok;
211 }
212
214 [[nodiscard]] ErrorCode errorCode() const noexcept
215 {
216 return code_;
217 }
218
219 private:
220 explicit Result(ErrorCode code)
221 : code_(code)
222 {}
223
225 };
226
227} // namespace contur
bool isError() const noexcept
Returns true if this Result represents failure.
Definition error.h:208
ErrorCode errorCode() const noexcept
Returns the error code (Ok if successful).
Definition error.h:214
bool isOk() const noexcept
Returns true if this Result represents success.
Definition error.h:202
Result(ErrorCode code)
Definition error.h:220
static Result ok()
Constructs a successful void Result.
Definition error.h:189
static Result error(ErrorCode code)
Constructs a failed void Result with the given error code.
Definition error.h:195
ErrorCode errorCode() const noexcept
Returns the error code.
Definition error.h:157
const T & value() const &
Returns a const reference to the success value.
Definition error.h:133
bool isOk() const noexcept
Returns true if this Result holds a success value.
Definition error.h:120
std::variant< T, ErrorCode > storage_
Definition error.h:181
Result(T value)
Definition error.h:174
bool isError() const noexcept
Returns true if this Result holds an error code.
Definition error.h:126
static Result error(ErrorCode code)
Constructs a failed Result with the given error code.
Definition error.h:113
static Result ok(T value)
Constructs a successful Result containing the given value.
Definition error.h:107
T & value() &
Returns a mutable reference to the success value.
Definition error.h:149
Result(ErrorCode code)
Definition error.h:177
T valueOr(T defaultValue) const &
Returns the value if ok, or the provided default value if error.
Definition error.h:164
T && value() &&
Returns an rvalue reference to the success value (move semantics).
Definition error.h:141
Definition block.h:15
@ Ok
No interrupt — normal execution.
Definition interrupt.h:18
ErrorCode
Error codes returned by kernel subsystem operations.
Definition error.h:21
constexpr std::string_view errorCodeToString(ErrorCode code) noexcept
Returns a human-readable name for the given error code.
Definition error.h:46