Skip to content

Commit 7b9e05a

Browse files
authored
feat: store message as a shared constant string (#80)
* feat: modify `Error` from struct to class * feat: store `message` as a `const std::shared_ptr<const std::string>` * feat: add `Error::message` function for getting string from the shared pointer * feat: add `Error::Error(const std::shared_ptr<const std::string>&)` constructor * feat: modify `Error::message_ptr` to be private
1 parent ddd776b commit 7b9e05a

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ API Docs
1212

1313
.. doxygenfunction:: errors::format
1414

15-
.. doxygenstruct:: errors::Error
15+
.. doxygenclass:: errors::Error
1616
:members:
1717

1818
License

include/errors/error.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <fmt/core.h>
44

5+
#include <memory>
56
#include <ostream>
67
#include <string>
78
#include <utility>
@@ -11,8 +12,24 @@ namespace errors {
1112
/**
1213
* @brief Represents error information.
1314
*/
14-
struct Error {
15-
const std::string message; /**< The error message. */
15+
class Error {
16+
private:
17+
const std::shared_ptr<const std::string> message_ptr;
18+
19+
public:
20+
Error(const std::shared_ptr<const std::string>& message_ptr);
21+
22+
/**
23+
* @brief Returns the error message.
24+
*
25+
* @code{.cpp}
26+
* const auto err = errors::make("unknown error");
27+
*
28+
* // Print "unknown error"
29+
* std::cout << err << std::endl;
30+
* @endcode
31+
*/
32+
std::string message() const;
1633

1734
/**
1835
* @brief Writes the string representation of an error object to the given

src/error.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
namespace errors {
44

5+
Error::Error(const std::shared_ptr<const std::string>& message_ptr) : message_ptr(message_ptr) {}
6+
7+
std::string Error::message() const {
8+
if (!message_ptr) return "no error";
9+
return *message_ptr;
10+
}
11+
512
std::ostream& operator<<(std::ostream& os, const errors::Error& err) {
6-
return os << "error: " << err.message;
13+
return os << "error: " << err.message();
714
}
815

916
bool operator==(const Error& lhs, const Error& rhs) {
10-
return lhs.message == rhs.message;
17+
return lhs.message() == rhs.message();
1118
}
1219

1320
bool operator!=(const Error& lhs, const Error& rhs) {
14-
return lhs.message != rhs.message;
21+
return lhs.message() != rhs.message();
1522
}
1623

17-
Error make(const std::string& msg) { return Error{.message = msg}; }
24+
Error make(const std::string& msg) {
25+
return Error(std::make_shared<const std::string>(msg));
26+
}
1827

1928
} // namespace error
2029

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

2837
format_context::iterator formatter<errors::Error>::format(
2938
const errors::Error& err, format_context& ctx) const {
30-
return format_to(ctx.out(), "error: {}", err.message);
39+
return format_to(ctx.out(), "error: {}", err.message());
3140
}
3241

3342
} // namespace fmt

test/error_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
TEST_CASE("Error Construction") {
99
const errors::Error err = errors::make("unknown error");
10-
REQUIRE(err.message == "unknown error");
10+
REQUIRE(err.message() == "unknown error");
1111
}
1212

1313
TEST_CASE("Error Construction With Formatting") {
1414
const errors::Error err = errors::format("HTTP error {}", 404);
15-
REQUIRE(err.message == "HTTP error 404");
15+
REQUIRE(err.message() == "HTTP error 404");
1616
}
1717

1818
TEST_CASE("Error Comparison") {

0 commit comments

Comments
 (0)