Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROOT::Experimental::RResult< T > Class Template Reference

template<typename T>
class ROOT::Experimental::RResult< T >

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:

RResult<int> MyIOFunc()
{
int rv = syscall(...);
if (rv == -1)
return R__FAIL("user-facing error message");
if (rv == kShortcut)
return 42;
return R__FORWARD_RESULT(FuncThatReturnsRResultOfInt());
}
#define R__FORWARD_RESULT(res)
Short-hand to return an RResult<T> value from a subroutine to the calling stack frame.
Definition RError.hxx:292
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:290
The class is used as a return type for operations that can fail; wraps a value of type T or an RError...
Definition RError.hxx:194

Code using MyIOFunc might look like this:

auto result = MyIOOperation();
if (!result) {
// custom error handling or result.Throw()
}
switch (result.Inspect()) {
...
}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result

Note that RResult<void> can be used for a function without return value, like this

RResult<void> DoSomething()
{
if (failure)
return R__FAIL("user-facing error messge");
}

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:

int value = FuncThatReturnsRResultOfInt().Unwrap(); // may throw
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value

There is no implict 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 194 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
 
RResultForward (RError::RLocation &&sourceLocation)
 Used by R__FORWARD_RESULT in order to keep track of the stack trace in case of errors.
 
const T & Inspect ()
 If the operation was successful, returns a const reference to the inner type.
 
 operator bool ()
 
RResultoperator= (const RResult &other)=delete
 
RResultoperator= (RResult &&other)=default
 
Unwrap ()
 If the operation was successful, returns the inner type by value.
 
- Public Member Functions inherited from ROOT::Experimental::RResultBase
 RResultBase (const RResultBase &other)=delete
 
 RResultBase (RResultBase &&other)=default
 
 ~RResultBase () noexcept(false)
 
RErrorGetError ()
 
RResultBaseoperator= (const RResultBase &other)=delete
 
RResultBaseoperator= (RResultBase &&other)=default
 
void Throw ()
 Throws an RException with fError.
 

Private Member Functions

void ThrowOnError ()
 

Private Attributes

fValue
 The result value in case of successful execution.
 

Additional Inherited Members

- Static Public Member Functions inherited from ROOT::Experimental::RResultBase
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 inherited from ROOT::Experimental::RResultBase
 RResultBase ()=default
 
 RResultBase (RError &&error)
 
bool Check ()
 Used by the RResult<T> bool operator.
 
- Protected Attributes inherited from ROOT::Experimental::RResultBase
std::unique_ptr< RErrorfError
 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.
 

#include <ROOT/RError.hxx>

Inheritance diagram for ROOT::Experimental::RResult< T >:
[legend]

Constructor & Destructor Documentation

◆ RResult() [1/5]

template<typename T >
ROOT::Experimental::RResult< T >::RResult ( const T &  value)
inline

Definition at line 214 of file RError.hxx.

◆ RResult() [2/5]

template<typename T >
ROOT::Experimental::RResult< T >::RResult ( T &&  value)
inline

Definition at line 215 of file RError.hxx.

◆ RResult() [3/5]

template<typename T >
ROOT::Experimental::RResult< T >::RResult ( RError &&  error)
inline

Definition at line 216 of file RError.hxx.

◆ RResult() [4/5]

template<typename T >
ROOT::Experimental::RResult< T >::RResult ( const RResult< T > &  other)
delete

◆ RResult() [5/5]

template<typename T >
ROOT::Experimental::RResult< T >::RResult ( RResult< T > &&  other)
default

◆ ~RResult()

template<typename T >
ROOT::Experimental::RResult< T >::~RResult ( )
default

Member Function Documentation

◆ Forward()

template<typename T >
RResult & ROOT::Experimental::RResult< T >::Forward ( RError::RLocation &&  sourceLocation)
inline

Used by R__FORWARD_RESULT in order to keep track of the stack trace in case of errors.

Definition at line 226 of file RError.hxx.

◆ Inspect()

template<typename T >
const T & ROOT::Experimental::RResult< T >::Inspect ( )
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 234 of file RError.hxx.

◆ operator bool()

template<typename T >
ROOT::Experimental::RResult< T >::operator bool ( )
inlineexplicit

Definition at line 251 of file RError.hxx.

◆ operator=() [1/2]

template<typename T >
RResult & ROOT::Experimental::RResult< T >::operator= ( const RResult< T > &  other)
delete

◆ operator=() [2/2]

template<typename T >
RResult & ROOT::Experimental::RResult< T >::operator= ( RResult< T > &&  other)
default

◆ ThrowOnError()

template<typename T >
void ROOT::Experimental::RResult< T >::ThrowOnError ( )
inlineprivate

Definition at line 200 of file RError.hxx.

◆ Unwrap()

template<typename T >
T ROOT::Experimental::RResult< T >::Unwrap ( )
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 246 of file RError.hxx.

Member Data Documentation

◆ fValue

template<typename T >
T ROOT::Experimental::RResult< T >::fValue
private

The result value in case of successful execution.

Definition at line 197 of file RError.hxx.

  • core/foundation/v7/inc/ROOT/RError.hxx