123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383 |
- /*******************************************************************************
- 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 "HAL/PlatformProcess.h"
- #include "Misc/ScopeLock.h"
- #include "Wwise/Stats/Concurrency.h"
- #include "Wwise/Stats/AsyncStats.h"
- //
- // 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. Even when waiting a result, it is usually ready for usage.
- //
- // This means there is a very strong chance a Future doesn't need to retrieve an Event from its pool of resources.
- //
- // Since some platforms have set amount of hardware synchronization primitites, it's preferable to reduce the number of
- // them to a minimum. Although they are useful for actually waiting for a longer operation to finish in a particular
- // thread, even then, there will usually be one lengthy operation, and all the subsequent operations will be done
- // instantaneously without a lock.
- //
- /**
- * Base class for the internal state of asynchronous return values (futures).
- */
- class FWwiseFutureState
- {
- public:
- /** Default constructor. */
- FWwiseFutureState()
- : CompletionCallback(nullptr)
- , CompletionEvent(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)
- , CompletionEvent(nullptr)
- , Complete(false)
- {
- ASYNC_INC_DWORD_STAT(STAT_WwiseFutures);
- }
- /** Destructor. */
- ~FWwiseFutureState()
- {
- ASYNC_DEC_DWORD_STAT(STAT_WwiseFutures);
- if (CompletionCallback)
- {
- delete CompletionCallback;
- CompletionCallback = nullptr;
- }
- if (CompletionEvent)
- {
- FPlatformProcess::ReturnSynchEventToPool(CompletionEvent);
- CompletionEvent = nullptr;
- ASYNC_DEC_DWORD_STAT(STAT_WwiseFuturesWithEvent);
- }
- }
- 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(EMemoryOrder::SequentiallyConsistent);
- }
- /**
- * Blocks the calling thread until the future result is available.
- *
- * @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) const
- {
- if (IsComplete())
- {
- return true;
- }
- SCOPED_WWISECONCURRENCY_EVENT_4(TEXT("FWwiseFutureState::WaitFor"));
- FEvent* CompletionEventToUse = CompletionEvent.Load(EMemoryOrder::SequentiallyConsistent);
- if (!CompletionEventToUse)
- {
- auto* NewCompletionEvent = FPlatformProcess::GetSynchEventFromPool(true);
- if (CompletionEvent.CompareExchange(CompletionEventToUse, NewCompletionEvent))
- {
- CompletionEventToUse = NewCompletionEvent;
- ASYNC_INC_DWORD_STAT(STAT_WwiseFuturesWithEvent);
- }
- else
- {
- FPlatformProcess::ReturnSynchEventToPool(NewCompletionEvent);
- }
- if (IsComplete())
- {
- return true;
- }
- }
- const bool bIsInGameThread = IsInGameThread();
- CONDITIONAL_SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyGameThreadWait, bIsInGameThread);
- CONDITIONAL_SCOPE_CYCLE_COUNTER(STAT_WwiseConcurrencyWait, !bIsInGameThread);
- if (CompletionEventToUse->Wait(Duration))
- {
- return true;
- }
- return false;
- }
- /**
- * 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, EMemoryOrder::SequentiallyConsistent);
- FEvent* Event = CompletionEvent.Load(EMemoryOrder::SequentiallyConsistent);
- auto* Continuation = CompletionCallback.Exchange(nullptr);
- if (Event)
- {
- Event->Trigger();
- }
- if (Continuation)
- {
- (*Continuation)();
- delete Continuation;
- }
- }
- private:
- /** An optional callback function that is executed the state is completed. */
- TAtomic< TUniqueFunction<void()>* > CompletionCallback;
- /** Holds an event signaling that the result is available. */
- mutable TAtomic< FEvent* > CompletionEvent;
- /** Whether the asynchronous result is available. */
- TAtomic<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() const
- {
- 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.
- * Reseting 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<typename ResultType> class TWwiseSharedFuture;
- /**
- * 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();
- }
- /**
- * Moves this future's state into a shared future.
- *
- * @return The shared future object.
- */
- TWwiseSharedFuture<ResultType> Share()
- {
- return TWwiseSharedFuture<ResultType>(MoveTemp(*this));
- }
- /**
- * 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();
- }
- /**
- * Moves this future's state into a shared future.
- *
- * @return The shared future object.
- */
- TWwiseSharedFuture<ResultType&> Share()
- {
- return TWwiseSharedFuture<ResultType&>(MoveTemp(*this));
- }
- /**
- * 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();
- }
- /**
- * Moves this future's state into a shared future.
- *
- * @return The shared future object.
- */
- TWwiseSharedFuture<void> Share();
- /**
- * 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&);
- };
- /* TWwiseSharedFuture
- *****************************************************************************/
- /**
- * Template for shared futures.
- */
- template<typename ResultType>
- class TWwiseSharedFuture
- : public TWwiseFutureBase<ResultType>
- {
- typedef TWwiseFutureBase<ResultType> BaseType;
- public:
- /** Default constructor. */
- TWwiseSharedFuture() { }
- /**
- * Creates and initializes a new instance.
- *
- * @param InState The shared state to initialize from.
- */
- TWwiseSharedFuture(const typename BaseType::StateType& InState)
- : BaseType(InState)
- { }
- /**
- * Creates and initializes a new instances from a future object.
- *
- * @param Future The future object to initialize from.
- */
- TWwiseSharedFuture(TWwiseFuture<ResultType>&& Future)
- : BaseType(MoveTemp(Future))
- { }
- /**
- * Copy constructor.
- */
- TWwiseSharedFuture(const TWwiseSharedFuture&) = default;
- /**
- * Copy assignment operator.
- */
- TWwiseSharedFuture& operator=(const TWwiseSharedFuture& Other) = default;
- /**
- * Move constructor.
- */
- TWwiseSharedFuture(TWwiseSharedFuture&&) = default;
- /**
- * Move assignment operator.
- */
- TWwiseSharedFuture& operator=(TWwiseSharedFuture&& Other) = default;
- /** Destructor. */
- ~TWwiseSharedFuture() { }
- public:
- /**
- * Gets the future's result.
- *
- * @return The result.
- */
- ResultType Get() const
- {
- return this->GetState()->GetResult();
- }
- };
- /**
- * Template for shared futures (specialization for reference types).
- */
- template<typename ResultType>
- class TWwiseSharedFuture<ResultType&>
- : public TWwiseFutureBase<ResultType*>
- {
- typedef TWwiseFutureBase<ResultType*> BaseType;
- public:
- /** Default constructor. */
- TWwiseSharedFuture() { }
- /**
- * Creates and initializes a new instance.
- *
- * @param InState The shared state to initialize from.
- */
- TWwiseSharedFuture(const typename BaseType::StateType& InState)
- : BaseType(InState)
- { }
- /**
- * Creates and initializes a new instances from a future object.
- *
- * @param Future The future object to initialize from.
- */
- TWwiseSharedFuture(TWwiseFuture<ResultType>&& Future)
- : BaseType(MoveTemp(Future))
- { }
- /**
- * Copy constructor.
- */
- TWwiseSharedFuture(const TWwiseSharedFuture&) = default;
- /** Copy assignment operator. */
- TWwiseSharedFuture& operator=(const TWwiseSharedFuture& Other) = default;
- /**
- * Move constructor.
- */
- TWwiseSharedFuture(TWwiseSharedFuture&&) = default;
- /**
- * Move assignment operator.
- */
- TWwiseSharedFuture& operator=(TWwiseSharedFuture&& Other) = default;
- /** Destructor. */
- ~TWwiseSharedFuture() { }
- public:
- /**
- * Gets the future's result.
- *
- * @return The result.
- */
- ResultType& Get() const
- {
- return *this->GetState()->GetResult();
- }
- };
- /**
- * Template for shared futures (specialization for void).
- */
- template<>
- class TWwiseSharedFuture<void>
- : public TWwiseFutureBase<int>
- {
- typedef TWwiseFutureBase<int> BaseType;
- public:
- /** Default constructor. */
- TWwiseSharedFuture() { }
- /**
- * Creates and initializes a new instance from shared state.
- *
- * @param InState The shared state to initialize from.
- */
- TWwiseSharedFuture(const BaseType::StateType& InState)
- : BaseType(InState)
- { }
- /**
- * Creates and initializes a new instances from a future object.
- *
- * @param Future The future object to initialize from.
- */
- TWwiseSharedFuture(TWwiseFuture<void>&& Future)
- : BaseType(MoveTemp(Future))
- { }
- /**
- * Copy constructor.
- */
- TWwiseSharedFuture(const TWwiseSharedFuture&) = default;
- /**
- * Copy assignment operator.
- */
- TWwiseSharedFuture& operator=(const TWwiseSharedFuture& Other) = default;
- /**
- * Move constructor.
- */
- TWwiseSharedFuture(TWwiseSharedFuture&&) = default;
- /**
- * Move assignment operator.
- */
- TWwiseSharedFuture& operator=(TWwiseSharedFuture&& Other) = default;
- /** Destructor. */
- ~TWwiseSharedFuture() { }
- public:
- /**
- * Gets the future's result.
- *
- * @return The result.
- */
- void Get() const
- {
- GetState()->GetResult();
- }
- };
- inline TWwiseSharedFuture<void> TWwiseFuture<void>::Share()
- {
- return TWwiseSharedFuture<void>(MoveTemp(*this));
- }
- /* 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;
- }
|