Simple Lua
A Simple C++ Lua Wrapper
Loading...
Searching...
No Matches
Result.hpp
1#pragma once
2
3#include "../Def.hpp"
4
5#include <string>
6#include <cassert>
7#include <optional>
8
9namespace SL::Util
10{
15 template<typename T = int>
16 struct Error
17 {
18 Error(const T& num, const std::string& msg = "");
19 ~Error() = default;
20
21 const T& code() const;
22 const std::string& message() const;
23
24 private:
25 T _num;
26 std::string _msg;
27 };
28
35 template<typename T, typename E = Error<>>
36 struct Result
37 {
38 Result(const Result&) = delete;
39 Result(Result&&) = delete;
40
41 Result(T&& val);
42 Result(const E& err);
43 ~Result() = default;
44
45 T& value();
46 const T& value() const;
47 const E& error() const;
48 bool good() const;
49
50 operator bool() const;
51
52 constexpr T* operator->() { return &*_val; }
53 constexpr const T* operator->() const { return &*_val; }
54 constexpr T& operator*() { return *_val; }
55 constexpr const T& operator*() const { return *_val; }
56
57 private:
58 std::optional<T> _val;
59 std::optional<E> _err;
60 };
61
62 template<typename E>
63 struct Result<void, E>
64 {
65 Result(const Result&) = delete;
66 Result(Result&&) = delete;
67
68 Result() = default;
69 Result(const E& err);
70 ~Result() = default;
71
72 const E& error() const;
73 bool good() const;
74
75 operator bool() const;
76
77 private:
78 std::optional<E> _err;
79 };
80
81 /* struct Error */
82 template<typename T>
83 Error<T>::Error(const T& num, const std::string& msg) :
84 _num(num),
85 _msg(msg)
86 { }
87
88 template<typename T>
89 const T& Error<T>::code() const
90 { return _num; }
91
92 template<typename T>
93 const std::string& Error<T>::message() const
94 { return _msg; }
95
96 /* struct Result */
97 template<typename T, typename E>
98 Result<T, E>::Result(T&& val) :
99 _val(val)
100 { }
101
102 template<typename T, typename E>
103 Result<T, E>::Result(const E& err) :
104 _err(err)
105 { }
106
107 template<typename E>
108 Result<void, E>::Result(const E& err) :
109 _err(err)
110 { }
111
112 template<typename T, typename E>
113 T& Result<T, E>::value()
114 {
115 SL_ASSERT(good(), "Result not good!");
116 return _val.value();
117 }
118
119 template<typename T, typename E>
120 const T& Result<T, E>::value() const
121 {
122 SL_ASSERT(good(), "Result not good!");
123 return _val.value();
124 }
125
126 template<typename T, typename E>
127 const E& Result<T, E>::error() const
128 {
129 SL_ASSERT(_err.has_value(), "Result is good!");
130 return _err.value();
131 }
132
133 template<typename E>
134 const E& Result<void, E>::error() const
135 {
136 SL_ASSERT(_err.has_value(), "Result is good!");
137 return _err.value();
138 }
139
140 template<typename T, typename E>
141 bool Result<T, E>::good() const
142 {
143 return _val.has_value();
144 }
145
146 template<typename E>
147 bool Result<void, E>::good() const
148 {
149 return !_err.has_value();
150 }
151
152 template<typename T, typename E>
153 Result<T, E>::operator bool() const
154 { return good(); }
155
156 template<typename E>
157 Result<void, E>::operator bool() const
158 { return good(); }
159
160} // S2D::Util
Simple wrapper class for passing errors along to user.
Definition Result.hpp:17
Basic way to return a value or an error.
Definition Result.hpp:37