Asylo
Classes | Public Types | Public Member Functions | Friends | List of all members
asylo::StatusOr< T > Class Template Reference

A class for representing either a usable value, or an error. More...

#include <statusor.h>

Public Types

using value_type = T
 An alias for T. Useful for generic programming. More...
 

Public Member Functions

 StatusOr ()
 Constructs a StatusOr object that contains a non-OK status. More...
 
 ~StatusOr ()
 
 StatusOr (const Status &status)
 Constructs a StatusOr object with the given non-OK Status object. More...
 
 StatusOr (const absl::Status &status)
 Constructs a StatusOr object with the given non-OK absl::Status object. More...
 
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, U>::value && !std::is_same<typename std::remove_reference< typename std::remove_cv<U>::type>::type, Status>::value && !std::is_same<typename std::remove_reference< typename std::remove_cv<U>::type>::type, absl::Status>::value>::type>
 StatusOr (U &&value)
 Constructs a StatusOr object that contains value. More...
 
 StatusOr (const StatusOr &other)
 Copy constructor. More...
 
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, const U &>::value>::type>
 StatusOr (const StatusOr< U > &other)
 Templatized constructor that constructs a StatusOr<T> from a const reference to a StatusOr<U>. More...
 
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, const U &>::value>::type>
 StatusOr (const absl::StatusOr< U > &other)
 Templatized constructor that constructs a StatusOr<T> from a const reference to an absl::StatusOr<U>. More...
 
StatusOroperator= (const StatusOr &other)
 Copy-assignment operator. More...
 
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, U &&>::value>::type>
 StatusOr (StatusOr< U > &&other)
 Templatized constructor which constructs a StatusOr<T> by moving the contents of a StatusOr<U>. More...
 
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, U &&>::value>::type>
 StatusOr (absl::StatusOr< U > &&other)
 Templatized constructor which constructs a StatusOr<T> by moving the contents of an absl::StatusOr<U>. More...
 
StatusOroperator= (StatusOr &&other)
 Move-assignment operator. More...
 
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<U, T &&>::value>::type>
 operator absl::StatusOr< U > () const
 
bool ok () const
 Indicates whether the object contains a T value. More...
 
Status status () const
 Gets the stored status object, or an OK status if a T value is stored. More...
 
const T & value () const &
 Gets the stored T value. More...
 
T & value () &
 Gets the stored T value. More...
 
value () &&
 Moves and returns the internally-stored T value. More...
 
 ABSL_DEPRECATED ("Deprecated as part of Asylo's absl::Status migration. Use value(), " "operator*(), or operator->() instead.") const T &ValueOrDie() const &
 Gets the stored T value. More...
 
 ABSL_DEPRECATED ("Deprecated as part of Asylo's absl::Status migration. Use value(), " "operator*(), or operator->() instead.") T &ValueOrDie() &
 Gets a mutable reference to the stored T value. More...
 
 ABSL_DEPRECATED ("Deprecated as part of Asylo's absl::Status migration. Use value(), " "operator*(), or operator->() instead.") T ValueOrDie() &&
 Moves and returns the internally-stored T value. More...
 
const T & operator* () const &
 Gets the stored T value. More...
 
T & operator* () &
 Gets the stored T value. More...
 
operator* () &&
 Gets the stored T value. More...
 
const T * operator-> () const
 Aecceses the stored T value. More...
 
T * operator-> ()
 Gets the stored T value. More...
 

Friends

template<typename U >
class StatusOr
 

Detailed Description

template<class T>
class asylo::StatusOr< T >

A class for representing either a usable value, or an error.

A StatusOr object either contains a value of type T or a Status object explaining why such a value is not present. The type T must be copy-constructible and/or move-constructible.

The state of a StatusOr object may be determined by calling ok() or status(). The ok() method returns true if the object contains a valid value. The status() method returns the internal Status object. A StatusOr object that contains a valid value will return an OK Status for a call to status().

A value of type T may be extracted by dereferencing an OK StatusOr object, either with operator*() or operator->(). These operators should only be called if a call to ok() returns true. Sample usage:

asylo::StatusOr<Foo> result = CalculateFoo();
if (result.ok()) {
Foo foo = *result;
foo.DoSomethingCool();
} else {
LOG(ERROR) << result.status();
}

Or more concisely:

asylo::StatusOr<Foo> result = CalculateFoo();
if (result.ok()) {
result->DoSomethingCool();
} else {
LOG(ERROR) << result.status();
}

If T is a move-only type, like std::unique_ptr<>, then the value should only be extracted after invoking std::move() on the StatusOr object. Sample usage:

asylo::StatusOr<std::unique_ptr<Foo>> result = CalculateFoo();
if (result.ok()) {
std::unique_ptr<Foo> foo = *std::move(result);
foo->DoSomethingCool();
} else {
LOG(ERROR) << result.status();
}

If exceptions are enabled, callers can alternatively use the value() method to extract the contents of a StatusOr. Calls to value() throw absl::BadStatusOrAccess if the StatusOr is not OK. Sample usage:

asylo::StatusOr<Foo> result = CalculateFoo();
try {
result.value().DoSomethingCool();
} catch (const absl::BadStatusOrAccess &bad_access) {
LOG(ERROR) << bad_access.status();
}

If exceptions are disabled, then calls to value() on a non-OK StatusOr will abort the program.

StatusOr is provided for the convenience of implementing functions that return some value but may fail during execution. For instance, consider a function with the following signature:

asylo::Status CalculateFoo(int *output);

This function may instead be written as:

asylo::StatusOr<int> CalculateFoo();

Member Typedef Documentation

◆ value_type

template<class T>
using asylo::StatusOr< T >::value_type = T

An alias for T. Useful for generic programming.

Constructor & Destructor Documentation

◆ StatusOr() [1/9]

template<class T>
asylo::StatusOr< T >::StatusOr ( )
inlineexplicit

Constructs a StatusOr object that contains a non-OK status.

The non-OK status has an error code of -1. This is a non-standard POSIX error code and is used in this context to indicate an unknown error.

This constructor is marked explicit to prevent attempts to return {} from a function with a return type of, for example, StatusOr<std::vector<int>>. While return {} seems like it would return an empty vector, it will actually invoke the default constructor of StatusOr.

◆ ~StatusOr()

template<class T>
asylo::StatusOr< T >::~StatusOr ( )
inline

◆ StatusOr() [2/9]

template<class T>
asylo::StatusOr< T >::StatusOr ( const Status status)
inline

Constructs a StatusOr object with the given non-OK Status object.

The given status must not be an OK status, otherwise this constructor will abort.

This constructor is not declared explicit so that a function with a return type of StatusOr<T> can return a Status object, and the status will be implicitly converted to the appropriate return type as a matter of convenience.

Parameters
statusThe non-OK Status object to initalize to.

◆ StatusOr() [3/9]

template<class T>
asylo::StatusOr< T >::StatusOr ( const absl::Status &  status)
inline

Constructs a StatusOr object with the given non-OK absl::Status object.

All calls to value() on this object will throw an exception or cause the program to abort. The given status must not be an OK status, otherwise this constructor will cause an abort.

This constructor is not declared explicit so that a function with a return type of StatusOr<T> can return an absl::Status object, and the status will be implicitly converted to the appropriate return type as a matter of convenience.

Parameters
statusThe non-OK absl::Status object to initalize to.

◆ StatusOr() [4/9]

template<class T>
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, U>::value && !std::is_same<typename std::remove_reference< typename std::remove_cv<U>::type>::type, Status>::value && !std::is_same<typename std::remove_reference< typename std::remove_cv<U>::type>::type, absl::Status>::value>::type>
asylo::StatusOr< T >::StatusOr ( U &&  value)
inline

Constructs a StatusOr object that contains value.

The resulting object is considered to have an OK status. The wrapped element can be accessed by dereferencing or with value().

This constructor is made implicit so that a function with a return type of StatusOr<T> can return an object of type U &&, implicitly converting it to a StatusOr<T> object.

Note that T must be implicitly constructible from U, and U must not be a (cv-qualified) Status or Status-reference type. Due to C++ reference-collapsing rules and perfect-forwarding semantics, this constructor matches invocations that pass value either as a const reference or as an rvalue reference. Since StatusOr needs to work for both reference and rvalue-reference types, the constructor uses perfect forwarding to avoid invalidating arguments that were passed by reference. See http://thbecker.net/articles/rvalue_references/section_08.html for additional details.

Parameters
valueThe value to initialize to.

◆ StatusOr() [5/9]

template<class T>
asylo::StatusOr< T >::StatusOr ( const StatusOr< T > &  other)
inline

Copy constructor.

This constructor needs to be explicitly defined because the presence of the move-assignment operator deletes the default copy constructor. In such a scenario, since the deleted copy constructor has stricter binding rules than the templated copy constructor, the templated constructor cannot act as a copy constructor, and any attempt to copy-construct a StatusOr object results in a compilation error.

Parameters
otherThe value to copy from.

◆ StatusOr() [6/9]

template<class T>
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, const U &>::value>::type>
asylo::StatusOr< T >::StatusOr ( const StatusOr< U > &  other)
inline

Templatized constructor that constructs a StatusOr<T> from a const reference to a StatusOr<U>.

T must be implicitly constructible from const U &.

Parameters
otherThe value to copy from.

◆ StatusOr() [7/9]

template<class T>
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, const U &>::value>::type>
asylo::StatusOr< T >::StatusOr ( const absl::StatusOr< U > &  other)
inline

Templatized constructor that constructs a StatusOr<T> from a const reference to an absl::StatusOr<U>.

T must be implicitly constructible from const U &.

Parameters
otherThe value to copy from.

◆ StatusOr() [8/9]

template<class T>
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, U &&>::value>::type>
asylo::StatusOr< T >::StatusOr ( StatusOr< U > &&  other)
inline

Templatized constructor which constructs a StatusOr<T> by moving the contents of a StatusOr<U>.

T must be implicitly constructible from U &&.

Sets other to a valid but unspecified state.

Parameters
otherThe StatusOr object to move from.

◆ StatusOr() [9/9]

template<class T>
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<T, U &&>::value>::type>
asylo::StatusOr< T >::StatusOr ( absl::StatusOr< U > &&  other)
inline

Templatized constructor which constructs a StatusOr<T> by moving the contents of an absl::StatusOr<U>.

T must be implicitly constructible from U &&.

Parameters
otherThe absl::StatusOr<U> object to move from.

Member Function Documentation

◆ ABSL_DEPRECATED() [1/3]

template<class T>
asylo::StatusOr< T >::ABSL_DEPRECATED ( "Deprecated as part of Asylo's absl::Status migration. Use   value(),
" "operator*  (),
or operator->() instead."   
) const &
inline

Gets the stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise this call will abort.

Deprecated:
Deprecated as part of Asylo's absl::Status migration. Use value(), operator*(), or operator->() instead.
Returns
The stored T value.

◆ ABSL_DEPRECATED() [2/3]

template<class T>
asylo::StatusOr< T >::ABSL_DEPRECATED ( "Deprecated as part of Asylo's absl::Status migration. Use   value(),
" "operator*  (),
or operator->() instead."   
) &
inline

Gets a mutable reference to the stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise this call will abort.

Deprecated:
Deprecated as part of Asylo's absl::Status migration. Use value(), operator*(), or operator->() instead.
Returns
The stored T value.

◆ ABSL_DEPRECATED() [3/3]

template<class T>
asylo::StatusOr< T >::ABSL_DEPRECATED ( "Deprecated as part of Asylo's absl::Status migration. Use   value(),
" "operator*  (),
or operator->() instead."   
) &&
inline

Moves and returns the internally-stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise this call will abort. The StatusOr object is changed after this call to a valid but unspecified state.

Deprecated:
Deprecated as part of Asylo's absl::Status migration. Use value(), operator*(), or operator->() instead.
Returns
The stored T value.

◆ ok()

template<class T>
bool asylo::StatusOr< T >::ok ( ) const
inline

Indicates whether the object contains a T value.

Returns
True if this StatusOr object's status is OK (i.e. a call to ok() returns true). If this function returns true, then it is safe to dereference this StatusOr.

◆ operator absl::StatusOr< U >()

template<class T>
template<typename U , typename E = typename std::enable_if< is_implicitly_constructible<U, T &&>::value>::type>
asylo::StatusOr< T >::operator absl::StatusOr< U > ( ) const
inline

◆ operator*() [1/3]

template<class T>
const T& asylo::StatusOr< T >::operator* ( ) const &
inline

Gets the stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise the behavior of this method is undefined.

Returns
The stored T value.

◆ operator*() [2/3]

template<class T>
T& asylo::StatusOr< T >::operator* ( ) &
inline

Gets the stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise the behavior of this method is undefined.

Returns
The stored T value.

◆ operator*() [3/3]

template<class T>
T asylo::StatusOr< T >::operator* ( ) &&
inline

Gets the stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise the behavior of this method is undefined. The StatusOr object is changed after this call to a valid but unspecified state.

Returns
The stored T value.

◆ operator->() [1/2]

template<class T>
const T* asylo::StatusOr< T >::operator-> ( ) const
inline

Aecceses the stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise the behavior of this method is undefined.

Returns
A pointer to the stored T value.

◆ operator->() [2/2]

template<class T>
T* asylo::StatusOr< T >::operator-> ( )
inline

Gets the stored T value.

This method should only be called if this StatusOr object's status is OK (i.e. a call to ok() returns true), otherwise the behavior of this method is undefined.

Returns
The stored T value.

◆ operator=() [1/2]

template<class T>
StatusOr& asylo::StatusOr< T >::operator= ( const StatusOr< T > &  other)
inline

Copy-assignment operator.

Parameters
otherThe StatusOr object to copy.

◆ operator=() [2/2]

template<class T>
StatusOr& asylo::StatusOr< T >::operator= ( StatusOr< T > &&  other)
inline

Move-assignment operator.

Sets other to a valid but unspecified state.

Parameters
otherThe StatusOr object to assign from.

◆ status()

template<class T>
Status asylo::StatusOr< T >::status ( ) const
inline

Gets the stored status object, or an OK status if a T value is stored.

Returns
The stored non-OK status object, or an OK status if this object has a value.

◆ value() [1/3]

template<class T>
const T& asylo::StatusOr< T >::value ( ) const &
inline

Gets the stored T value.

If this StatusOr object is not OK, then this method either throws an absl::BadStatusOrAccess exception or aborts the program, depending on whether exceptions are enabled.

Returns
The stored T value.

◆ value() [2/3]

template<class T>
T& asylo::StatusOr< T >::value ( ) &
inline

Gets the stored T value.

If this StatusOr object is not OK, then this method either throws an absl::BadStatusOrAccess exception or aborts the program, depending on whether exceptions are enabled.

Returns
The stored T value.

◆ value() [3/3]

template<class T>
T asylo::StatusOr< T >::value ( ) &&
inline

Moves and returns the internally-stored T value.

The StatusOr object is invalidated after this call and will be updated to a valid but unspecified state.

If this StatusOr object is not OK, then this method either throws an absl::BadStatusOrAccess exception or aborts the program, depending on whether exceptions are enabled.

Returns
The stored T value.

Friends And Related Function Documentation

◆ StatusOr

template<class T>
template<typename U >
friend class StatusOr
friend

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