GTIRB  v2.1.0
GrammaTech Intermediate Representation for Binaries: C++ API
Public Types | Public Member Functions | Friends | List of all members
gtirb::ErrorOr< T > Class Template Reference

#include <ByteInterval.hpp>

Public Types

using storage_type = std::conditional_t< isRef, wrap, T >
 

Public Member Functions

 ErrorOr (const ErrorInfo &EI)
 
 ErrorOr (const ErrorOr &Other)
 
template<class OtherT >
 ErrorOr (const ErrorOr< OtherT > &Other, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
 
template<class OtherT >
 ErrorOr (const ErrorOr< OtherT > &Other, std::enable_if_t<!std::is_convertible< OtherT, const T & >::value > *=nullptr)
 
template<class E >
 ErrorOr (E ErrorCode, const std::string &Msg="", std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, void * >=nullptr)
 
 ErrorOr (ErrorOr &&Other)
 
template<class OtherT >
 ErrorOr (ErrorOr< OtherT > &&Other, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
 
template<class OtherT >
 ErrorOr (ErrorOr< OtherT > &&Other, std::enable_if_t<!std::is_convertible< OtherT, T >::value > *=nullptr)
 
template<class OtherT >
 ErrorOr (OtherT &&Val, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
 
 ErrorOr (std::error_code EC, const std::string &Msg="")
 
 ~ErrorOr ()
 
reference get ()
 
const_reference get () const
 
ErrorInfo getError () const
 
 operator bool () const
 Return false if there is an error. More...
 
reference operator* ()
 
const_reference operator* () const
 
pointer operator-> ()
 
const_pointer operator-> () const
 
ErrorOroperator= (const ErrorOr &Other)
 
ErrorOroperator= (ErrorOr &&Other)
 

Friends

template<class OtherT >
class ErrorOr
 

Detailed Description

template<class T>
class gtirb::ErrorOr< T >

Represents either an error or a value T.

ErrorOr<T> is a pointer-like class that represents the result of an operation. The result is either an error, or a value of type T. This is designed to emulate the usage of returning a pointer where nullptr indicates failure. However instead of just knowing that the operation failed, we also have an error_code and optional user data that describes why it failed.

It is used like the following.

ErrorOr<Buffer> getBuffer();
auto buffer = getBuffer();
if (error_code ec = buffer.getError())
return ec;
buffer->write("adena");

Implicit conversion to bool returns true if there is a usable value. The unary * and -> operators provide pointer like access to the value. Accessing the value when there is an error has undefined behavior.

When T is a reference type the behavior is slightly different. The reference is held in a std::reference_wrapper<std::remove_reference<T>::type>, and there is special handling to make operator -> work as if T was not a reference.

T cannot be a rvalue reference.

Member Typedef Documentation

◆ storage_type

template<class T >
using gtirb::ErrorOr< T >::storage_type = std::conditional_t<isRef, wrap, T>

Constructor & Destructor Documentation

◆ ErrorOr() [1/10]

template<class T >
template<class E >
gtirb::ErrorOr< T >::ErrorOr ( ErrorCode,
const std::string &  Msg = "",
std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, void * >  = nullptr 
)
inline

◆ ErrorOr() [2/10]

template<class T >
gtirb::ErrorOr< T >::ErrorOr ( std::error_code  EC,
const std::string &  Msg = "" 
)
inline

◆ ErrorOr() [3/10]

template<class T >
gtirb::ErrorOr< T >::ErrorOr ( const ErrorInfo EI)
inline

◆ ErrorOr() [4/10]

template<class T >
template<class OtherT >
gtirb::ErrorOr< T >::ErrorOr ( OtherT &&  Val,
std::enable_if_t< std::is_convertible< OtherT, T >::value > *  = nullptr 
)
inline

◆ ErrorOr() [5/10]

template<class T >
gtirb::ErrorOr< T >::ErrorOr ( const ErrorOr< T > &  Other)
inline

◆ ErrorOr() [6/10]

template<class T >
template<class OtherT >
gtirb::ErrorOr< T >::ErrorOr ( const ErrorOr< OtherT > &  Other,
std::enable_if_t< std::is_convertible< OtherT, T >::value > *  = nullptr 
)
inline

◆ ErrorOr() [7/10]

template<class T >
template<class OtherT >
gtirb::ErrorOr< T >::ErrorOr ( const ErrorOr< OtherT > &  Other,
std::enable_if_t<!std::is_convertible< OtherT, const T & >::value > *  = nullptr 
)
inlineexplicit

◆ ErrorOr() [8/10]

template<class T >
gtirb::ErrorOr< T >::ErrorOr ( ErrorOr< T > &&  Other)
inline

◆ ErrorOr() [9/10]

template<class T >
template<class OtherT >
gtirb::ErrorOr< T >::ErrorOr ( ErrorOr< OtherT > &&  Other,
std::enable_if_t< std::is_convertible< OtherT, T >::value > *  = nullptr 
)
inline

◆ ErrorOr() [10/10]

template<class T >
template<class OtherT >
gtirb::ErrorOr< T >::ErrorOr ( ErrorOr< OtherT > &&  Other,
std::enable_if_t<!std::is_convertible< OtherT, T >::value > *  = nullptr 
)
inlineexplicit

◆ ~ErrorOr()

template<class T >
gtirb::ErrorOr< T >::~ErrorOr ( )
inline

Member Function Documentation

◆ get() [1/2]

template<class T >
reference gtirb::ErrorOr< T >::get ( )
inline

◆ get() [2/2]

template<class T >
const_reference gtirb::ErrorOr< T >::get ( ) const
inline

◆ getError()

template<class T >
ErrorInfo gtirb::ErrorOr< T >::getError ( ) const
inline

◆ operator bool()

template<class T >
gtirb::ErrorOr< T >::operator bool ( ) const
inlineexplicit

Return false if there is an error.

◆ operator*() [1/2]

template<class T >
reference gtirb::ErrorOr< T >::operator* ( )
inline

◆ operator*() [2/2]

template<class T >
const_reference gtirb::ErrorOr< T >::operator* ( ) const
inline

◆ operator->() [1/2]

template<class T >
pointer gtirb::ErrorOr< T >::operator-> ( )
inline

◆ operator->() [2/2]

template<class T >
const_pointer gtirb::ErrorOr< T >::operator-> ( ) const
inline

◆ operator=() [1/2]

template<class T >
ErrorOr& gtirb::ErrorOr< T >::operator= ( const ErrorOr< T > &  Other)
inline

◆ operator=() [2/2]

template<class T >
ErrorOr& gtirb::ErrorOr< T >::operator= ( ErrorOr< T > &&  Other)
inline

Friends And Related Function Documentation

◆ ErrorOr

template<class T >
template<class OtherT >
friend class ErrorOr
friend

Member Data Documentation

◆ ErrorStorage

template<class T >
char gtirb::ErrorOr< T >::ErrorStorage[sizeof(ErrorInfo)]

◆ TStorage

template<class T >
char gtirb::ErrorOr< T >::TStorage[sizeof(storage_type)]

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