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
-
AsError
public: TOptional<E> AsError() const;
Returns option with cloned error data.
In case of result wrapping value data, it returns none.
-
AsOk
public: TOptional<T> AsOk() const;
Returns option with cloned value data.
In case of result wrapping error data, it returns none.
-
GetError
public: E& GetError();
-
GetError
public: const E& GetError() const;
-
GetOk
public: T& GetOk();
-
GetOk
public: const T& GetOk() const;
-
IsError
public: bool IsError() const;
Tells if result wraps error data.
-
IsOk
public: bool IsOk() const;
Tells if result wraps value data.
-
SetError
public: void SetError( const E& Error );
-
SetError
public: void SetError( E&& Error );
-
SetOk
public: void SetOk( const T& Value );
-
SetOk
public: void SetOk( T&& Value );
-
TResult
public: TResult( const T& Value );
-
TResult
public: TResult( T&& Value );
-
TResult
public: TResult( const E& Error );
-
TResult
public: TResult( E&& Error );
-
TResult
public: TResult( const TResult& Other );
-
TResult
public: TResult( TResult&& Other );
-
operator bool
public: 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