Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Change combine(bool) to combine(CombinatorMode)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlau committed Mar 22, 2017
1 parent 25ca895 commit 9a622e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ observe(future).context(context, [](bool toggled) {
```
AsyncFuture::combine(bool settleAllMode)
AsyncFuture::combine(CombinatorMode mode = FailFast)
------------
This function creates a Combinator object (inherit `Observable<void>`) for combining multiple future objects with different type into a single future.
Expand All @@ -206,14 +206,14 @@ For example:
QFuture<QImage> f1 = QtConcurrent::run(readImage, QString("image.jpg"));
QFuture<void> f2 = observe(timer, &QTimer::timeout).future();
QFuture<QImage> result = (combine(false) << f1 << f2).subscribe([=](){
QFuture<QImage> result = (combine(AllSettled) << f1 << f2).subscribe([=](){
// Read an image but do not return before timeout
return f1.result();
}).future();
```

Once all the observed futures finished, the contained future will be finished too. And it will be cancelled immediately if any one of the observed future is cancelled. In case you wish the cancellation take place after all the futures finished, you should set `settleAllMode` flag to on.
Once all the observed futures finished, the contained future will be finished too. And it will be cancelled immediately if any one of the observed future is cancelled in fail fast mode. In case you wish the cancellation take place after all the futures finished, you should set mode to `AllSettled`.


AsyncFuture::deferred&lt;T&gt;()
Expand Down Expand Up @@ -288,7 +288,7 @@ observe(reading).subscribe([](QFuture<QImage> future) {
});
```
The return type can be none or any kind value. That would determine what type of `Observable<R>` generated by context()/subscribe().
The return type can be none or any kind of value. That would determine what type of `Observable<R>` generated by context()/subscribe().
In case, you return a QFuture object. Then the new `Observable<R>` object will be deferred to complete/cancel until your future object is resolved. Therefore, you could run QtConcurrent::run within your callback function to make a more complex/flexible multiple threading programming models.
Expand Down
13 changes: 9 additions & 4 deletions asyncfuture.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,13 +691,18 @@ class Deferred<void> : public Observable<void> {
QSharedPointer<Private::DeferredFuture<void> > defer;
};

typedef enum {
FailFast,
AllSettled
} CombinatorMode;

class Combinator : public Observable<void> {
private:
QSharedPointer<Private::CombinedFuture> combinedFuture;

public:
inline Combinator(bool settleAllMode = false) : Observable<void>() {
combinedFuture = Private::CombinedFuture::create(settleAllMode);
inline Combinator(CombinatorMode mode = FailFast) : Observable<void>() {
combinedFuture = Private::CombinedFuture::create(mode == AllSettled);
m_future = combinedFuture->future();
}
inline ~Combinator() {
Expand Down Expand Up @@ -754,8 +759,8 @@ auto deferred() -> Deferred<T> {
return Deferred<T>();
}

inline Combinator combine(bool settleAllMode = false) {
return Combinator(settleAllMode);
inline Combinator combine(CombinatorMode mode = FailFast) {
return Combinator(mode);
}

}
2 changes: 1 addition & 1 deletion tests/asyncfutureunittests/asyncfuturetests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ void AsyncFutureTests::test_Combinator()
auto d2 = deferred<QString>();
auto d3 = deferred<void>();

auto combinator = combine(true);
auto combinator = combine(AllSettled);
combinator << d1.future() << d2.future() << d3.future();

QFuture<void> future = combinator.future();
Expand Down

0 comments on commit 9a622e0

Please sign in to comment.