Adds copy operators on Result.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-12-11 16:38:38 +01:00
parent e3853bf290
commit fdfd5f05c0
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
1 changed files with 25 additions and 2 deletions

View File

@ -12,16 +12,39 @@ namespace ArbUt {
/// @brief Return a success, along with the success value. /// @brief Return a success, along with the success value.
/// @param value The value to return. /// @param value The value to return.
/// @return The result class /// @return The result class
inline static const Result& Ok(T value) { return Result(ResultStatus::Ok, value); } inline static Result Ok(T value) { return Result(ResultStatus::Ok, value); }
/// @brief Return an error, along with an error message. /// @brief Return an error, along with an error message.
/// @param message The error message to return /// @param message The error message to return
/// @return The result class /// @return The result class
inline static const Result& Error(const std::string& message) { return Result(message); } inline static Result Error(const std::string& message) { return Result(message); }
/// @brief Implicitly return a success result with a value. /// @brief Implicitly return a success result with a value.
/// @param value The value to return. /// @param value The value to return.
inline Result(const T& value) : Result(ResultStatus::Ok, value) {} inline Result(const T& value) : Result(ResultStatus::Ok, value) {}
/// @brief Copy constructor
/// @param rhs The object to construct from.
Result(const Result& rhs) : _resultStatus(rhs._resultStatus) {
if (_resultStatus == ResultStatus::Ok) {
_resultValue = rhs._resultValue;
} else {
_errorMessage = rhs._errorMessage;
}
}
/// @brief Copy operator.
inline Result<T>& operator=(const Result<T>& rhs) {
if (this == &rhs) {
return *this;
}
_resultStatus = rhs._resultStatus;
if (_resultStatus == ResultStatus::Ok) {
_resultValue = rhs._resultValue;
} else {
_errorMessage = rhs._errorMessage;
}
return *this;
}
/// @brief Returns whether or not the function succeeded, /// @brief Returns whether or not the function succeeded,
/// @return Whether or not the function succeeded. /// @return Whether or not the function succeeded.
[[nodiscard]] inline ResultStatus GetStatus() const noexcept { return _resultStatus; } [[nodiscard]] inline ResultStatus GetStatus() const noexcept { return _resultStatus; }