Pasha Bibko Util Library
Loading...
Searching...
No Matches
ReturnVal< Res_Ty, Err_Ty > Class Template Referencefinal

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_TyError ()
 Returns a const reference to the error.
 
template<Result force = Result::Check>
Res_TyResult ()
 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.
 

Detailed Description

template<typename Res_Ty, typename Err_Ty = DefaultError>
requires (!std::same_as<Res_Ty, void>) && (!std::same_as<Err_Ty, void>)
class PashaBibko::Util::ReturnVal< Res_Ty, Err_Ty >

Class to return a result from a function that can fail.

Template Parameters
Res_TyThe type of the result from the function (if there are no errors).
Err_TyThe 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.

Constructor & Destructor Documentation

◆ ReturnVal() [1/3]

template<typename Res_Ty , typename Err_Ty = DefaultError>
ReturnVal ( Res_Ty  _result)
inlineexplicit

Copies the success result.

Recommended to use the copy-constructor for larger types.

◆ ReturnVal() [2/3]

template<typename Res_Ty , typename Err_Ty = DefaultError>
ReturnVal ( Res_Ty &&  _result)
inline

Moves the success value.

Recommended to use for larger types.

◆ ReturnVal() [3/3]

template<typename Res_Ty , typename Err_Ty = DefaultError>
ReturnVal ( FunctionFail< Err_Ty > &&  _error)
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.

Member Function Documentation

◆ Error()

template<typename Res_Ty , typename Err_Ty = DefaultError>
template<Result force = Result::Check>
Err_Ty & Error ( )
inline

Returns a const reference to the error.

Template Parameters
forceDefaults to Result::Check, when set to Result::Force the function does not check if the function has failed.
Note
If checks are enabled and you try to access the error when the function has succeeded it will trigger a breakpoint and end the program.
Warning
Using Result::Force when the function may have succeeded can return invalid values. Using these values is undefined-behaviour so it is recommended to only use Result::Force when you are sure the function has failed.

◆ Failed()

template<typename Res_Ty , typename Err_Ty = DefaultError>
bool Failed ( ) const
inline

Returns whether the function failed or not.

◆ Result()

template<typename Res_Ty , typename Err_Ty = DefaultError>
template<Result force = Result::Check>
Res_Ty & Result ( )
inline

Returns a const reference to the result.

Template Parameters
forceDefaults to Result::Check, when set to Result::Force the function does not check if the function has succeeded.
Note
If checks are enabled and you try to access the result when the function has failed it will trigger a breakpoint and end the program.
Warning
Using Result::Force when the function may have failed can return invalid values. Using these values is undefined-behaviour so it is recommended to only use Result::Force when you are sure of the result.

◆ Success()

template<typename Res_Ty , typename Err_Ty = DefaultError>
bool Success ( ) const
inline

Returns whether the function suceeded or not.


The documentation for this class was generated from the following file: