Pasha Bibko Util Library
Loading...
Searching...
No Matches
ReturnVal.h
1#pragma once
2
3#include <sections/Misc.h>
4
5#include <type_traits>
6#include <concepts>
7#include <utility>
8
16namespace PashaBibko::Util
17{
18 #ifndef DOXYGEN_HIDE
19
20 /* Wether functions should check to see if the call is safe or not */
21 enum class Result
22 {
23 Check,
24 Force
25 };
26
27 /*
28 * Undocumented as it should be mentioned in all functions it is in
29 * and does not need its own section as it is essentially a boolean.
30 */
31
32 #endif // DOXYGEN_HIDE
33
41 {
46 : message("NO REASON PROVIDED") {}
47
54 DefaultError(const char* _message)
55 : message(_message) {}
56
60 const char* message;
61 };
62
71 template<typename Err_Ty = DefaultError>
72 struct FunctionFail final
73 {
77 explicit FunctionFail(Err_Ty _error)
78 : error(_error)
79 {}
80
91 template<typename... Args>
92 requires std::is_constructible_v<Err_Ty, Args...>
93 explicit FunctionFail(Args&&... args)
94 : error(std::forward<Args>(args)...)
95 {}
96
100 Err_Ty error;
101 };
102
122 template<typename Res_Ty, typename Err_Ty = DefaultError>
123 requires (!std::same_as<Res_Ty, void>) && (!std::same_as<Err_Ty, void>)
124 class ReturnVal final
125 {
126 public:
132 explicit ReturnVal(Res_Ty _result)
133 : m_Result(_result), m_FunctionFailed(false)
134 {}
135
141 ReturnVal(Res_Ty&& _result)
142 : m_Result(std::move(_result)), m_FunctionFailed(false)
143 {}
144
152 : m_Error(std::move(_error.error)), m_FunctionFailed(true)
153 {}
154
155 /* Constructor is not manually called by someone using the library so it is excluded from docs */
156 #ifndef DOXYGEN_HIDE
157
158 ~ReturnVal()
159 {
160 /* Makes sure the types deconstructor is called to avoid memory leaks */
161 if (m_FunctionFailed)
162 m_Error.~Err_Ty();
163
164 else
165 m_Result.~Res_Ty();
166 }
167
168 #endif
169
183 template<Result force = Result::Check>
184 inline Err_Ty& Error()
185 {
186 /* Force bypasses checking if the function failed or not */
187 if constexpr (force == Result::Check)
188 {
189 /* Ends the process if the error was tried to access on a sucessful return */
190 if (!m_FunctionFailed)
191 EndProcess();
192 }
193
194 /* Returns a const reference to the error */
195 return m_Error;
196 }
197
211 template<Result force = Result::Check>
212 inline Res_Ty& Result()
213 {
214 /* Force bypasses checking if the function failed or not */
215 if constexpr (force == Result::Check)
216 {
217 /* Ends the process if the result was tried to access on a failed result */
218 if (m_FunctionFailed)
219 EndProcess();
220 }
221
222 /* Returns a const reference to the result */
223 return m_Result;
224 }
225
229 inline bool Failed() const { return m_FunctionFailed; }
230
234 inline bool Success() const { return !m_FunctionFailed; }
235
236 private:
237 #ifndef DOXYGEN_HIDE
238
239 /* Union to hold either the result or the error */
240 union
241 {
242 Res_Ty m_Result;
243 Err_Ty m_Error;
244 };
245
246 /*
247 * It appears in Doxygen even though it is within a private block
248 * which is why it is located in a pre-processor block
249 */
250
251 #endif // DOXYGEN_HIDE
252
253 /* Holds wether the function failed or not */
254 const bool m_FunctionFailed;
255 };
256}
Class to return a result from a function that can fail.
Definition ReturnVal.h:125
bool Success() const
Returns whether the function suceeded or not.
Definition ReturnVal.h:234
ReturnVal(Res_Ty _result)
Copies the success result.
Definition ReturnVal.h:132
ReturnVal(Res_Ty &&_result)
Moves the success value.
Definition ReturnVal.h:141
bool Failed() const
Returns whether the function failed or not.
Definition ReturnVal.h:229
ReturnVal(FunctionFail< Err_Ty > &&_error)
Moves the contents of a Util::FunctionFail<Err_Ty> to a Util::ReturnVal.
Definition ReturnVal.h:151
Res_Ty & Result()
Returns a const reference to the result.
Definition ReturnVal.h:212
Err_Ty & Error()
Returns a const reference to the error.
Definition ReturnVal.h:184
Definition Colour.h:10
void EndProcess(bool breakpoint=true)
Ends the current process.
Default error class for Uti::ReturnVal.
Definition ReturnVal.h:41
const char * message
A pointer to a c-string error message, not owned by the class/object.
Definition ReturnVal.h:60
DefaultError(const char *_message)
Provide an error message for the error to carry.
Definition ReturnVal.h:54
DefaultError()
Default constructor, sets the message to "NO REASON PROVIDED".
Definition ReturnVal.h:45
Class to create when a function fails.
Definition ReturnVal.h:73
Err_Ty error
The error that the function is returning.
Definition ReturnVal.h:100
FunctionFail(Err_Ty _error)
Constructor that copies an error and stores it.
Definition ReturnVal.h:77
FunctionFail(Args &&... args)
Constructor that creates the error within the class.
Definition ReturnVal.h:93