Contur 2
Educational OS kernel simulator
Loading...
Searching...
No Matches

A result type that holds either a success value of type T or an ErrorCode. More...

#include <error.h>

Public Member Functions

bool isOk () const noexcept
 Returns true if this Result holds a success value.
bool isError () const noexcept
 Returns true if this Result holds an error code.
const T & value () const &
 Returns a const reference to the success value.
T && value () &&
 Returns an rvalue reference to the success value (move semantics).
T & value () &
 Returns a mutable reference to the success value.
ErrorCode errorCode () const noexcept
 Returns the error code.
valueOr (T defaultValue) const &
 Returns the value if ok, or the provided default value if error.

Static Public Member Functions

static Result ok (T value)
 Constructs a successful Result containing the given value.
static Result error (ErrorCode code)
 Constructs a failed Result with the given error code.

Private Member Functions

 Result (T value)
 Result (ErrorCode code)

Private Attributes

std::variant< T, ErrorCodestorage_

Detailed Description

template<typename T>
class contur::Result< T >

A result type that holds either a success value of type T or an ErrorCode.

Inspired by Rust's Result<T, E>. Use the static factory methods ok() and error() to construct instances. Check isOk() / isError() before accessing the contained value or error code.

Template Parameters
TThe success value type.

Definition at line 103 of file error.h.

Constructor & Destructor Documentation

◆ Result() [1/2]

template<typename T>
contur::Result< T >::Result ( T value)
inlineexplicitprivate

Definition at line 174 of file error.h.

References storage_, and value().

Referenced by error(), and ok().

◆ Result() [2/2]

template<typename T>
contur::Result< T >::Result ( ErrorCode code)
inlineexplicitprivate

Definition at line 177 of file error.h.

References storage_.

Member Function Documentation

◆ error()

template<typename T>
Result contur::Result< T >::error ( ErrorCode code)
inlinestaticnodiscard

Constructs a failed Result with the given error code.

Definition at line 113 of file error.h.

References contur::Ok, and Result().

Referenced by contur::parseRegisterValue(), contur::readFileValue(), contur::validateCommand(), contur::validatePlaybackConfig(), and contur::writeFileValue().

◆ errorCode()

template<typename T>
ErrorCode contur::Result< T >::errorCode ( ) const
inlinenodiscardnoexcept

Returns the error code.

Precondition
isError() must be true. Undefined behavior otherwise.

Definition at line 157 of file error.h.

References isError(), and storage_.

◆ isError()

template<typename T>
bool contur::Result< T >::isError ( ) const
inlinenodiscardnoexcept

Returns true if this Result holds an error code.

Definition at line 126 of file error.h.

References storage_.

Referenced by errorCode().

◆ isOk()

template<typename T>
bool contur::Result< T >::isOk ( ) const
inlinenodiscardnoexcept

Returns true if this Result holds a success value.

Definition at line 120 of file error.h.

References storage_.

Referenced by value(), value(), value(), and valueOr().

◆ ok()

template<typename T>
Result contur::Result< T >::ok ( T value)
inlinestaticnodiscard

Constructs a successful Result containing the given value.

Definition at line 107 of file error.h.

References Result(), and value().

Referenced by contur::parseRegisterValue(), contur::validateCommand(), contur::validatePlaybackConfig(), and contur::writeFileValue().

◆ value() [1/3]

template<typename T>
T & contur::Result< T >::value ( ) &
inlinenodiscard

Returns a mutable reference to the success value.

Precondition
isOk() must be true.

Definition at line 149 of file error.h.

References isOk(), and storage_.

◆ value() [2/3]

template<typename T>
T && contur::Result< T >::value ( ) &&
inlinenodiscard

Returns an rvalue reference to the success value (move semantics).

Precondition
isOk() must be true.

Definition at line 141 of file error.h.

References isOk(), and storage_.

◆ value() [3/3]

template<typename T>
const T & contur::Result< T >::value ( ) const &
inlinenodiscard

Returns a const reference to the success value.

Precondition
isOk() must be true. Undefined behavior otherwise.

Definition at line 133 of file error.h.

References isOk(), and storage_.

Referenced by ok(), and Result().

◆ valueOr()

template<typename T>
T contur::Result< T >::valueOr ( T defaultValue) const &
inlinenodiscard

Returns the value if ok, or the provided default value if error.

Definition at line 164 of file error.h.

References isOk(), and storage_.

Member Data Documentation

◆ storage_

template<typename T>
std::variant<T, ErrorCode> contur::Result< T >::storage_
private

Definition at line 181 of file error.h.

Referenced by errorCode(), isError(), isOk(), Result(), Result(), value(), value(), value(), and valueOr().


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