Skip to content

Commit

Permalink
Introduce NewSimpleFuture and NewSimplePromise
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperquantum committed Jul 14, 2024
1 parent 93a3311 commit 24ededb
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/cmd-remote/cmd-remote-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 289,13 @@ inline void testFutures()
auto future3 = promise.future();

promise.setOutcome(failure);

// ====

auto simplePromise = NewAsync::createSimplePromise<QString>();
auto simpleFuture = simplePromise.future();

simplePromise.setOutcome("Finished");
}

void printVersion(QTextStream& out)
Expand Down
9 changes: 9 additions & 0 deletions src/common/newasync.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 32,9 @@ namespace PMP
template<class TResult, class TError>
static NewPromise<TResult, TError> createPromise();

template<class TOutcome>
static NewSimplePromise<TOutcome> createSimplePromise();

template<class TResult, class TError>
static NewFuture<TResult, TError> runOnEventLoop(
QObject* receiver,
Expand All @@ -47,6 50,12 @@ namespace PMP
return NewPromise<TResult, TError>();
}

template<class TOutcome>
NewSimplePromise<TOutcome> NewAsync::createSimplePromise()
{
return NewSimplePromise<TOutcome>();
}

template<class TResult, class TError>
NewFuture<TResult, TError> NewAsync::runOnEventLoop(
QObject* receiver,
Expand Down
75 changes: 73 additions & 2 deletions src/common/newfuture.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 37,8 @@ namespace PMP

template<class TResult, class TError> class NewPromise;
template<class TResult, class TError> class NewFuture;
template<class TOutcome> class NewSimplePromise;
template<class TOutcome> class NewSimpleFuture;
template<class TResult, class TError> class NewFutureStorage;
template<class TResult, class TError> class Continuation;

Expand Down Expand Up @@ -109,6 111,7 @@ namespace PMP

friend class QSharedPointer<NewFutureStorage<TResult, TError>>;
friend class NewPromise<TResult, TError>;
friend class NewSimplePromise<TResult>;
template<class, class> friend class NewFuture;
friend class NewConcurrent;

Expand Down Expand Up @@ -281,15 284,83 @@ namespace PMP
auto wrapper =
[f, storage, runner]()
{
auto resultOrError = f();
auto outcome = f();

// store result, notify listeners, and run continuation
storage->storeAndContinueFrom(resultOrError, runner);
storage->storeAndContinueFrom(outcome, runner);
};

runner->run(wrapper);

return NewFuture<TResult, TError>(storage);
}

// =================================================================== //

template<class TOutcome>
class NewSimpleFuture
{
public:
using OutcomeType = TOutcome;

void handleOnEventLoop(QObject* receiver, std::function<void(OutcomeType)> f);

private:
using StoragePtr = QSharedPointer<NewFutureStorage<TOutcome, FailureType>>;
using ContinuationPtr = QSharedPointer<Continuation<TOutcome, FailureType>>;

NewSimpleFuture(StoragePtr storage);

static NewSimpleFuture<TOutcome> createForRunner(
QSharedPointer<Runner> runner,
std::function<OutcomeType()> f);

friend class NewSimplePromise<TOutcome>;

StoragePtr _storage;
};

template<class TOutcome>
inline void NewSimpleFuture<TOutcome>::handleOnEventLoop(
QObject* receiver,
std::function<void (OutcomeType)> f)
{
auto runner = QSharedPointer<EventLoopRunner>::create(receiver);

auto continuation = ContinuationPtr::create(runner, f);

_storage->setContinuation(continuation);
}

template<class TOutcome>
NewSimpleFuture<TOutcome>::NewSimpleFuture(StoragePtr storage)
: _storage(storage)
{
//
}

template<class TOutcome>
NewSimpleFuture<TOutcome> NewSimpleFuture<TOutcome>::createForRunner(
QSharedPointer<Runner> runner,
std::function<OutcomeType ()> f)
{
auto storage = StoragePtr::create();

auto wrapper =
[f, storage, runner]()
{
auto outcome = f();

auto resultOrError =
ResultOrError<TOutcome, FailureType>::fromResult(outcome);

// store result, notify listeners, and run continuation
storage->storeAndContinueFrom(resultOrError, runner);
};

runner->run(wrapper);

return NewSimpleFuture<TOutcome>(storage);
}
}
#endif
42 changes: 42 additions & 0 deletions src/common/newpromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 75,47 @@ namespace PMP
{
//
}

// =================================================================== //

template<class TOutcome>
class NewSimplePromise
{
public:
using OutcomeType = TOutcome;
using FutureType = NewSimpleFuture<TOutcome>;

FutureType future() const;

void setOutcome(OutcomeType const& outcome);

private:
NewSimplePromise();

friend class NewAsync;

QSharedPointer<NewFutureStorage<TOutcome, FailureType>> _storage;
};

template<class TOutcome>
NewSimpleFuture<TOutcome> NewSimplePromise<TOutcome>::future() const
{
return NewSimpleFuture<TOutcome>(_storage);
}

template<class TOutcome>
void NewSimplePromise<TOutcome>::setOutcome(const OutcomeType& outcome)
{
auto resultOrError = ResultOrError<TOutcome, FailureType>::fromResult(outcome);

_storage->storeAndContinueFrom(resultOrError, nullptr);
}

template<class TOutcome>
NewSimplePromise<TOutcome>::NewSimplePromise()
: _storage(QSharedPointer<NewFutureStorage<TOutcome, FailureType>>::create())
{
//
}
}
#endif

0 comments on commit 24ededb

Please sign in to comment.