diff --git a/include/errors/error.hpp b/include/errors/error.hpp index b1212fa..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: /** @@ -47,7 +47,7 @@ class Error { */ explicit operator bool() const; - friend Error make(const std::string& 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 std::string& msg); +Error make(std::string_view msg); /** diff --git a/src/error.cpp b/src/error.cpp index f702810..23ad13f 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -1,24 +1,28 @@ +#include #include 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; + 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) { return os << "error: " << err.message(); } -Error make(const std::string& msg) { - return Error(std::make_shared(msg)); +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() {