Raven Engine v0.1
Loading...
Searching...
No Matches
Raven::Expected< T > Class Template Reference

Template representing either a valid value or an error. More...

#include <Expected.h>

Public Types

using StoredType = std::conditional_t<std::is_reference_v<T>, std::remove_reference_t<T>*, T>
 Alias for internal storage: value or pointer-to-value for references.

Public Member Functions

constexpr Expected (std::source_location loc=std::source_location::current())
 Constructs a default fatal-error Expected, logs error.
constexpr Expected (ErrorCode error, const char *message="", std::source_location loc=std::source_location::current())
 Constructs an error Expected with given code and message, logs error.
template<typename U = T, typename = std::enable_if_t<!std::is_reference_v<U>>>
constexpr Expected (U value, std::source_location loc=std::source_location::current())
 Constructs a successful Expected holding a value (value types only).
template<typename U = T, typename = std::enable_if_t<std::is_reference_v<U>>>
constexpr Expected (std::remove_reference_t< T > &value, std::source_location loc=std::source_location::current())
 Constructs a successful Expected holding a reference (reference types only).
constexpr Expected (Expected &&other) noexcept
 Move constructor, leaves source in OBJECT_MOVED state.
constexpr Expectedoperator= (Expected &&other) noexcept
 Move assignment, leaves source in OBJECT_MOVED state.
 Expected (const Expected &)=delete
Expectedoperator= (const Expected &)=delete
constexpr bool IsSuccess () const noexcept
 Returns true if the Expected holds a success code.
constexpr bool IsFailure () const noexcept
 Returns true if the Expected holds an error.
void Terminate () const
 Closes the application immediately.
constexpr auto Value () -> std::conditional_t< std::is_reference_v< T >, std::remove_reference_t< T > &, T & >
 Accesses the stored value (lvalue reference for references, value for value types).
constexpr auto Value () const -> std::conditional_t< std::is_reference_v< T >, const std::remove_reference_t< T > &, const T & >
 Const overload of Value().
constexpr ErrorCode Error () const
 Gets the stored error code.
constexpr std::string_view ErrorToString () const
 Gets the error code as a string.
constexpr const char * Message () const
 Gets the error message.
constexpr const char * Function () const
 Gets the function name where this Expected was created.

Detailed Description

template<typename T>
class Raven::Expected< T >

Template representing either a valid value or an error.

Template Parameters
TThe type of the expected value, or a reference type.

The Expected<T> class encapsulates either a successfully produced value of type T or an error condition. It supports move-only semantics, automatic logging of errors on construction, and integration with the Application lifecycle (Terminate on failure). Error codes are represented by the ErrorCode enum. Reference types and value types are both supported via the StoredType alias. Use IsSuccess()/IsFailure() to branch logic, and Value() to access the contained value. On failure, a critical log is emitted.

Member Typedef Documentation

◆ StoredType

template<typename T>
using Raven::Expected< T >::StoredType = std::conditional_t<std::is_reference_v<T>, std::remove_reference_t<T>*, T>

Alias for internal storage: value or pointer-to-value for references.

Constructor & Destructor Documentation

◆ Expected() [1/6]

template<typename T>
Raven::Expected< T >::Expected ( std::source_location loc = std::source_location::current())
inlineexplicitconstexpr

Constructs a default fatal-error Expected, logs error.

Parameters
locSource location of the call (auto-filled).

◆ Expected() [2/6]

template<typename T>
Raven::Expected< T >::Expected ( ErrorCode error,
const char * message = "",
std::source_location loc = std::source_location::current() )
inlineconstexpr

Constructs an error Expected with given code and message, logs error.

Parameters
errorThe ErrorCode to store.
messageOptional message describing the error.
locSource location of the call (auto-filled).

◆ Expected() [3/6]

template<typename T>
template<typename U = T, typename = std::enable_if_t<!std::is_reference_v<U>>>
Raven::Expected< T >::Expected ( U value,
std::source_location loc = std::source_location::current() )
inlineconstexpr

Constructs a successful Expected holding a value (value types only).

Parameters
valueThe value to store.
locSource location of the call (auto-filled).

◆ Expected() [4/6]

template<typename T>
template<typename U = T, typename = std::enable_if_t<std::is_reference_v<U>>>
Raven::Expected< T >::Expected ( std::remove_reference_t< T > & value,
std::source_location loc = std::source_location::current() )
inlineconstexpr

Constructs a successful Expected holding a reference (reference types only).

Parameters
valueThe referenced object.
locSource location of the call (auto-filled).

◆ Expected() [5/6]

template<typename T>
Raven::Expected< T >::Expected ( Expected< T > && other)
inlineconstexprnoexcept

Move constructor, leaves source in OBJECT_MOVED state.

◆ Expected() [6/6]

template<typename T>
Raven::Expected< T >::Expected ( const Expected< T > & )
delete

Member Function Documentation

◆ Error()

template<typename T>
ErrorCode Raven::Expected< T >::Error ( ) const
inlineconstexpr

Gets the stored error code.

◆ ErrorToString()

template<typename T>
std::string_view Raven::Expected< T >::ErrorToString ( ) const
inlineconstexpr

Gets the error code as a string.

◆ Function()

template<typename T>
const char * Raven::Expected< T >::Function ( ) const
inlineconstexpr

Gets the function name where this Expected was created.

◆ IsFailure()

template<typename T>
bool Raven::Expected< T >::IsFailure ( ) const
inlineconstexprnoexcept

Returns true if the Expected holds an error.

◆ IsSuccess()

template<typename T>
bool Raven::Expected< T >::IsSuccess ( ) const
inlineconstexprnoexcept

Returns true if the Expected holds a success code.

◆ Message()

template<typename T>
const char * Raven::Expected< T >::Message ( ) const
inlineconstexpr

Gets the error message.

◆ operator=() [1/2]

template<typename T>
Expected & Raven::Expected< T >::operator= ( const Expected< T > & )
delete

◆ operator=() [2/2]

template<typename T>
Expected & Raven::Expected< T >::operator= ( Expected< T > && other)
inlineconstexprnoexcept

Move assignment, leaves source in OBJECT_MOVED state.

◆ Terminate()

template<typename T>
void Raven::Expected< T >::Terminate ( ) const
inline

Closes the application immediately.

◆ Value() [1/2]

template<typename T>
auto Raven::Expected< T >::Value ( ) -> std::conditional_t<std::is_reference_v<T>, std::remove_reference_t<T>&, T&>
inlineconstexpr

Accesses the stored value (lvalue reference for references, value for value types).

Returns
The contained value or reference.

◆ Value() [2/2]

template<typename T>
auto Raven::Expected< T >::Value ( ) const -> std::conditional_t<std::is_reference_v<T>, const std::remove_reference_t<T>&, const T&>
inlineconstexpr

Const overload of Value().


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