Skip to content

Commit

Permalink
feat: better Expected
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jun 25, 2023
1 parent f1f8ee0 commit 5b2fa54
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 153 deletions.
47 changes: 31 additions & 16 deletions include/mrdox/Support/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace mrdox {
/** Holds the description of an error, or success.
*/
class [[nodiscard]] MRDOX_DECL
Error final : public std::exception
Error final
{
std::string message_;
std::string reason_;
Expand All @@ -47,19 +47,19 @@ class [[nodiscard]] MRDOX_DECL
A default constructed error is
equivalent to success.
*/
Error() = default;
Error() noexcept = default;

/** Constructor.
*/
Error(Error&&) = default;
Error(Error&&) noexcept = default;

/** Constructor.
*/
Error(Error const&) = default;

/** Constructor.
*/
Error& operator=(Error&&) = default;
Error& operator=(Error&&) noexcept = default;

/** Assignment.
*/
Expand Down Expand Up @@ -110,64 +110,79 @@ class [[nodiscard]] MRDOX_DECL

/** Return true if this holds an error.
*/
bool failed() const noexcept
constexpr bool
failed() const noexcept
{
return ! message_.empty();
}

/** Return true if this holds an error.
*/
explicit
constexpr explicit
operator bool() const noexcept
{
return failed();
}

/** Return the error string.
*/
std::string_view
constexpr std::string_view
message() const noexcept
{
return message_;
}

/** Return the reason string.
*/
std::string_view
constexpr std::string_view
reason() const noexcept
{
return reason_;
}

/** Return the source location.
*/
std::source_location
constexpr std::source_location
location() const noexcept
{
return loc_;
}

/** Return true if this equals other.
/** Return true if this equals rhs.
*/
bool
operator==(Error const& other) const noexcept
constexpr bool
operator==(Error const& rhs) const noexcept
{
return message_ == other.message_;
return message_ == rhs.message_;
}

#if 0
/** Return a null-terminated error string.
*/
char const*
what() const noexcept override
{
return reason_.c_str();
}
#endif

constexpr void swap(Error& rhs) noexcept
{
using std::swap;
swap(message_, rhs.message_);
swap(reason_, rhs.reason_);
swap(loc_, rhs.loc_);
}

friend constexpr void swap(
Error& lhs, Error& rhs) noexcept
{
lhs.swap(rhs);
}

/** Return a value indicating success.
*/
static
Error
success() noexcept;
static Error success() noexcept;
};

inline
Expand Down
Loading

0 comments on commit 5b2fa54

Please sign in to comment.