Skip to content

Commit

Permalink
Add Clone() function to Error class
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Dec 15, 2020
1 parent e656f12 commit 0f8be09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
16 changes: 12 additions & 4 deletions include/Pomdog/Utility/Errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class POMDOG_EXPORT Error {
public:
virtual ~Error() noexcept = default;

/// Returns a string representation of the error.
[[nodiscard]] virtual std::string ToString() const noexcept = 0;

/// Creates a new object that is a copy of the error.
[[nodiscard]] virtual std::unique_ptr<Error> Clone() const noexcept = 0;
};

} // namespace Pomdog
Expand All @@ -26,23 +30,27 @@ class POMDOG_EXPORT IOError final : public Error {
std::errc Kind;
std::string Reason;

/// Returns a string representation of the error.
[[nodiscard]] std::string ToString() const noexcept;

/// Creates a new object that is a copy of the error.
[[nodiscard]] std::unique_ptr<Error> Clone() const noexcept;
};

[[nodiscard]] POMDOG_EXPORT
std::shared_ptr<IOError>
New(std::errc kind, std::string&& reason);
New(std::errc kind, std::string&& reason) noexcept;

[[nodiscard]] POMDOG_EXPORT
std::shared_ptr<Error>
New(std::string&& message);
New(std::string&& message) noexcept;

[[nodiscard]] POMDOG_EXPORT
std::shared_ptr<Error>
Wrap(const std::shared_ptr<Error>& err, std::string&& message);
Wrap(const std::shared_ptr<Error>& err, std::string&& message) noexcept;

[[nodiscard]] POMDOG_EXPORT
std::shared_ptr<Error>
Wrap(std::shared_ptr<Error>&& err, std::string&& message);
Wrap(std::shared_ptr<Error>&& err, std::string&& message) noexcept;

} // namespace Pomdog::Errors
31 changes: 27 additions & 4 deletions src/Utility/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class StringError final : public Error {
{
return this->Message;
}

[[nodiscard]] std::unique_ptr<Error> Clone() const noexcept
{
auto err = std::make_unique<StringError>();
err->Message = this->Message;
return err;
}
};

class WrappedError final : public Error {
Expand All @@ -29,37 +36,53 @@ class WrappedError final : public Error {
}
return this->Message;
}

[[nodiscard]] std::unique_ptr<Error> Clone() const noexcept
{
auto err = std::make_unique<WrappedError>();
err->Err = this->Err->Clone();
err->Message = this->Message;
return err;
}
};

std::string IOError::ToString() const noexcept
{
return this->Reason + ": " + std::make_error_code(this->Kind).message();
}

std::shared_ptr<IOError> New(std::errc kind, std::string&& reason)
std::unique_ptr<Error> IOError::Clone() const noexcept
{
auto err = std::make_unique<IOError>();
err->Kind = this->Kind;
err->Reason = this->Reason;
return err;
}

std::shared_ptr<IOError> New(std::errc kind, std::string&& reason) noexcept
{
auto err = std::make_shared<IOError>();
err->Kind = kind;
err->Reason = std::move(reason);
return err;
}

std::shared_ptr<Error> New(std::string&& message)
std::shared_ptr<Error> New(std::string&& message) noexcept
{
auto err = std::make_shared<StringError>();
err->Message = std::move(message);
return std::move(err);
}

std::shared_ptr<Error> Wrap(const std::shared_ptr<Error>& err, std::string&& message)
std::shared_ptr<Error> Wrap(const std::shared_ptr<Error>& err, std::string&& message) noexcept
{
auto wrapped = std::make_shared<WrappedError>();
wrapped->Err = err;
wrapped->Message = std::move(message);
return std::move(wrapped);
}

std::shared_ptr<Error> Wrap(std::shared_ptr<Error>&& err, std::string&& message)
std::shared_ptr<Error> Wrap(std::shared_ptr<Error>&& err, std::string&& message) noexcept
{
auto wrapped = std::make_shared<WrappedError>();
wrapped->Err = std::move(err);
Expand Down

0 comments on commit 0f8be09

Please sign in to comment.