From 0405bae9844b9121379d0683a8a11a5d2434985e Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 27 Dec 2023 16:29:15 +0700 Subject: [PATCH 1/4] feat: use `const char*` for the `msg` parameter in the `make` function --- components/format/include/errors/format.ipp | 2 +- include/errors/error.hpp | 4 ++-- src/error.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/format/include/errors/format.ipp b/components/format/include/errors/format.ipp index 4b0401f..bbe1a5a 100644 --- a/components/format/include/errors/format.ipp +++ b/components/format/include/errors/format.ipp @@ -2,7 +2,7 @@ namespace errors { template Error format(fmt::format_string fmt, T&&... args) { - return errors::make(fmt::format(fmt, std::forward(args)...)); + return errors::make(fmt::format(fmt, std::forward(args)...).c_str()); } } // namespace errors diff --git a/include/errors/error.hpp b/include/errors/error.hpp index b1212fa..82cb0e3 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -47,7 +47,7 @@ class Error { */ explicit operator bool() const; - friend Error make(const std::string& msg); + friend Error make(const char* msg); friend const Error& nil(); /** @@ -76,7 +76,7 @@ class Error { * @param msg The error message. * @return A new error object. */ -Error make(const std::string& msg); +Error make(const char* msg); /** diff --git a/src/error.cpp b/src/error.cpp index f702810..772d451 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -17,7 +17,7 @@ std::ostream& operator<<(std::ostream& os, const errors::Error& err) { return os << "error: " << err.message(); } -Error make(const std::string& msg) { +Error make(const char* msg) { return Error(std::make_shared(msg)); } From d28cfd3e7332081b6b64d4c55d7b27fd77721be1 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 27 Dec 2023 16:43:49 +0700 Subject: [PATCH 2/4] feat: store `Error::message` as `std::shared_ptr` --- include/errors/error.hpp | 4 ++-- src/error.cpp | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/errors/error.hpp b/include/errors/error.hpp index 82cb0e3..eee6079 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -13,9 +13,9 @@ namespace errors { */ class Error { private: - const std::shared_ptr message_ptr; + const std::shared_ptr message_ptr; - Error(const std::shared_ptr& message_ptr); + Error(const std::shared_ptr& message_ptr); public: /** diff --git a/src/error.cpp b/src/error.cpp index 772d451..2891827 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -1,12 +1,13 @@ +#include #include namespace errors { -Error::Error(const std::shared_ptr& message_ptr) : message_ptr(message_ptr) {} +Error::Error(const std::shared_ptr& message_ptr) : message_ptr(message_ptr) {} std::string_view Error::message() const { if (!message_ptr) return "no error"; - return *message_ptr; + return message_ptr.get(); } Error::operator bool() const { @@ -18,7 +19,9 @@ std::ostream& operator<<(std::ostream& os, const errors::Error& err) { } Error make(const char* msg) { - return Error(std::make_shared(msg)); + auto msg_copy = new char[std::strlen(msg)]; + std::strcpy(msg_copy, msg); + return Error(std::shared_ptr(msg_copy)); } const Error& nil() { From 3cfa12bf343c8838100647d25404946a0608263c Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 27 Dec 2023 16:56:27 +0700 Subject: [PATCH 3/4] feat: use `std::string_view` for the `msg` parameter in the `make` function --- components/format/include/errors/format.ipp | 2 +- include/errors/error.hpp | 4 ++-- src/error.cpp | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/components/format/include/errors/format.ipp b/components/format/include/errors/format.ipp index bbe1a5a..4b0401f 100644 --- a/components/format/include/errors/format.ipp +++ b/components/format/include/errors/format.ipp @@ -2,7 +2,7 @@ namespace errors { template Error format(fmt::format_string fmt, T&&... args) { - return errors::make(fmt::format(fmt, std::forward(args)...).c_str()); + return errors::make(fmt::format(fmt, std::forward(args)...)); } } // namespace errors diff --git a/include/errors/error.hpp b/include/errors/error.hpp index eee6079..4329295 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -47,7 +47,7 @@ class Error { */ explicit operator bool() const; - friend Error make(const char* msg); + friend Error make(std::string_view msg); friend const Error& nil(); /** @@ -76,7 +76,7 @@ class Error { * @param msg The error message. * @return A new error object. */ -Error make(const char* msg); +Error make(std::string_view msg); /** diff --git a/src/error.cpp b/src/error.cpp index 2891827..f0ef282 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace errors { @@ -18,10 +18,11 @@ std::ostream& operator<<(std::ostream& os, const errors::Error& err) { return os << "error: " << err.message(); } -Error make(const char* msg) { - auto msg_copy = new char[std::strlen(msg)]; - std::strcpy(msg_copy, msg); - return Error(std::shared_ptr(msg_copy)); +Error make(std::string_view msg) { + auto c_msg = new char[msg.size() + 1]; + msg.copy(c_msg, msg.size()); + c_msg[msg.size()] = 0; + return Error(std::shared_ptr(c_msg)); } const Error& nil() { From a27f04b3088fad3c505ab2072a962cfa5930141f Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 27 Dec 2023 16:57:55 +0700 Subject: [PATCH 4/4] style: rename `Error::message_ptr` to `Error::msg_ptr` --- include/errors/error.hpp | 4 ++-- src/error.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/errors/error.hpp b/include/errors/error.hpp index 4329295..c01f0e2 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -13,9 +13,9 @@ namespace errors { */ class Error { private: - const std::shared_ptr message_ptr; + const std::shared_ptr msg_ptr; - Error(const std::shared_ptr& message_ptr); + Error(const std::shared_ptr& msg_ptr); public: /** diff --git a/src/error.cpp b/src/error.cpp index f0ef282..23ad13f 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -3,15 +3,15 @@ namespace errors { -Error::Error(const std::shared_ptr& message_ptr) : message_ptr(message_ptr) {} +Error::Error(const std::shared_ptr& msg_ptr) : msg_ptr(msg_ptr) {} std::string_view Error::message() const { - if (!message_ptr) return "no error"; - return message_ptr.get(); + if (!msg_ptr) return "no error"; + return msg_ptr.get(); } Error::operator bool() const { - return (bool)message_ptr; + return (bool)msg_ptr; } std::ostream& operator<<(std::ostream& os, const errors::Error& err) {