diff --git a/.README.md.kate-swp b/.README.md.kate-swp new file mode 100644 index 0000000..f976aaa Binary files /dev/null and b/.README.md.kate-swp differ diff --git a/result.hpp b/result.hpp index 499bbfb..ae470a5 100644 --- a/result.hpp +++ b/result.hpp @@ -57,7 +57,7 @@ public: * @param code * machine readable error code */ - Error(std::string message, ErrorEnum code) + Error(const std::string message, const ErrorEnum code) : m_code(code) { m_message = std::move(message); @@ -67,15 +67,15 @@ public: * @param message * human readable error message */ - Error(ErrorEnum code) + Error(const ErrorEnum code) : Error("", code){}; /** * @brief Error class constructor with empty error message * @param code * machine readable error code */ - Error(std::string message) - : Error(message, static_cast(0)){}; + Error(const std::string message) + : Error(message, ErrorEnum()){}; /** * @brief Error class destructor */ @@ -110,7 +110,7 @@ private: * @tparam ErrorEnum * error code enumeration */ -template +template class Result : public std::variant> { public: @@ -123,14 +123,14 @@ public: * @param value * result value */ - Result(T value) + Result(const T value) : std::variant>(value){}; /** * @brief Result constructor with error * @param error * result error */ - Result(Error error) + Result(const Error error) : std::variant>(error){}; /** @@ -150,6 +150,7 @@ public: }; /** * @brief get result content + * @return result object content type */ Content type() const { @@ -164,6 +165,41 @@ public: }; }; }; + +// additional functions +/** + * @brief match result function + * @tparam T + * value class name + * @tparam ErrorEnum + * error code enumeration + * @tparam UnaryFunctionValue + * function type which will be called if result contains value + * @tparam UnaryFunctionError + * function type which will be called if result contains error + * @param result + * result object + * @param apply_value + * function which will be called if result contains value + * @param apply_error + * function which will be called if result contains error + */ +template +void match(const Result &result, UnaryFunctionValue apply_value, + UnaryFunctionError apply_error) +{ + switch (result.type()) { + case Content::Value: + apply_value(result.get()); + break; + case Content::Error: + apply_error(result.error()); + break; + case Content::Empty: + break; + } +}; }; #endif /* _RESULT_HPP_ */