diff options
Diffstat (limited to 'include/Result.hpp')
-rw-r--r-- | include/Result.hpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/include/Result.hpp b/include/Result.hpp new file mode 100644 index 0000000..316e45a --- /dev/null +++ b/include/Result.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include <string> + +namespace amalgine +{ + template <typename T> + class Result + { + public: + static Result<T> Ok(const T &data) + { + Result<T> result; + result.data = data; + result.errorCode = 0; + return result; + } + + static Result<T> Err(const std::string &errorMsg, int errorCode = -1) + { + Result<T> result; + result.errorMsg = errorMsg; + result.errorCode = errorCode; + return result; + } + + template <typename OtherType> + static Result<T> Err(const Result<OtherType> &otherResult) + { + Result<T> result; + result.errorMsg = otherResult.errorMsg; + result.errorCode = otherResult.errorCode; + return result; + } + + bool isOk() const { return errorCode == 0; } + bool isErr() const { return !isOk(); } + operator bool() const { return isOk(); } + + const std::string& getErrorMsg() const { return errorMsg; } + private: + Result(){} + private: + T data; + int errorCode; + std::string errorMsg; + }; +} |