-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow QSharedPointer as rejection reason
Embed the promise fulfillment value and rejection reason in respectively PromiseValue and PromiseError private wrappers, both storing the data in a shared pointer (QPromiseError is now deprecated).
- Loading branch information
1 parent
2c8ed6e
commit 7b0cba5
Showing
7 changed files
with
195 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
// QtPromise | ||
#include "qpromise_p.h" | ||
#include "qpromiseerror.h" | ||
#include "qpromiseglobal.h" | ||
|
||
// Qt | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ SUBDIRS += \ | |
fail \ | ||
finally \ | ||
operators \ | ||
reject \ | ||
resolve \ | ||
tap \ | ||
tapfail \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TARGET = tst_qpromise_reject | ||
SOURCES += $$PWD/tst_reject.cpp | ||
|
||
include(../../qtpromise.pri) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Tests | ||
#include "../../shared/utils.h" | ||
|
||
// QtPromise | ||
#include <QtPromise> | ||
|
||
// Qt | ||
#include <QtTest> | ||
|
||
// STL | ||
#include <memory> | ||
|
||
using namespace QtPromise; | ||
|
||
class tst_qpromise_reject : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private Q_SLOTS: | ||
void rejectWithValue(); | ||
void rejectWithQSharedPtr(); | ||
void rejectWithStdSharedPtr(); | ||
}; | ||
|
||
QTEST_MAIN(tst_qpromise_reject) | ||
#include "tst_reject.moc" | ||
|
||
void tst_qpromise_reject::rejectWithValue() | ||
{ | ||
auto p = QPromise<int>::reject(42); | ||
|
||
QCOMPARE(p.isRejected(), true); | ||
QCOMPARE(waitForError(p, -1), 42); | ||
} | ||
|
||
// https://github.com/simonbrunel/qtpromise/issues/6 | ||
void tst_qpromise_reject::rejectWithQSharedPtr() | ||
{ | ||
QWeakPointer<int> wptr; | ||
|
||
{ | ||
QSharedPointer<int> sptr(new int(42)); | ||
auto p = QPromise<int>::reject(sptr); | ||
|
||
QCOMPARE(waitForError(p, QSharedPointer<int>()), sptr); | ||
|
||
wptr = sptr; | ||
sptr.reset(); | ||
|
||
QCOMPARE(wptr.isNull(), false); // "p" still holds a reference | ||
} | ||
|
||
QCOMPARE(wptr.isNull(), true); | ||
} | ||
|
||
// https://github.com/simonbrunel/qtpromise/issues/6 | ||
void tst_qpromise_reject::rejectWithStdSharedPtr() | ||
{ | ||
std::weak_ptr<int> wptr; | ||
|
||
{ | ||
std::shared_ptr<int> sptr(new int(42)); | ||
auto p = QPromise<int>::reject(sptr); | ||
|
||
QCOMPARE(waitForError(p, std::shared_ptr<int>()), sptr); | ||
|
||
wptr = sptr; | ||
sptr.reset(); | ||
|
||
QCOMPARE(wptr.use_count(), 1l); // "p" still holds a reference | ||
} | ||
|
||
QCOMPARE(wptr.use_count(), 0l); | ||
} |
Oops, something went wrong.