litert::Expected

#include <litert_expected.h>

A utility for generic return values that may represent a failure.

Summary

Expected stores and owns the lifetime of either an Unexpected object or a value of type T. T can be any primitive or non-primitive type.

No dynamic allocations occur during initialization, so the underlying T is only movable (as opposed to being releasable). Arguments should be constructed in-place when initializing the Expected object if possible.

Unexpected&& and T&& can be implicitly cast to an Expected. For example:

Expected Bar() {
  bool success = ...
  if (!success) {
    return Unexpected(kLiteRtStatus, "Bad Baz");
  }
  return Foo();
}

Constructors and Destructors

Expected(std::initializer_list< U > il)
Constructs T from an initializer list in-place.
Expected(Args &&... args)
Constructs T from forwarded arguments in-place.
Expected(reference t)
Allows for implicit conversion from a convertible T value in-place.
Expected(const U & t)
Copy-constructs from a constant reference.
Expected(value_type && t)
Expected(const Unexpected & err)
Constructs from an Unexpected object in-place.
Expected(Unexpected && err)
Expected(const class Error & e)
Expected(Expected && other)
Expected(const Expected & other)
~Expected()

Public types

StorageType using
std::conditional_t< std::is_reference_v< T >, std::reference_wrapper< std::remove_reference_t< T >>, T >
const_pointer using
const value_type *
const_reference using
const value_type &
pointer using
std::remove_reference_t< T > *
reference using
std::remove_reference_t< T > &
value_type using
std::decay_t< T >
The following type definitions are in snake_case to match standard member types.

Public attributes

unexpected_
value_
StorageType

Public functions

Error() const &
const class Error &
Observer for Unexpected.
Error()&
class Error &
Error() const &&=delete
const class Error &&
Deleted: an Expected should always be checked before accessing its error.
Error()&&=delete
class Error &&
Deleted: an Expected should always be checked before accessing its error.
HasValue() const
bool
Checks if this Expected contains a T value.
Value() const &
const_reference
Observers for the T value.
Value()&
reference
Value() const &&=delete
reference &
Deleted: an Expected should always be checked before accessing its value.
Value()&&=delete
reference &
Deleted: an Expected should always be checked before accessing its value.
operator bool() const
Converts to bool for HasValue.
operator*() const &
const_reference
operator*()&
reference
operator*() const &&=delete
reference &
Deleted: an Expected should always be checked before accessing its value.
operator*()&&=delete
reference &
Deleted: an Expected should always be checked before accessing its value.
operator->() const &
const_pointer
operator->()&
pointer
operator->() const &&=delete
const_pointer
Deleted: an Expected should always be checked before accessing its value.
operator->()&&=delete
pointer
Deleted: an Expected should always be checked before accessing its value.
operator=(Expected && other)
operator=(const Expected & other)

Public types

StorageType

std::conditional_t< std::is_reference_v< T >, std::reference_wrapper< std::remove_reference_t< T >>, T > StorageType

const_pointer

const value_type * const_pointer

const_reference

const value_type & const_reference

pointer

std::remove_reference_t< T > * pointer

reference

std::remove_reference_t< T > & reference

value_type

std::decay_t< T > value_type

The following type definitions are in snake_case to match standard member types.

Public attributes

unexpected_

Unexpected unexpected_

value_

StorageType value_

Public functions

Error

const class Error & Error() const &

Observer for Unexpected.

The program exits if it doesn't have one.

Error

class Error & Error()&

Error

const class Error && Error() const &&=delete

Deleted: an Expected should always be checked before accessing its error.

Error

class Error && Error()&&=delete

Deleted: an Expected should always be checked before accessing its error.

Expected

 Expected(
  std::initializer_list< U > il
)

Constructs T from an initializer list in-place.

Expected

 Expected(
  Args &&... args
)

Constructs T from forwarded arguments in-place.

Expected

 Expected(
  reference t
)

Allows for implicit conversion from a convertible T value in-place.

Expected

 Expected(
  const U & t
)

Copy-constructs from a constant reference.

This is disabled if T is a constant reference.

Expected

 Expected(
  value_type && t
)

Expected

 Expected(
  const Unexpected & err
)

Constructs from an Unexpected object in-place.

Allows for implicit conversion from Error.

Expected

 Expected(
  Unexpected && err
)

Expected

 Expected(
  const class Error & e
)

Expected

 Expected(
  Expected && other
)

Expected

 Expected(
  const Expected & other
)

HasValue

bool HasValue() const 

Checks if this Expected contains a T value.

If not, it contains an Unexpected.

Value

const_reference Value() const &

Observers for the T value.

The program exits if it doesn't have one.

Value

reference Value()&

Value

reference & Value() const &&=delete

Deleted: an Expected should always be checked before accessing its value.

Value

reference & Value()&&=delete

Deleted: an Expected should always be checked before accessing its value.

operator bool

 operator bool() const 

Converts to bool for HasValue.

operator*

const_reference operator*() const &

operator*

reference operator*()&

operator*

reference & operator*() const &&=delete

Deleted: an Expected should always be checked before accessing its value.

operator*

reference & operator*()&&=delete

Deleted: an Expected should always be checked before accessing its value.

operator->

const_pointer operator->() const &

operator->

pointer operator->()&

operator->

const_pointer operator->() const &&=delete

Deleted: an Expected should always be checked before accessing its value.

operator->

pointer operator->()&&=delete

Deleted: an Expected should always be checked before accessing its value.

operator=

Expected & operator=(
  Expected && other
)

operator=

Expected & operator=(
  const Expected & other
)

~Expected

 ~Expected()