aboutsummaryrefslogtreecommitdiff
path: root/include/Result.hpp
blob: 316e45af4c691c179492dd27265b86bdee013bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
    };
}