Oboe  1.5
A library for creating real-time audio apps on Android
Public Member Functions | Static Public Member Functions | List of all members
oboe::ResultWithValue< T > Class Template Reference

#include <ResultWithValue.h>

Public Member Functions

 ResultWithValue (oboe::Result error)
 
 ResultWithValue (T value)
 
oboe::Result error () const
 
value () const
 
 operator bool () const
 
bool operator ! () const
 
 operator Result () const
 

Static Public Member Functions

static ResultWithValue< T > createBasedOnSign (T numericResult)
 

Detailed Description

template<typename T>
class oboe::ResultWithValue< T >

A ResultWithValue can store both the result of an operation (either OK or an error) and a value.

It has been designed for cases where the caller needs to know whether an operation succeeded and, if it did, a value which was obtained during the operation.

For example, when reading from a stream the caller needs to know the result of the read operation and, if it was successful, how many frames were read. Note that ResultWithValue can be evaluated as a boolean so it's simple to check whether the result is OK.

ResultWithValue<int32_t> resultOfRead = myStream.read(&buffer, numFrames, timeoutNanoseconds);

if (resultOfRead) { LOGD("Frames read: %d", resultOfRead.value()); } else { LOGD("Error reading from stream: %s", resultOfRead.error()); }

Constructor & Destructor Documentation

◆ ResultWithValue() [1/2]

template<typename T>
oboe::ResultWithValue< T >::ResultWithValue ( oboe::Result  error)
inline

Construct a ResultWithValue containing an error result.

Parameters
errorThe error

◆ ResultWithValue() [2/2]

template<typename T>
oboe::ResultWithValue< T >::ResultWithValue ( value)
inlineexplicit

Construct a ResultWithValue containing an OK result and a value.

Parameters
valuethe value to store

Member Function Documentation

◆ createBasedOnSign()

template<typename T>
static ResultWithValue<T> oboe::ResultWithValue< T >::createBasedOnSign ( numericResult)
inlinestatic

Create a ResultWithValue from a number. If the number is positive the ResultWithValue will have a result of Result::OK and the value will contain the number. If the number is negative the result will be obtained from the negative number (numeric error codes can be found in AAudio.h) and the value will be null.

◆ error()

template<typename T>
oboe::Result oboe::ResultWithValue< T >::error ( ) const
inline

Get the result.

Returns
the result

◆ operator !()

template<typename T>
bool oboe::ResultWithValue< T >::operator ! ( ) const
inline

Quick way to check for an error.

The caller could write something like this: if (!result) { printf("Got error %s\n", convertToText(result.error())); }

Returns
true if an error occurred

◆ operator bool()

template<typename T>
oboe::ResultWithValue< T >::operator bool ( ) const
inlineexplicit
Returns
true if OK

◆ operator Result()

template<typename T>
oboe::ResultWithValue< T >::operator Result ( ) const
inline

Implicitly convert to a Result. This enables easy comparison with Result values. Example:

ResultWithValue result = openStream(); if (result == Result::ErrorNoMemory){ // tell user they're out of memory }

◆ value()

template<typename T>
T oboe::ResultWithValue< T >::value ( ) const
inline

Get the value

Returns

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