11 #include <hilti/rt/exception.h> 20 Error(std::string description =
"<no description>", std::string context =
"")
21 : _description(std::move(description)), _context(std::move(context)) {}
22 const auto& description()
const {
return _description; }
23 const auto& context()
const {
return _context; }
24 operator std::string()
const {
return _description; }
25 operator std::string_view()
const {
return _description; }
28 std::string _description;
32 inline std::ostream& operator<<(std::ostream& out,
const Error& error) {
33 out << error.description();
37 inline bool operator==(
const Error& a,
const Error& b) {
return a.description() == b.description(); }
38 inline bool operator!=(
const Error& a,
const Error& b) {
return ! (a == b); }
43 NoResult(
Error err) : RuntimeError(err.description()), _error(std::move(err)) {}
52 NoError() : RuntimeError(
"<no error>") {}
59 inline bool operator==(
const Nothing&,
const Nothing&) {
return true; }
60 inline bool operator!=(
const Nothing&,
const Nothing&) {
return false; }
69 Result() : _value(std::in_place_type_t<result::Error>(),
result::Error(
"<result not initialized>")) {}
72 Result(
const T& t) : _value(
std::in_place_type_t<T>(), t) {}
90 const T&
value()
const {
return std::get<T>(_value); }
98 T&
value() {
return std::get<T>(_value); }
148 bool hasValue()
const {
return std::holds_alternative<T>(_value); }
155 const T*
operator->()
const {
return std::get_if<T>(&_value); }
160 explicit operator bool()
const {
return hasValue(); }
163 operator std::optional<T>()
const {
return hasValue() ? std::make_optional(value()) : std::nullopt; }
168 friend bool operator==(
const Result& a,
const Result& b) {
178 friend bool operator!=(
const Result& a,
const Result& b) {
return ! (a == b); }
181 std::variant<T, result::Error> _value;
const T & operator*() const
Definition: result.h:151
const T * operator->() const
Definition: result.h:155
const result::Error & error() const
Definition: result.h:132
Definition: optional.h:79
const T & valueOrThrow() const
Definition: result.h:106
T & valueOrThrow()
Definition: result.h:119
T * operator->()
Definition: result.h:157
Result(result::Error &&e)
Definition: result.h:78
bool hasValue() const
Definition: result.h:148
const result::Error & errorOrThrow() const
Definition: result.h:140
T & value()
Definition: result.h:98
Result(const T &t)
Definition: result.h:72
T & operator*()
Definition: result.h:153
const T & value() const
Definition: result.h:90
Result(T &&t)
Definition: result.h:74
Result(const result::Error &e)
Definition: result.h:76