2020-07-28 01:23:38 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
namespace gin {
|
2020-08-09 02:03:09 +02:00
|
|
|
class Error {
|
2020-08-09 02:09:49 +02:00
|
|
|
private:
|
|
|
|
std::string error_message;
|
|
|
|
int8_t error_;
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
public:
|
|
|
|
Error();
|
|
|
|
Error(const std::string &msg);
|
|
|
|
Error(const std::string &msg, int8_t code);
|
|
|
|
Error(const Error &error);
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
const std::string &message() const;
|
|
|
|
bool failed() const;
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
bool isCritical() const;
|
|
|
|
bool isRecoverable() const;
|
2020-08-09 02:03:09 +02:00
|
|
|
};
|
|
|
|
|
2020-08-09 02:04:48 +02:00
|
|
|
Error criticalError(const std::string &msg);
|
|
|
|
Error recoverableError(const std::string &msg);
|
2020-08-09 02:03:09 +02:00
|
|
|
Error noError();
|
|
|
|
|
|
|
|
class ErrorOrValue {
|
2020-08-09 02:09:49 +02:00
|
|
|
public:
|
|
|
|
virtual ~ErrorOrValue() = default;
|
2020-08-09 02:03:09 +02:00
|
|
|
};
|
|
|
|
|
2020-08-09 02:04:48 +02:00
|
|
|
template <typename T> class ErrorOr : public ErrorOrValue {
|
2020-08-09 02:09:49 +02:00
|
|
|
private:
|
|
|
|
std::variant<T, Error> value_or_error;
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
public:
|
|
|
|
ErrorOr(const T &value) : value_or_error{value} {}
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
ErrorOr(T &&value) : value_or_error{std::move(value)} {}
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
ErrorOr(const Error &error) : value_or_error{error} {}
|
|
|
|
ErrorOr(Error &&error) : value_or_error{std::move(error)} {}
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
bool isValue() const { return std::holds_alternative<T>(value_or_error); }
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
bool isError() const {
|
|
|
|
return std::holds_alternative<Error>(value_or_error);
|
|
|
|
}
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
const Error &error() const { return std::get<Error>(value_or_error); }
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
T &value() { return std::get<T>(value_or_error); }
|
2020-08-09 02:03:09 +02:00
|
|
|
|
2020-08-09 02:09:49 +02:00
|
|
|
const T &value() const { return std::get<T>(value_or_error); }
|
2020-08-09 02:03:09 +02:00
|
|
|
};
|
2020-08-09 02:04:48 +02:00
|
|
|
} // namespace gin
|