mirror of
https://github.com/badaix/snapcast.git
synced 2025-04-28 17:57:05 +02:00
Add ErrorOr class
This commit is contained in:
parent
4540e64224
commit
964801896a
1 changed files with 82 additions and 0 deletions
|
@ -22,25 +22,39 @@
|
|||
#include <optional>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <variant>
|
||||
|
||||
|
||||
namespace snapcast
|
||||
{
|
||||
|
||||
/// Error codes with detail information
|
||||
struct ErrorCode : public std::error_code
|
||||
{
|
||||
/// c'tor
|
||||
ErrorCode() : std::error_code(), detail_(std::nullopt)
|
||||
{
|
||||
}
|
||||
|
||||
/// c'tor
|
||||
/// @param code the std error code
|
||||
ErrorCode(const std::error_code& code) : std::error_code(code), detail_(std::nullopt)
|
||||
{
|
||||
}
|
||||
|
||||
/// c'tor
|
||||
/// @param code the std error code
|
||||
/// @param detail additional details
|
||||
ErrorCode(const std::error_code& code, std::string detail) : std::error_code(code), detail_(std::move(detail))
|
||||
{
|
||||
}
|
||||
|
||||
/// Move c'tor
|
||||
ErrorCode(ErrorCode&& other) = default;
|
||||
/// Move assignment
|
||||
ErrorCode& operator=(ErrorCode&& rhs) = default;
|
||||
|
||||
/// @return detaiöed error message
|
||||
std::string detailed_message() const
|
||||
{
|
||||
if (detail_.has_value())
|
||||
|
@ -49,8 +63,76 @@ struct ErrorCode : public std::error_code
|
|||
}
|
||||
|
||||
private:
|
||||
/// Optional error detais
|
||||
std::optional<std::string> detail_;
|
||||
};
|
||||
|
||||
|
||||
/// Storage for an ErrorCode or a type T
|
||||
/// Mostly used as return type
|
||||
template <class T>
|
||||
struct ErrorOr
|
||||
{
|
||||
/// Move construct with @p t
|
||||
ErrorOr(T&& t) : var(std::move(t))
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with @p t
|
||||
ErrorOr(const T& t) : var(t)
|
||||
{
|
||||
}
|
||||
|
||||
/// Move construct with an error
|
||||
ErrorOr(ErrorCode&& error) : var(std::move(error))
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with an error
|
||||
ErrorOr(const ErrorCode& error) : var(error)
|
||||
{
|
||||
}
|
||||
|
||||
/// @return true if contains a value (i.e. no error)
|
||||
bool hasValue()
|
||||
{
|
||||
return std::holds_alternative<T>(var);
|
||||
}
|
||||
|
||||
/// @return true if contains an error (i.e. no value)
|
||||
bool hasError()
|
||||
{
|
||||
return std::holds_alternative<ErrorCode>(var);
|
||||
}
|
||||
|
||||
/// @return the value
|
||||
const T& getValue()
|
||||
{
|
||||
return std::get<T>(var);
|
||||
}
|
||||
|
||||
/// @return the moved value
|
||||
T takeValue()
|
||||
{
|
||||
return std::move(std::get<T>(var));
|
||||
}
|
||||
|
||||
/// @return the error
|
||||
const ErrorCode& getError()
|
||||
{
|
||||
return std::get<ErrorCode>(var);
|
||||
}
|
||||
|
||||
/// @return the moved error
|
||||
ErrorCode takeError()
|
||||
{
|
||||
return std::move(std::get<ErrorCode>(var));
|
||||
}
|
||||
|
||||
private:
|
||||
/// The stored ErrorCode or value
|
||||
std::variant<ErrorCode, T> var;
|
||||
};
|
||||
|
||||
|
||||
} // namespace snapcast
|
||||
|
|
Loading…
Add table
Reference in a new issue