Pasha Bibko Util Library
|
Class to return a result from a function that can fail. More...
#include <ReturnVal.h>
Public Member Functions | |
ReturnVal (Res_Ty _result) | |
Copies the success result. | |
ReturnVal (Res_Ty &&_result) | |
Moves the success value. | |
ReturnVal (FunctionFail< Err_Ty > &&_error) | |
Moves the contents of a Util::FunctionFail<Err_Ty> to a Util::ReturnVal. | |
template<Result force = Result::Check> | |
Err_Ty & | Error () |
Returns a const reference to the error. | |
template<Result force = Result::Check> | |
Res_Ty & | Result () |
Returns a const reference to the result. | |
bool | Failed () const |
Returns whether the function failed or not. | |
bool | Success () const |
Returns whether the function suceeded or not. | |
Class to return a result from a function that can fail.
Res_Ty | The type of the result from the function (if there are no errors). |
Err_Ty | The type of the error from the function. |
Instead of throwing exceptions you can use this class as the return type of a function to either return the result or the error. This can be especially useful if manually allocating/deallocating memory.
When you have used it as the return type of a function you do not need to change how you return results. However if you want to return an error out of the function you now do it via: return Util::FunctionFail<Err_Ty>(args)
.
After you have returned the result out of a function you can check wether it failed or not using result.Failed()
or result.Succeeded()
. Depeding on the result you can then use result.Result()
or result.Error()
to get the value passed out of the function.
Copies the success result.
Recommended to use the copy-constructor for larger types.
Moves the success value.
Recommended to use for larger types.
|
inline |
Moves the contents of a Util::FunctionFail<Err_Ty> to a Util::ReturnVal.
Done automatically by the C++ compiler when returning a Util::FunctionFail so does not need to manually be called.
Returns a const reference to the error.
force | Defaults to Result::Check, when set to Result::Force the function does not check if the function has failed. |
Returns whether the function failed or not.
|
inline |
Returns a const reference to the result.
force | Defaults to Result::Check, when set to Result::Force the function does not check if the function has succeeded. |
Returns whether the function suceeded or not.