Struct: TResult
template <typename T, typename E>
struct TResult;
Result type that can wrap either T value or E error data.
Useful for return types in functions that can return either valid value or
some error. Given implementation tries to match TOptional API as much as
possible.
Example
UENUM()
enum class EGuessError : uint8
{
TooHigh,
TooLow,
};
USTRUCT()
struct FShiaTheSecretKeeper
{
GENERATED_BODY()
public:
FShiaTheSecretKeeper() : Secret(), Password()
{
}
FShiaTheSecretKeeper(FString InSecret, int InPassword) : Secret(InSecret), Password(InPassword)
{
}
TResult<FString, EGuessError> TryGuess(int GuessPassword)
{
if (GuessPassword == this->Password)
{
return TResult<FString, EGuessError>(this->Secret);
}
else if (GuessPassword < this->Password)
{
return TResult<FString, EGuessError>(EGuessError::TooLow);
}
else
{
return TResult<FString, EGuessError>(EGuessError::TooHigh);
}
}
private:
FString Secret = {};
int Password = {};
};
int Main()
{
const auto Shia = FShiaTheSecretKeeper("Just do it!", 42);
const auto Result = Shia.TryGuess(42);
if (Result.IsOk())
{
UE_LOG(LogTemp, Info, TEXT("Guessed! Secret: %s"), *Result.GetOk());
}
else
{
switch (Result.GetError())
{
case EGuessError::TooHigh:
UE_LOG(LogTemp, Error, TEXT("Too high!"));
break;
case EGuessError::TooLow:
UE_LOG(LogTemp, Error, TEXT("Too low!"));
break;
}
}
}
Methods
-
AsErrorpublic: TOptional<E> AsError() const;
Returns option with cloned error data.
In case of result wrapping value data, it returns none.
-
AsOkpublic: TOptional<T> AsOk() const;
Returns option with cloned value data.
In case of result wrapping error data, it returns none.
-
GetErrorpublic: E& GetError(); -
GetErrorpublic: const E& GetError() const; -
GetOkpublic: T& GetOk(); -
GetOkpublic: const T& GetOk() const; -
IsErrorpublic: bool IsError() const;
Tells if result wraps error data.
-
IsOkpublic: bool IsOk() const;
Tells if result wraps value data.
-
SetErrorpublic: void SetError( const E& Error ); -
SetErrorpublic: void SetError( E&& Error ); -
SetOkpublic: void SetOk( const T& Value ); -
SetOkpublic: void SetOk( T&& Value ); -
TResultpublic: TResult( const T& Value ); -
TResultpublic: TResult( T&& Value ); -
TResultpublic: TResult( const E& Error ); -
TResultpublic: TResult( E&& Error ); -
TResultpublic: TResult( const TResult& Other ); -
TResultpublic: TResult( TResult&& Other ); -
operator boolpublic: explicit operator bool() const; -
operator!=public: bool operator!=( const TResult& Lhs, const TResult& Rhs ); -
operator<<public: FArchive& operator<<( FArchive& Ar, TResult& Result ); -
operator=public: TResult& operator=( const TResult& Other ); -
operator=public: TResult& operator=( TResult&& Other ); -
operator=public: TResult& operator=( const T& Value ); -
operator=public: TResult& operator=( T&& Value ); -
operator=public: TResult& operator=( const E& Error ); -
operator=public: TResult& operator=( E&& Error ); -
operator==public: bool operator==( const TResult& Lhs, const TResult& Rhs );
Documentation built with Unreal-Doc v1.0.8 tool by PsichiX