aboutsummaryrefslogtreecommitdiff
path: root/include/Result.hpp
blob: 3ee60e53ae0b098dc5a203771f50d9d5bc20dba7 (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
49
50
51
#ifndef SIBS_RESULT_HPP
#define SIBS_RESULT_HPP

#include <cassert>
#include <string>

namespace sibs
{
	template <typename T>
	class Result
	{
	public:
		static Result Ok(const T &value)
		{
			Result result(value);
			result.error = false;
			return result;
		}

		static Result Err(const std::string &errMsg)
		{
			Result result;
			result.errMsg = errMsg;
			result.error = true;
			return result;
		}

		bool isOk() const { return !error; }
		bool isErr() const { return error; }

		const T& unwrap() const
		{
			assert(isOk());
			return value;
		}

		const std::string &getErrMsg() const
		{
			assert(isErr());
			return errMsg;
		}
	private:
		Result(const T &_value = T()) : value(_value) {}
	private:
		T value;
		std::string errMsg;
		bool error;
	};
}

#endif //SIBS_RESULT_HPP