duvc-ctl 2.0.0
USB Video Class Camera Control Library
Loading...
Searching...
No Matches
result.h
Go to the documentation of this file.
1#pragma once
2
8#include <iostream>
9#include <optional>
10#include <string>
11#include <system_error>
12#include <variant>
13
14namespace duvc {
15
30
36const char *to_string(ErrorCode code);
37
41class Error {
42public:
48 Error(ErrorCode code, std::string message = "");
49
55 Error(std::error_code code, std::string message = "");
56
58 ErrorCode code() const { return code_; }
59
61 const std::string &message() const { return message_; }
62
64 std::string description() const;
65
66private:
67 ErrorCode code_;
68 std::string message_;
69};
70
75template <typename T> class Result {
76public:
81 Result(T value) : data_(std::move(value)) {}
82
87 Result(Error error) : data_(std::move(error)) {}
88
94 Result(ErrorCode code, std::string message = "")
95 : data_(Error(code, std::move(message))) {}
96
101 bool is_ok() const { return std::holds_alternative<T>(data_); }
102
107 bool is_error() const { return std::holds_alternative<Error>(data_); }
108
114 const T &value() const & { return std::get<T>(data_); }
115
121 T &&value() && { return std::get<T>(std::move(data_)); }
122
128 const Error &error() const { return std::get<Error>(data_); }
129
135 T value_or(const T &default_value) const & {
136 return is_ok() ? value() : default_value;
137 }
138
145 return is_ok() ? std::move(*this).value() : std::move(default_value);
146 }
147
151 explicit operator bool() const { return is_ok(); }
152
153private:
154 std::variant<T, Error> data_;
155};
156
160template <> class Result<void> {
161public:
165 Result() : error_(std::nullopt) {}
166
171 Result(Error error) : error_(std::move(error)) {}
172
178 Result(ErrorCode code, std::string message = "")
179 : error_(Error(code, std::move(message))) {}
180
185 bool is_ok() const { return !error_.has_value(); }
186
191 bool is_error() const { return error_.has_value(); }
192
197 const Error &error() const { return *error_; }
198
202 explicit operator bool() const { return is_ok(); }
203
204private:
205 std::optional<Error> error_;
206};
207
214template <typename T> Result<T> Ok(T value) {
215 return Result<T>(std::move(value));
216}
217
222inline Result<void> Ok() { return Result<void>(); }
223
230template <typename T> Result<T> Err(Error error) {
231 return Result<T>(std::move(error));
232}
233
241template <typename T> Result<T> Err(ErrorCode code, std::string message = "") {
242 return Result<T>(code, std::move(message));
243}
244
245} // namespace duvc
Error information with context.
Definition result.h:41
const std::string & message() const
Get error message.
Definition result.h:61
ErrorCode code() const
Get error code.
Definition result.h:58
std::string description() const
Get full error description.
Definition result.cpp:47
bool is_ok() const
Check if result is success.
Definition result.h:185
Result(ErrorCode code, std::string message="")
Create error result from error code.
Definition result.h:178
bool is_error() const
Check if result is error.
Definition result.h:191
Result(Error error)
Create error result.
Definition result.h:171
Result()
Create successful void result.
Definition result.h:165
const Error & error() const
Get the error (assumes error)
Definition result.h:197
Result type that can contain either a value or an error.
Definition result.h:75
const T & value() const &
Get the value (assumes success)
Definition result.h:114
T value_or(const T &default_value) const &
Get value or default if error.
Definition result.h:135
T && value() &&
Get the value (assumes success)
Definition result.h:121
bool is_ok() const
Check if result contains a value (success)
Definition result.h:101
Result(ErrorCode code, std::string message="")
Create error result from error code.
Definition result.h:94
Result(T value)
Create successful result with value.
Definition result.h:81
bool is_error() const
Check if result contains an error.
Definition result.h:107
const Error & error() const
Get the error (assumes error)
Definition result.h:128
T value_or(T &&default_value) &&
Get value or default if error.
Definition result.h:144
Result(Error error)
Create error result.
Definition result.h:87
Definition core.h:13
ErrorCode
Error codes for duvc operations.
Definition result.h:19
@ InvalidValue
Property value out of range.
@ InvalidArgument
Invalid function argument.
@ DeviceBusy
Device is busy or in use.
@ PermissionDenied
Insufficient permissions.
@ Success
Operation succeeded.
@ PropertyNotSupported
Property not supported by device.
@ NotImplemented
Feature not implemented on this platform.
@ SystemError
System/platform error.
@ DeviceNotFound
Device not found or disconnected.
@ Error
Error messages.
const char * to_string(CamProp)
Convert camera property enum to string.
Definition core.cpp:678
Result< T > Err(Error error)
Helper to create error Result.
Definition result.h:230
Result< void > Ok()
Helper to create successful void Result.
Definition result.h:222