Skip to content

Commit

Permalink
feat: add nil function
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Dec 26, 2023
1 parent 00c5dcd commit 75283c3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/errors/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Error {
explicit operator bool() const;

friend Error make(const std::string& msg);
friend const Error& nil();

/**
* @brief Writes the string representation of an error object to the given
Expand Down Expand Up @@ -83,4 +84,11 @@ class Error {
*/
Error make(const std::string& msg);


/**
* @brief Gets a constant reference of an empty error.
* @return A constant reference of an empty error.
*/
const Error& nil();

} // namespace error
5 changes: 5 additions & 0 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ Error make(const std::string& msg) {
return Error(std::make_shared<const std::string>(msg));
}

const Error& nil() {
static const Error err(nullptr);
return err;
}

} // namespace error
2 changes: 1 addition & 1 deletion test/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TEST_CASE("Error Construction") {
}

TEST_CASE("Empty Error Construction") {
const errors::Error err;
const auto err = errors::nil();
REQUIRE_FALSE(err);
REQUIRE(err.message() == "no error");
}
Expand Down

0 comments on commit 75283c3

Please # to comment.