Path: blob/master/thirdparty/jolt_physics/Jolt/Core/Result.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56JPH_NAMESPACE_BEGIN78/// Helper class that either contains a valid result or an error9template <class Type>10class Result11{12public:13/// Default constructor14Result() { }1516/// Copy constructor17Result(const Result<Type> &inRHS) :18mState(inRHS.mState)19{20switch (inRHS.mState)21{22case EState::Valid:23new (&mResult) Type (inRHS.mResult);24break;2526case EState::Error:27new (&mError) String(inRHS.mError);28break;2930case EState::Invalid:31break;32}33}3435/// Move constructor36Result(Result<Type> &&inRHS) noexcept :37mState(inRHS.mState)38{39switch (inRHS.mState)40{41case EState::Valid:42new (&mResult) Type (std::move(inRHS.mResult));43break;4445case EState::Error:46new (&mError) String(std::move(inRHS.mError));47break;4849case EState::Invalid:50break;51}5253// Don't reset the state of inRHS, the destructors still need to be called after a move operation54}5556/// Destructor57~Result() { Clear(); }5859/// Copy assignment60Result<Type> & operator = (const Result<Type> &inRHS)61{62Clear();6364mState = inRHS.mState;6566switch (inRHS.mState)67{68case EState::Valid:69new (&mResult) Type (inRHS.mResult);70break;7172case EState::Error:73new (&mError) String(inRHS.mError);74break;7576case EState::Invalid:77break;78}7980return *this;81}8283/// Move assignment84Result<Type> & operator = (Result<Type> &&inRHS) noexcept85{86Clear();8788mState = inRHS.mState;8990switch (inRHS.mState)91{92case EState::Valid:93new (&mResult) Type (std::move(inRHS.mResult));94break;9596case EState::Error:97new (&mError) String(std::move(inRHS.mError));98break;99100case EState::Invalid:101break;102}103104// Don't reset the state of inRHS, the destructors still need to be called after a move operation105106return *this;107}108109/// Clear result or error110void Clear()111{112switch (mState)113{114case EState::Valid:115mResult.~Type();116break;117118case EState::Error:119mError.~String();120break;121122case EState::Invalid:123break;124}125126mState = EState::Invalid;127}128129/// Checks if the result is still uninitialized130bool IsEmpty() const { return mState == EState::Invalid; }131132/// Checks if the result is valid133bool IsValid() const { return mState == EState::Valid; }134135/// Get the result value136const Type & Get() const { JPH_ASSERT(IsValid()); return mResult; }137138/// Set the result value139void Set(const Type &inResult) { Clear(); new (&mResult) Type(inResult); mState = EState::Valid; }140141/// Set the result value (move value)142void Set(Type &&inResult) { Clear(); new (&mResult) Type(std::move(inResult)); mState = EState::Valid; }143144/// Check if we had an error145bool HasError() const { return mState == EState::Error; }146147/// Get the error value148const String & GetError() const { JPH_ASSERT(HasError()); return mError; }149150/// Set an error value151void SetError(const char *inError) { Clear(); new (&mError) String(inError); mState = EState::Error; }152void SetError(const string_view &inError) { Clear(); new (&mError) String(inError); mState = EState::Error; }153void SetError(String &&inError) { Clear(); new (&mError) String(std::move(inError)); mState = EState::Error; }154155private:156union157{158Type mResult; ///< The actual result object159String mError; ///< The error description if the result failed160};161162/// State of the result163enum class EState : uint8164{165Invalid,166Valid,167Error168};169170EState mState = EState::Invalid;171};172173JPH_NAMESPACE_END174175176