12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- /*******************************************************************************
- The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
- Technology released in source code form as part of the game integration package.
- The content of this file may not be used without valid licenses to the
- AUDIOKINETIC Wwise Technology.
- Note that the use of the game engine is subject to the Unreal(R) Engine End User
- License Agreement at https://www.unrealengine.com/en-US/eula/unreal
-
- License Usage
-
- Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
- this file in accordance with the end user license agreement provided with the
- software or, alternatively, in accordance with the terms contained
- in a written agreement between you and Audiokinetic Inc.
- Copyright (c) 2023 Audiokinetic Inc.
- *******************************************************************************/
- #pragma once
- #include "CoreTypes.h"
- #include "Misc/AssertionMacros.h"
- #include "Templates/UnrealTemplate.h"
- #include "Templates/Function.h"
- #include "Misc/Timespan.h"
- #include "Templates/SharedPointer.h"
- #include "Misc/DateTime.h"
- #include "HAL/Event.h"
- #include "Wwise/Stats/Concurrency.h"
- #include "Wwise/Stats/AsyncStats.h"
- #include <atomic>
- //
- // WwiseFuture is a replica of Unreal's Async/Future.h code, with optimizations on the FutureState to reduce the
- // amount of FCriticalSection and FEvent to the bare minimum. Most of Wwise Integrations code uses the "Then" paradigm
- // of Futures/Promises instead of the Wait paradigm.
- //
- /**
- * Base class for the internal state of asynchronous return values (futures).
- */
- class FWwiseFutureState
- {
- public:
- /** Default constructor. */
- FWwiseFutureState()
- : CompletionCallback(nullptr)
- , Complete(false)
- {
- ASYNC_INC_DWORD_STAT(STAT_WwiseFutures);
- }
- /**
- * Create and initialize a new instance with a callback.
- *
- * @param InCompletionCallback A function that is called when the state is completed.
- */
- FWwiseFutureState(TUniqueFunction<void()>&& InCompletionCallback)
- : CompletionCallback(InCompletionCallback ? new TUniqueFunction<void()>(MoveTemp(InCompletionCallback)) : nullptr)
- , Complete(false)
- {
- ASYNC_INC_DWORD_STAT(STAT_WwiseFutures);
- }
- /** Destructor. */
- ~FWwiseFutureState()
- {
- const auto* Continuation = CompletionCallback.exchange(nullptr);
- check(!Continuation);
- if (UNLIKELY(Continuation))
- {
- delete Continuation;
- }
- ASYNC_DEC_DWORD_STAT(STAT_WwiseFutures);
- }
- public:
- /**
- * Checks whether the asynchronous result has been set.
- *
- * @return true if the result has been set, false otherwise.
- * @see WaitFor
- */
- bool IsComplete() const
- {
- return Complete.load(std::memory_order_seq_cst);
- }
- /**
- * Blocks the calling thread until the future result is available.
- *
- * Compared to Unreal's native version, this version uses continuation, and assumes you haven't set a continuation
- * function on this state.
- *
- * @param Duration The maximum time span to wait for the future result.
- * @return true if the result is available, false otherwise.
- * @see IsComplete
- */
- bool WaitFor(const FTimespan& Duration)
- {
- check(!CompletionCallback.load(std::memory_order_seq_cst));
-
- if (IsComplete())
- {
- return true;
- }
- auto CompletionEvent = MakeShared<FEventRef, ESPMode::ThreadSafe>();
- SetContinuation([CompletionEvent]
- {
- CompletionEvent.Get()->Trigger();
- });
- return CompletionEvent.Get()->Wait(Duration);
- }
- /**
- * Set a continuation to be called on completion of the promise
- * @param Continuation
- */
- void SetContinuation(TUniqueFunction<void()>&& Continuation)
- {
- if (IsComplete())
- {
- if (Continuation)
- {
- Continuation();
- }
- return;
- }
- // Store the Copy to the CompletionCallback
- auto Copy = Continuation ? new TUniqueFunction<void()>(MoveTemp(Continuation)) : nullptr;
- auto OldCopy = CompletionCallback.exchange(Copy);
- check(!OldCopy); // We can only execute one continuation per WwiseFuture.
- if (!IsComplete())
- {
- return;
- }
- // We are already complete. See if we need to execute ourselves.
- Copy = CompletionCallback.exchange(nullptr);
- if (Copy)
- {
- (*Copy)();
- delete Copy;
- }
- }
- protected:
- /** Notifies any waiting threads that the result is available. */
- void MarkComplete()
- {
- Complete.store(true, std::memory_order_seq_cst);
- auto* Continuation = CompletionCallback.exchange(nullptr);
- if (Continuation)
- {
- (*Continuation)();
- delete Continuation;
- }
- }
- private:
- /** An optional callback function that is executed the state is completed. */
- std::atomic< TUniqueFunction<void()>* > CompletionCallback;
- /** Whether the asynchronous result is available. */
- std::atomic<bool> Complete;
- };
- /**
- * Implements the internal state of asynchronous return values (futures).
- */
- template<typename InternalResultType>
- class TWwiseFutureState
- : public FWwiseFutureState
- {
- public:
- /** Default constructor. */
- TWwiseFutureState()
- : FWwiseFutureState()
- { }
- ~TWwiseFutureState()
- {
- if (IsComplete())
- {
- DestructItem(Result.GetTypedPtr());
- }
- }
- /**
- * Create and initialize a new instance with a callback.
- *
- * @param CompletionCallback A function that is called when the state is completed.
- */
- TWwiseFutureState(TUniqueFunction<void()>&& CompletionCallback)
- : FWwiseFutureState(MoveTemp(CompletionCallback))
- { }
- public:
- /**
- * Gets the result (will block the calling thread until the result is available).
- *
- * @return The result value.
- * @see EmplaceResult
- */
- const InternalResultType& GetResult()
- {
- while (!IsComplete())
- {
- WaitFor(FTimespan::MaxValue());
- }
- return *Result.GetTypedPtr();
- }
- /**
- * Sets the result and notifies any waiting threads.
- *
- * @param InResult The result to set.
- * @see GetResult
- */
- template<typename... ArgTypes>
- void EmplaceResult(ArgTypes&&... Args)
- {
- check(!IsComplete());
- new(Result.GetTypedPtr()) InternalResultType(Forward<ArgTypes>(Args)...);
- MarkComplete();
- }
- private:
- /** Holds the asynchronous result. */
- TTypeCompatibleBytes<InternalResultType> Result;
- };
- /* TWwiseFuture
- *****************************************************************************/
- /**
- * Abstract base template for futures and shared futures.
- */
- template<typename InternalResultType>
- class TWwiseFutureBase
- {
- public:
- /**
- * Checks whether this future object has its value set.
- *
- * @return true if this future has a shared state and the value has been set, false otherwise.
- * @see IsValid
- */
- bool IsReady() const
- {
- return State.IsValid() ? State->IsComplete() : false;
- }
- /**
- * Checks whether this future object has a valid state.
- *
- * @return true if the state is valid, false otherwise.
- * @see IsReady
- */
- bool IsValid() const
- {
- return State.IsValid();
- }
- /**
- * Blocks the calling thread until the future result is available.
- *
- * Note that this method may block forever if the result is never set. Use
- * the WaitFor or WaitUntil methods to specify a maximum timeout for the wait.
- *
- * @see WaitFor, WaitUntil
- */
- void Wait() const
- {
- if (State.IsValid())
- {
- while (!WaitFor(FTimespan::MaxValue()));
- }
- }
- /**
- * Blocks the calling thread until the future result is available or the specified duration is exceeded.
- *
- * @param Duration The maximum time span to wait for the future result.
- * @return true if the result is available, false otherwise.
- * @see Wait, WaitUntil
- */
- bool WaitFor(const FTimespan& Duration) const
- {
- return State.IsValid() ? State->WaitFor(Duration) : false;
- }
- /**
- * Blocks the calling thread until the future result is available or the specified time is hit.
- *
- * @param Time The time until to wait for the future result (in UTC).
- * @return true if the result is available, false otherwise.
- * @see Wait, WaitUntil
- */
- bool WaitUntil(const FDateTime& Time) const
- {
- return WaitFor(Time - FDateTime::UtcNow());
- }
- protected:
- typedef TSharedPtr<TWwiseFutureState<InternalResultType>, ESPMode::ThreadSafe> StateType;
- /** Default constructor. */
- TWwiseFutureBase() { }
- /**
- * Creates and initializes a new instance.
- *
- * @param InState The shared state to initialize with.
- */
- TWwiseFutureBase(const StateType& InState)
- : State(InState)
- { }
- /**
- * Protected move construction
- */
- TWwiseFutureBase(TWwiseFutureBase&&) = default;
- /**
- * Protected move assignment
- */
- TWwiseFutureBase& operator=(TWwiseFutureBase&&) = default;
- /**
- * Protected copy construction
- */
- TWwiseFutureBase(const TWwiseFutureBase&) = default;
- /**
- * Protected copy assignment
- */
- TWwiseFutureBase& operator=(const TWwiseFutureBase&) = default;
- /** Protected destructor. */
- ~TWwiseFutureBase() { }
- protected:
- /**
- * Gets the shared state object.
- *
- * @return The shared state object.
- */
- const StateType& GetState() const
- {
- // if you hit this assertion then your future has an invalid state.
- // this happens if you have an uninitialized future or if you moved
- // it to another instance.
- check(State.IsValid());
- return State;
- }
- /**
- * Set a completion callback that will be called once the future completes
- * or immediately if already completed
- *
- * @param Continuation a continuation taking an argument of type TWwiseFuture<InternalResultType>
- * @return nothing at the moment but could return another future to allow future chaining
- */
- template<typename Func>
- auto Then(Func Continuation);
- /**
- * Convenience wrapper for Then that
- * set a completion callback that will be called once the future completes
- * or immediately if already completed
- * @param Continuation a continuation taking an argument of type InternalResultType
- * @return nothing at the moment but could return another future to allow future chaining
- */
- template<typename Func>
- auto Next(Func Continuation);
- /**
- * Reset the future.
- * Resetting a future removes any continuation from its shared state and invalidates it.
- * Useful for discarding yet to be completed future cleanly.
- */
- void Reset()
- {
- if (IsValid())
- {
- this->State->SetContinuation(nullptr);
- this->State.Reset();
- }
- }
- private:
- /** Holds the future's state. */
- StateType State;
- };
- /**
- * Template for unshared futures.
- */
- template<typename ResultType>
- class TWwiseFuture
- : public TWwiseFutureBase<ResultType>
- {
- typedef TWwiseFutureBase<ResultType> BaseType;
- public:
- /** Default constructor. */
- TWwiseFuture() { }
- /**
- * Creates and initializes a new instance.
- *
- * @param InState The shared state to initialize with.
- */
- TWwiseFuture(const typename BaseType::StateType& InState)
- : BaseType(InState)
- { }
- /**
- * Move constructor.
- */
- TWwiseFuture(TWwiseFuture&&) = default;
- /**
- * Move assignment operator.
- */
- TWwiseFuture& operator=(TWwiseFuture&& Other) = default;
- /** Destructor. */
- ~TWwiseFuture() { }
- public:
- /**
- * Gets the future's result.
- *
- * @return The result.
- */
- ResultType Get() const
- {
- return this->GetState()->GetResult();
- }
- /**
- * Expose Then functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Then;
- /**
- * Expose Next functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Next;
- /**
- * Expose Reset functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Reset;
- private:
- /** Hidden copy constructor (futures cannot be copied). */
- TWwiseFuture(const TWwiseFuture&);
- /** Hidden copy assignment (futures cannot be copied). */
- TWwiseFuture& operator=(const TWwiseFuture&);
- };
- /**
- * Template for unshared futures (specialization for reference types).
- */
- template<typename ResultType>
- class TWwiseFuture<ResultType&>
- : public TWwiseFutureBase<ResultType*>
- {
- typedef TWwiseFutureBase<ResultType*> BaseType;
- public:
- /** Default constructor. */
- TWwiseFuture() { }
- /**
- * Creates and initializes a new instance.
- *
- * @param InState The shared state to initialize with.
- */
- TWwiseFuture(const typename BaseType::StateType& InState)
- : BaseType(InState)
- { }
- /**
- * Move constructor.
- */
- TWwiseFuture(TWwiseFuture&&) = default;
- /**
- * Move assignment operator.
- */
- TWwiseFuture& operator=(TWwiseFuture&& Other) = default;
- /** Destructor. */
- ~TWwiseFuture() { }
- public:
- /**
- * Gets the future's result.
- *
- * @return The result.
- */
- ResultType& Get() const
- {
- return *this->GetState()->GetResult();
- }
- /**
- * Expose Then functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Then;
- /**
- * Expose Next functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Next;
- /**
- * Expose Reset functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Reset;
- private:
- /** Hidden copy constructor (futures cannot be copied). */
- TWwiseFuture(const TWwiseFuture&);
- /** Hidden copy assignment (futures cannot be copied). */
- TWwiseFuture& operator=(const TWwiseFuture&);
- };
- /**
- * Template for unshared futures (specialization for void).
- */
- template<>
- class TWwiseFuture<void>
- : public TWwiseFutureBase<int>
- {
- typedef TWwiseFutureBase<int> BaseType;
- public:
- /** Default constructor. */
- TWwiseFuture() { }
- /**
- * Creates and initializes a new instance.
- *
- * @param InState The shared state to initialize with.
- */
- TWwiseFuture(const BaseType::StateType& InState)
- : BaseType(InState)
- { }
- /**
- * Move constructor.
- */
- TWwiseFuture(TWwiseFuture&&) = default;
- /**
- * Move assignment operator.
- */
- TWwiseFuture& operator=(TWwiseFuture&& Other) = default;
- /** Destructor. */
- ~TWwiseFuture() { }
- public:
- /**
- * Gets the future's result.
- *
- * @return The result.
- */
- void Get() const
- {
- GetState()->GetResult();
- }
- /**
- * Expose Then functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Then;
- /**
- * Expose Next functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Next;
- /**
- * Expose Reset functionality
- * @see TWwiseFutureBase
- */
- using BaseType::Reset;
- private:
- /** Hidden copy constructor (futures cannot be copied). */
- TWwiseFuture(const TWwiseFuture&);
- /** Hidden copy assignment (futures cannot be copied). */
- TWwiseFuture& operator=(const TWwiseFuture&);
- };
- /* TWwisePromise
- *****************************************************************************/
- template<typename InternalResultType>
- class TWwisePromiseBase
- : FNoncopyable
- {
- typedef TSharedPtr<TWwiseFutureState<InternalResultType>, ESPMode::ThreadSafe> StateType;
- public:
- /** Default constructor. */
- TWwisePromiseBase()
- : State(MakeShared<TWwiseFutureState<InternalResultType>, ESPMode::ThreadSafe>())
- { }
- /**
- * Move constructor.
- *
- * @param Other The promise holding the shared state to move.
- */
- TWwisePromiseBase(TWwisePromiseBase&& Other)
- : State(MoveTemp(Other.State))
- {
- Other.State.Reset();
- }
- /**
- * Create and initialize a new instance with a callback.
- *
- * @param CompletionCallback A function that is called when the future state is completed.
- */
- TWwisePromiseBase(TUniqueFunction<void()>&& CompletionCallback)
- : State(MakeShared<TWwiseFutureState<InternalResultType>, ESPMode::ThreadSafe>(MoveTemp(CompletionCallback)))
- { }
- public:
- /** Move assignment operator. */
- TWwisePromiseBase& operator=(TWwisePromiseBase&& Other)
- {
- State = Other.State;
- Other.State.Reset();
- return *this;
- }
- protected:
- /** Destructor. */
- ~TWwisePromiseBase()
- {
- if (State.IsValid())
- {
- // if you hit this assertion then your promise never had its result
- // value set. broken promises are considered programming errors.
- check(State->IsComplete());
- State.Reset();
- }
- }
- /**
- * Gets the shared state object.
- *
- * @return The shared state object.
- */
- const StateType& GetState()
- {
- // if you hit this assertion then your promise has an invalid state.
- // this happens if you move the promise to another instance.
- check(State.IsValid());
- return State;
- }
- private:
- /** Holds the shared state object. */
- StateType State;
- };
- /**
- * Template for promises.
- */
- template<typename ResultType>
- class TWwisePromise
- : public TWwisePromiseBase<ResultType>
- {
- public:
- typedef TWwisePromiseBase<ResultType> BaseType;
- /** Default constructor (creates a new shared state). */
- TWwisePromise()
- : BaseType()
- , FutureRetrieved(false)
- { }
- /**
- * Move constructor.
- *
- * @param Other The promise holding the shared state to move.
- */
- TWwisePromise(TWwisePromise&& Other)
- : BaseType(MoveTemp(Other))
- , FutureRetrieved(MoveTemp(Other.FutureRetrieved))
- { }
- /**
- * Create and initialize a new instance with a callback.
- *
- * @param CompletionCallback A function that is called when the future state is completed.
- */
- TWwisePromise(TUniqueFunction<void()>&& CompletionCallback)
- : BaseType(MoveTemp(CompletionCallback))
- , FutureRetrieved(false)
- { }
- public:
- /**
- * Move assignment operator.
- *
- * @param Other The promise holding the shared state to move.
- */
- TWwisePromise& operator=(TWwisePromise&& Other)
- {
- BaseType::operator=(MoveTemp(Other));
- FutureRetrieved = MoveTemp(Other.FutureRetrieved);
- return *this;
- }
- public:
- /**
- * Gets a TWwiseFuture object associated with the shared state of this promise.
- *
- * @return The TWwiseFuture object.
- */
- TWwiseFuture<ResultType> GetFuture()
- {
- check(!FutureRetrieved);
- FutureRetrieved = true;
- return TWwiseFuture<ResultType>(this->GetState());
- }
- /**
- * Sets the promised result.
- *
- * The result must be set only once. An assertion will
- * be triggered if this method is called a second time.
- *
- * @param Result The result value to set.
- */
- FORCEINLINE void SetValue(const ResultType& Result)
- {
- EmplaceValue(Result);
- }
- /**
- * Sets the promised result (from rvalue).
- *
- * The result must be set only once. An assertion will
- * be triggered if this method is called a second time.
- *
- * @param Result The result value to set.
- */
- FORCEINLINE void SetValue(ResultType&& Result)
- {
- EmplaceValue(MoveTemp(Result));
- }
- /**
- * Sets the promised result.
- *
- * The result must be set only once. An assertion will
- * be triggered if this method is called a second time.
- *
- * @param Result The result value to set.
- */
- template<typename... ArgTypes>
- void EmplaceValue(ArgTypes&&... Args)
- {
- this->GetState()->EmplaceResult(Forward<ArgTypes>(Args)...);
- }
- private:
- /** Whether a future has already been retrieved from this promise. */
- bool FutureRetrieved;
- };
- /**
- * Template for promises (specialization for reference types).
- */
- template<typename ResultType>
- class TWwisePromise<ResultType&>
- : public TWwisePromiseBase<ResultType*>
- {
- typedef TWwisePromiseBase<ResultType*> BaseType;
- public:
- /** Default constructor (creates a new shared state). */
- TWwisePromise()
- : BaseType()
- , FutureRetrieved(false)
- { }
- /**
- * Move constructor.
- *
- * @param Other The promise holding the shared state to move.
- */
- TWwisePromise(TWwisePromise&& Other)
- : BaseType(MoveTemp(Other))
- , FutureRetrieved(MoveTemp(Other.FutureRetrieved))
- { }
- /**
- * Create and initialize a new instance with a callback.
- *
- * @param CompletionCallback A function that is called when the future state is completed.
- */
- TWwisePromise(TUniqueFunction<void()>&& CompletionCallback)
- : BaseType(MoveTemp(CompletionCallback))
- , FutureRetrieved(false)
- { }
- public:
- /**
- * Move assignment operator.
- *
- * @param Other The promise holding the shared state to move.
- */
- TWwisePromise& operator=(TWwisePromise&& Other)
- {
- BaseType::operator=(MoveTemp(Other));
- FutureRetrieved = MoveTemp(Other.FutureRetrieved);
- return this;
- }
- public:
- /**
- * Gets a TWwiseFuture object associated with the shared state of this promise.
- *
- * @return The TWwiseFuture object.
- */
- TWwiseFuture<ResultType&> GetFuture()
- {
- check(!FutureRetrieved);
- FutureRetrieved = true;
- return TWwiseFuture<ResultType&>(this->GetState());
- }
- /**
- * Sets the promised result.
- *
- * The result must be set only once. An assertion will
- * be triggered if this method is called a second time.
- *
- * @param Result The result value to set.
- */
- FORCEINLINE void SetValue(ResultType& Result)
- {
- EmplaceValue(Result);
- }
- /**
- * Sets the promised result.
- *
- * The result must be set only once. An assertion will
- * be triggered if this method is called a second time.
- *
- * @param Result The result value to set.
- */
- void EmplaceValue(ResultType& Result)
- {
- this->GetState()->EmplaceResult(&Result);
- }
- private:
- /** Whether a future has already been retrieved from this promise. */
- bool FutureRetrieved;
- };
- /**
- * Template for promises (specialization for void results).
- */
- template<>
- class TWwisePromise<void>
- : public TWwisePromiseBase<int>
- {
- typedef TWwisePromiseBase<int> BaseType;
- public:
- /** Default constructor (creates a new shared state). */
- TWwisePromise()
- : BaseType()
- , FutureRetrieved(false)
- { }
- /**
- * Move constructor.
- *
- * @param Other The promise holding the shared state to move.
- */
- TWwisePromise(TWwisePromise&& Other)
- : BaseType(MoveTemp(Other))
- , FutureRetrieved(false)
- { }
- /**
- * Create and initialize a new instance with a callback.
- *
- * @param CompletionCallback A function that is called when the future state is completed.
- */
- TWwisePromise(TUniqueFunction<void()>&& CompletionCallback)
- : BaseType(MoveTemp(CompletionCallback))
- , FutureRetrieved(false)
- { }
- public:
- /**
- * Move assignment operator.
- *
- * @param Other The promise holding the shared state to move.
- */
- TWwisePromise& operator=(TWwisePromise&& Other)
- {
- BaseType::operator=(MoveTemp(Other));
- FutureRetrieved = MoveTemp(Other.FutureRetrieved);
- return *this;
- }
- public:
- /**
- * Gets a TWwiseFuture object associated with the shared state of this promise.
- *
- * @return The TWwiseFuture object.
- */
- TWwiseFuture<void> GetFuture()
- {
- check(!FutureRetrieved);
- FutureRetrieved = true;
- return TWwiseFuture<void>(GetState());
- }
- /**
- * Sets the promised result.
- *
- * The result must be set only once. An assertion will
- * be triggered if this method is called a second time.
- */
- FORCEINLINE void SetValue()
- {
- EmplaceValue();
- }
- /**
- * Sets the promised result.
- *
- * The result must be set only once. An assertion will
- * be triggered if this method is called a second time.
- *
- * @param Result The result value to set.
- */
- void EmplaceValue()
- {
- this->GetState()->EmplaceResult(0);
- }
- private:
- /** Whether a future has already been retrieved from this promise. */
- bool FutureRetrieved;
- };
- /* TWwiseFuture::Then
- *****************************************************************************/
- namespace FutureDetail
- {
- /**
- * Template for setting a promise value from a continuation.
- */
- template<typename Func, typename ParamType, typename ResultType>
- inline void SetPromiseValue(TWwisePromise<ResultType>& Promise, Func& Function, TWwiseFuture<ParamType>&& Param)
- {
- Promise.SetValue(Function(MoveTemp(Param)));
- }
- template<typename Func, typename ParamType>
- inline void SetPromiseValue(TWwisePromise<void>& Promise, Func& Function, TWwiseFuture<ParamType>&& Param)
- {
- Function(MoveTemp(Param));
- Promise.SetValue();
- }
- }
- // Then implementation
- template<typename InternalResultType>
- template<typename Func>
- auto TWwiseFutureBase<InternalResultType>::Then(Func Continuation) //-> TWwiseFuture<decltype(Continuation(MoveTemp(TWwiseFuture<InternalResultType>())))>
- {
- check(IsValid());
- using ReturnValue = decltype(Continuation(MoveTemp(TWwiseFuture<InternalResultType>())));
- TWwisePromise<ReturnValue> Promise;
- TWwiseFuture<ReturnValue> FutureResult = Promise.GetFuture();
- TUniqueFunction<void()> Callback = [PromiseCapture = MoveTemp(Promise), ContinuationCapture = MoveTemp(Continuation), StateCapture = this->State]() mutable
- {
- FutureDetail::SetPromiseValue(PromiseCapture, ContinuationCapture, TWwiseFuture<InternalResultType>(MoveTemp(StateCapture)));
- };
- // This invalidate this future.
- StateType MovedState = MoveTemp(this->State);
- MovedState->SetContinuation(MoveTemp(Callback));
- return FutureResult;
- }
- // Next implementation
- template<typename InternalResultType>
- template<typename Func>
- auto TWwiseFutureBase<InternalResultType>::Next(Func Continuation) //-> TWwiseFuture<decltype(Continuation(Get()))>
- {
- return this->Then([Continuation = MoveTemp(Continuation)](TWwiseFuture<InternalResultType> Self) mutable
- {
- return Continuation(Self.Get());
- });
- }
- /** Helper to create and immediately fulfill a promise */
- template<typename ResultType, typename... ArgTypes>
- TWwisePromise<ResultType> MakeFulfilledWwisePromise(ArgTypes&&... Args)
- {
- TWwisePromise<ResultType> Promise;
- Promise.EmplaceValue(Forward<ArgTypes>(Args)...);
- return Promise;
- }
|