Skip to content

Commit a43072b

Browse files
authored
chore: merge pull request #39 from threeal/error-friend-operators
Error: Declare Operators as Friend in `Error` Struct
2 parents bfc7457 + 721b1d5 commit a43072b

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

docs/error/index.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ API Docs
1313
.. doxygenstruct:: error::Error
1414
:members:
1515

16-
.. doxygenfunction:: error::operator==
17-
18-
.. doxygenfunction:: error::operator!=
19-
2016
License
2117
-------
2218

error/.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
BasedOnStyle: Google
2+
ColumnLimit: 0

error/include/error/error.hpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ struct Error {
3333
* @endcode
3434
*/
3535
friend std::ostream& operator<<(std::ostream& os, const error::Error& err);
36+
37+
/**
38+
* @brief Checks if two error objects are equal.
39+
* @param lhs The left-hand side error object.
40+
* @param rhs The right-hand side error object.
41+
* @return True if equal, false otherwise.
42+
*
43+
* This operator allows the comparison of two error objects using the == operator.
44+
*
45+
* @code{.cpp}
46+
* const auto err = error::make("unknown error");
47+
* const auto other_err = err;
48+
*
49+
* assert(err == other_err);
50+
* @endcode
51+
*/
52+
friend bool operator==(const Error& lhs, const Error& rhs);
53+
54+
/**
55+
* @brief Checks if two error objects are not equal.
56+
* @param lhs The left-hand side error object.
57+
* @param rhs The right-hand side error object.
58+
* @return True if not equal, false otherwise.
59+
*
60+
* This operator allows the comparison of two error objects using the != operator.
61+
*
62+
* @code{.cpp}
63+
* const auto err = error::make("unknown error");
64+
* const auto other_err = error::make("other error");
65+
*
66+
* assert(err != other_err);
67+
* @endcode
68+
*/
69+
friend bool operator!=(const Error& lhs, const Error& rhs);
3670
};
3771

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

57-
/**
58-
* @brief Checks if two error objects are equal.
59-
* @param lhs The left-hand side error object.
60-
* @param rhs The right-hand side error object.
61-
* @return True if equal, false otherwise.
62-
*/
63-
bool operator==(const Error& lhs, const Error& rhs);
64-
65-
/**
66-
* @brief Checks if two error objects are not equal.
67-
* @param lhs The left-hand side error object.
68-
* @param rhs The right-hand side error object.
69-
* @return True if not equal, false otherwise.
70-
*/
71-
bool operator!=(const Error& lhs, const Error& rhs);
72-
7391
} // namespace error
7492

7593
template <>

error/src/error.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ std::ostream& operator<<(std::ostream& os, const error::Error& err) {
66
return os << "error: " << err.message;
77
}
88

9-
Error make(const std::string& msg) { return Error{.message = msg}; }
10-
119
bool operator==(const Error& lhs, const Error& rhs) {
1210
return lhs.message == rhs.message;
1311
}
@@ -16,6 +14,8 @@ bool operator!=(const Error& lhs, const Error& rhs) {
1614
return lhs.message != rhs.message;
1715
}
1816

17+
Error make(const std::string& msg) { return Error{.message = msg}; }
18+
1919
} // namespace error
2020

2121
namespace fmt {

0 commit comments

Comments
 (0)