From 75283c35f62ff3b1bc9c579e0fe865ed90ea36d1 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Tue, 26 Dec 2023 22:39:08 +0700 Subject: [PATCH] feat: add `nil` function --- include/errors/error.hpp | 8 ++++++++ src/error.cpp | 5 +++++ test/error_test.cpp | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/errors/error.hpp b/include/errors/error.hpp index c664762..4b58ed5 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -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 @@ -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 diff --git a/src/error.cpp b/src/error.cpp index 40d756f..5003c86 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -23,4 +23,9 @@ Error make(const std::string& msg) { return Error(std::make_shared(msg)); } +const Error& nil() { + static const Error err(nullptr); + return err; +} + } // namespace error diff --git a/test/error_test.cpp b/test/error_test.cpp index 6b8b1a4..c544fc3 100644 --- a/test/error_test.cpp +++ b/test/error_test.cpp @@ -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"); }