Skip to content

Error: Declare Operators as Friend in Error Struct #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/error/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ API Docs
.. doxygenstruct:: error::Error
:members:

.. doxygenfunction:: error::operator==

.. doxygenfunction:: error::operator!=

License
-------

Expand Down
1 change: 1 addition & 0 deletions error/.clang-format
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
BasedOnStyle: Google
ColumnLimit: 0
50 changes: 34 additions & 16 deletions error/include/error/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@ struct Error {
* @endcode
*/
friend std::ostream& operator<<(std::ostream& os, const error::Error& err);

/**
* @brief Checks if two error objects are equal.
* @param lhs The left-hand side error object.
* @param rhs The right-hand side error object.
* @return True if equal, false otherwise.
*
* This operator allows the comparison of two error objects using the == operator.
*
* @code{.cpp}
* const auto err = error::make("unknown error");
* const auto other_err = err;
*
* assert(err == other_err);
* @endcode
*/
friend bool operator==(const Error& lhs, const Error& rhs);

/**
* @brief Checks if two error objects are not equal.
* @param lhs The left-hand side error object.
* @param rhs The right-hand side error object.
* @return True if not equal, false otherwise.
*
* This operator allows the comparison of two error objects using the != operator.
*
* @code{.cpp}
* const auto err = error::make("unknown error");
* const auto other_err = error::make("other error");
*
* assert(err != other_err);
* @endcode
*/
friend bool operator!=(const Error& lhs, const Error& rhs);
};

/**
Expand All @@ -54,22 +88,6 @@ Error format(fmt::format_string<T...> fmt, T&&... args) {
return error::make(fmt::format(fmt, std::forward<T>(args)...));
}

/**
* @brief Checks if two error objects are equal.
* @param lhs The left-hand side error object.
* @param rhs The right-hand side error object.
* @return True if equal, false otherwise.
*/
bool operator==(const Error& lhs, const Error& rhs);

/**
* @brief Checks if two error objects are not equal.
* @param lhs The left-hand side error object.
* @param rhs The right-hand side error object.
* @return True if not equal, false otherwise.
*/
bool operator!=(const Error& lhs, const Error& rhs);

} // namespace error

template <>
Expand Down
4 changes: 2 additions & 2 deletions error/src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ std::ostream& operator<<(std::ostream& os, const error::Error& err) {
return os << "error: " << err.message;
}

Error make(const std::string& msg) { return Error{.message = msg}; }

bool operator==(const Error& lhs, const Error& rhs) {
return lhs.message == rhs.message;
}
Expand All @@ -16,6 +14,8 @@ bool operator!=(const Error& lhs, const Error& rhs) {
return lhs.message != rhs.message;
}

Error make(const std::string& msg) { return Error{.message = msg}; }

} // namespace error

namespace fmt {
Expand Down