some improvements

* add const modifiers to constructors
* replace class to typename in templates
* add Result::match function
This commit is contained in:
Evgenii Alekseev 2017-10-26 03:49:36 +03:00
parent f3e2f844fa
commit f73aa9ca16
2 changed files with 43 additions and 7 deletions

BIN
.README.md.kate-swp Normal file

Binary file not shown.

View File

@ -57,7 +57,7 @@ public:
* @param code * @param code
* machine readable error code * machine readable error code
*/ */
Error(std::string message, ErrorEnum code) Error(const std::string message, const ErrorEnum code)
: m_code(code) : m_code(code)
{ {
m_message = std::move(message); m_message = std::move(message);
@ -67,15 +67,15 @@ public:
* @param message * @param message
* human readable error message * human readable error message
*/ */
Error(ErrorEnum code) Error(const ErrorEnum code)
: Error("", code){}; : Error("", code){};
/** /**
* @brief Error class constructor with empty error message * @brief Error class constructor with empty error message
* @param code * @param code
* machine readable error code * machine readable error code
*/ */
Error(std::string message) Error(const std::string message)
: Error(message, static_cast<ErrorEnum>(0)){}; : Error(message, ErrorEnum()){};
/** /**
* @brief Error class destructor * @brief Error class destructor
*/ */
@ -110,7 +110,7 @@ private:
* @tparam ErrorEnum * @tparam ErrorEnum
* error code enumeration * error code enumeration
*/ */
template <class T, typename ErrorEnum> template <typename T, typename ErrorEnum>
class Result : public std::variant<T, Error<ErrorEnum>> class Result : public std::variant<T, Error<ErrorEnum>>
{ {
public: public:
@ -123,14 +123,14 @@ public:
* @param value * @param value
* result value * result value
*/ */
Result(T value) Result(const T value)
: std::variant<T, Error<ErrorEnum>>(value){}; : std::variant<T, Error<ErrorEnum>>(value){};
/** /**
* @brief Result constructor with error * @brief Result constructor with error
* @param error * @param error
* result error * result error
*/ */
Result(Error<ErrorEnum> error) Result(const Error<ErrorEnum> error)
: std::variant<T, Error<ErrorEnum>>(error){}; : std::variant<T, Error<ErrorEnum>>(error){};
/** /**
@ -150,6 +150,7 @@ public:
}; };
/** /**
* @brief get result content * @brief get result content
* @return result object content type
*/ */
Content type() const 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 <typename T, typename ErrorEnum, typename UnaryFunctionValue,
typename UnaryFunctionError>
void match(const Result<T, ErrorEnum> &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_ */ #endif /* _RESULT_HPP_ */