The class is used as a return type for operations that can fail; wraps a value of type T or an RError.
The RResult<T> class and their related classes are used for call chains that can throw exceptions, such as I/O code paths. Throwing of the exception is deferred to allow for if (result) style error checking where it makes sense. If an RResult in error state leaves the scope unchecked, it will throw.
A function returning an RResult might look like this:
Code using MyIOFunc might look like this:
Note that RResult<void> can be used for a function without return value, like this
RResult<T>::Unwrap() can be used as a short hand for "give me the wrapped value or, in case of an error, throw". For instance:
There is no implicit operator that converts RResult<T> to T. This is intentional to make it clear in the calling code where an exception may be thrown.
Definition at line 197 of file RError.hxx.
Public Member Functions | |
| RResult (const RResult &other)=delete | |
| RResult (const T &value) | |
| RResult (RError &&error) | |
| RResult (RResult &&other)=default | |
| RResult (T &&value) | |
| ~RResult ()=default | |
| RResult & | Forward (RError::RLocation &&sourceLocation) |
| Used by R__FORWARD_RESULT in order to keep track of the stack trace in case of errors. | |
| std::optional< RError > | GetError () const |
| const T & | Inspect () |
| If the operation was successful, returns a const reference to the inner type. | |
| operator bool () | |
| RResult & | operator= (const RResult &other)=delete |
| RResult & | operator= (RResult &&other)=default |
| void | Throw () |
| Throws an RException with fError. | |
| T | Unwrap () |
| If the operation was successful, returns the inner type by value. | |
Static Public Member Functions | |
| static RError | ForwardError (RResultBase &&result, RError::RLocation &&sourceLocation) |
| Used by R__FORWARD_ERROR in order to keep track of the stack trace. | |
Protected Member Functions | |
| bool | Check () |
| Used by the RResult<T> bool operator. | |
Protected Attributes | |
| std::unique_ptr< RError > | fError |
| This is the nullptr for an RResult representing success. | |
| bool | fIsChecked {false} |
| Switches to true once the user of an RResult object checks the object status. | |
Private Member Functions | |
| void | ThrowOnError () |
Private Attributes | |
| std::optional< T > | fValue |
| The result value, only present in case of successful execution. | |
#include <ROOT/RError.hxx>
|
inline |
Definition at line 218 of file RError.hxx.
|
inline |
Definition at line 219 of file RError.hxx.
|
inline |
Definition at line 220 of file RError.hxx.
|
delete |
|
default |
|
default |
|
inlineprotectedinherited |
Used by the RResult<T> bool operator.
Definition at line 108 of file RError.hxx.
|
inline |
Used by R__FORWARD_RESULT in order to keep track of the stack trace in case of errors.
Definition at line 230 of file RError.hxx.
|
inlinestaticnodiscardinherited |
Used by R__FORWARD_ERROR in order to keep track of the stack trace.
Definition at line 128 of file RError.hxx.
|
inlineinherited |
Definition at line 122 of file RError.hxx.
|
inline |
If the operation was successful, returns a const reference to the inner type.
If there was an error, Inspect() instead throws an exception.
Definition at line 239 of file RError.hxx.
|
inlineexplicit |
Definition at line 258 of file RError.hxx.
|
delete |
|
default |
|
inherited |
Throws an RException with fError.
Definition at line 61 of file RError.cxx.
|
inlineprivate |
Definition at line 203 of file RError.hxx.
|
inline |
If the operation was successful, returns the inner type by value.
For move-only types, Unwrap can only be called once, as it yields ownership of the inner value to the caller using std::move, potentially leaving the RResult in an unspecified state.
If there was an error, Unwrap() instead throws an exception.
Definition at line 252 of file RError.hxx.
|
protectedinherited |
This is the nullptr for an RResult representing success.
Definition at line 100 of file RError.hxx.
|
protectedinherited |
Switches to true once the user of an RResult object checks the object status.
Definition at line 102 of file RError.hxx.
|
private |
The result value, only present in case of successful execution.
Definition at line 200 of file RError.hxx.