summaryrefslogtreecommitdiffhomepage
path: root/dev/lib/logic/opt.hpp
blob: facbf2c7b4ee254b5a29864df1167ddd23405172 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * File: opt.hpp
 * Author: Amlal El Mahrouss,
 * Copyright 2023-2025, Amlal El Mahrouss/SNU Systems Corp all rights reserved.
 */

#ifndef _SNU_OPT_HPP
#define _SNU_OPT_HPP

#include <lib/detail/error.hpp>
#include <utility>

namespace snu::opt
{
	enum class return_type
	{
		invalid = 0,
		okay	= 100,
		err,
		count = err - okay + 1,
	};

	struct opt final
	{
		explicit opt(const return_type& return_type)
			: m_ret(return_type)
		{
		}

		opt& expect(const char* input)
		{
			if (m_ret == return_type::err)
			{
				throw std::runtime_error(input);
			}

			return *this;
		}

	private:
		return_type m_ret;
	};

	template <typename Teller, typename... Lst>
	inline return_type eval(Teller tell, Lst&&... arg)
	{
		return tell(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
	}

	namespace traits
	{
		struct int_eq_teller
		{
			explicit int_eq_teller()
			{
			}

			bool operator()(int a, int b)
			{
				return (a == b);
			}
		};

		struct int_greater_than_teller
		{
			explicit int_greater_than_teller()
			{
			}

			bool operator()(int a, int b)
			{
				return (a > b);
			}
		};

		struct int_less_than_teller
		{
			explicit int_less_than_teller()
			{
			}

			bool operator()(int a, int b)
			{
				return (a < b);
			}
		};
	} // namespace traits

	template <typename... Lst>
	inline return_type eval_less_than(Lst&&... arg)
	{
		static traits::int_less_than_teller eq;
		return eq(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
	}

	template <typename... Lst>
	inline return_type eval_eq(Lst&&... arg)
	{
		static traits::int_eq_teller less_than;
		return less_than(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
	}

	template <typename... Lst>
	inline return_type eval_greater_than(Lst&&... arg)
	{
		static traits::int_greater_than_teller greater_than;
		return greater_than(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err;
	}

	inline return_type eval_true()
	{
		return return_type::okay;
	}

	inline return_type eval_false()
	{
		return return_type::err;
	}
} // namespace snu::opt

#endif /* ifndef _SNU_OPT_HPP */