From fdfd5f05c0d2a1bd187bd725481c30d7858c8756 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 11 Dec 2020 16:38:38 +0100 Subject: [PATCH] Adds copy operators on Result. --- src/Result.hpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Result.hpp b/src/Result.hpp index ad3bc5c..05b392f 100644 --- a/src/Result.hpp +++ b/src/Result.hpp @@ -12,16 +12,39 @@ namespace ArbUt { /// @brief Return a success, along with the success value. /// @param value The value to return. /// @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. /// @param message The error message to return /// @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. /// @param value The value to return. 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& operator=(const Result& 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, /// @return Whether or not the function succeeded. [[nodiscard]] inline ResultStatus GetStatus() const noexcept { return _resultStatus; }