Skip to content

Store Message as a Shared Constant String #80

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 5 commits into from
Dec 23, 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
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ API Docs

.. doxygenfunction:: errors::format

.. doxygenstruct:: errors::Error
.. doxygenclass:: errors::Error
:members:

License
Expand Down
21 changes: 19 additions & 2 deletions include/errors/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <fmt/core.h>

#include <memory>
#include <ostream>
#include <string>
#include <utility>
Expand All @@ -11,8 +12,24 @@ namespace errors {
/**
* @brief Represents error information.
*/
struct Error {
const std::string message; /**< The error message. */
class Error {
private:
const std::shared_ptr<const std::string> message_ptr;

public:
Error(const std::shared_ptr<const std::string>& message_ptr);

/**
* @brief Returns the error message.
*
* @code{.cpp}
* const auto err = errors::make("unknown error");
*
* // Print "unknown error"
* std::cout << err << std::endl;
* @endcode
*/
std::string message() const;

/**
* @brief Writes the string representation of an error object to the given
Expand Down
19 changes: 14 additions & 5 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@

namespace errors {

Error::Error(const std::shared_ptr<const std::string>& message_ptr) : message_ptr(message_ptr) {}

std::string Error::message() const {
if (!message_ptr) return "no error";
return *message_ptr;
}

std::ostream& operator<<(std::ostream& os, const errors::Error& err) {
return os << "error: " << err.message;
return os << "error: " << err.message();
}

bool operator==(const Error& lhs, const Error& rhs) {
return lhs.message == rhs.message;
return lhs.message() == rhs.message();
}

bool operator!=(const Error& lhs, const Error& rhs) {
return lhs.message != rhs.message;
return lhs.message() != rhs.message();
}

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

} // namespace error

Expand All @@ -27,7 +36,7 @@ format_parse_context::iterator formatter<errors::Error>::parse(

format_context::iterator formatter<errors::Error>::format(
const errors::Error& err, format_context& ctx) const {
return format_to(ctx.out(), "error: {}", err.message);
return format_to(ctx.out(), "error: {}", err.message());
}

} // namespace fmt
4 changes: 2 additions & 2 deletions test/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

TEST_CASE("Error Construction") {
const errors::Error err = errors::make("unknown error");
REQUIRE(err.message == "unknown error");
REQUIRE(err.message() == "unknown error");
}

TEST_CASE("Error Construction With Formatting") {
const errors::Error err = errors::format("HTTP error {}", 404);
REQUIRE(err.message == "HTTP error 404");
REQUIRE(err.message() == "HTTP error 404");
}

TEST_CASE("Error Comparison") {
Expand Down