Use C++11 curly braces initialization

This commit is contained in:
Simon Brunel
2020-02-17 20:02:26 +01:00
parent 0bfdddd887
commit be5455a8c8
55 changed files with 655 additions and 655 deletions

View File

@ -145,11 +145,11 @@ will be resolved when the network request is finished:
```cpp
QtPromise::QPromise<QByteArray> download(const QUrl& url)
{
return QtPromise::QPromise<QByteArray>([&](
return QtPromise::QPromise<QByteArray>{[&](
const QtPromise::QPromiseResolve<QByteArray>& resolve,
const QtPromise::QPromiseReject<QByteArray>& reject) {
QNetworkReply* reply = manager->get(QNetworkRequest(url));
QNetworkReply* reply = manager->get(QNetworkRequest{url});
QObject::connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error() == QNetworkReply::NoError) {
resolve(reply->readAll());
@ -159,7 +159,7 @@ QtPromise::QPromise<QByteArray> download(const QUrl& url)
reply->deleteLater();
});
});
}};
}
```
@ -174,7 +174,7 @@ QtPromise::QPromise<Entries> uncompress(const QByteArray& data)
// {...} uncompress data and parse content.
if (error) {
throw MalformedException();
throw MalformedException{};
}
return entries;
@ -193,7 +193,7 @@ It's then easy to chain the whole asynchronous process using promises:
```cpp
download(url).then(&uncompress).then([](const Entries& entries) {
if (entries.isEmpty()) {
throw UpdateException("No entries");
throw UpdateException{"No entries"};
}
// {...} process entries
}).finally([]() {

View File

@ -29,7 +29,7 @@ QPromise<QByteArray> process(const QUrl& url)
{
return QtPromise::attempt([&]() {
if (!url.isValid()) {
throw InvalidUrlException();
throw InvalidUrlException{};
}
return download(url);

View File

@ -31,7 +31,7 @@ Q_SIGNALS:
void error(ErrorCode);
};
auto sender = new Sender();
auto sender = new Sender{};
auto output = QtPromise::connect(sender, &Sender::finished, &Sender::error);
// 'output' resolves as soon as one of the following events happens:

View File

@ -27,9 +27,9 @@ auto output = QtPromise::each(QVector<QUrl>{
QUrl("http://b..."),
QUrl("http://c...")
}, [](const QUrl& url, ...) {
return QPromise<void>([&](auto resolve, auto reject) {
return QPromise<void>{[&](auto resolve, auto reject) {
// process url asynchronously ...
})
}};
});
// `output` resolves as soon as all promises returned by

View File

@ -29,10 +29,10 @@ auto output = QtPromise::filter(QVector{
QUrl("http://b..."),
QUrl("http://c...")
}, [](const QUrl& url, ...) {
return QPromise<bool>([&](auto resolve, auto reject) {
return QPromise<bool>{[&](auto resolve, auto reject) {
// resolve(true) if 'url' is reachable, else resolve(false)
// {...}
});
}};
});
// 'output' resolves as soon as all promises returned by

View File

@ -28,10 +28,10 @@ auto output = QtPromise::map(QVector{
QUrl("http://b..."),
QUrl("http://c...")
}, [](const QUrl& url, ...) {
return QPromise<QByteArray>([&](auto resolve, auto reject) {
return QPromise<QByteArray>{[&](auto resolve, auto reject) {
// download content at url and resolve
// {...}
});
}};
});
// 'output' resolves as soon as all promises returned by

View File

@ -33,9 +33,9 @@ auto output = QtPromise::reduce(QList<QUrl>{
"file:f2.txt" // contains "42"
}, [](const QString& acc, const QString& cur, int idx) {
return readAsync(cur).then([=](const QString& res) {
return QString("%1;%2:%3").arg(acc).arg(idx).arg(res);
return QString{"%1;%2:%3"}.arg(acc).arg(idx).arg(res);
});
}, QString("index:text"));
}, QString{"index:text"});
// 'output' resolves as soon as all promises returned by
// 'reducer' are fulfilled or at least one is rejected.

View File

@ -16,7 +16,7 @@ resolved from a given `value` but without the extra typing:
```cpp
auto promise = QtPromise::resolve(); // QPromise<void>
auto promise = QtPromise::resolve(42); // QPromise<int>
auto promise = QtPromise::resolve(QString("foo")); // QPromise<QString>
auto promise = QtPromise::resolve(QString{"foo"}); // QPromise<QString>
```
This method also allows to convert `QFuture<T>` to `QPromise<T>`, delayed until the `QFuture` is

View File

@ -18,7 +18,7 @@ QPromise<int> promise([](const QPromiseResolve<int>& resolve, const QPromiseReje
if (success) {
resolve(result);
} else {
reject(customException());
reject(customException{});
}
});
});
@ -32,9 +32,9 @@ no argument.
C++14 alternative:
```cpp
QPromise<int> promise([](const auto& resolve, const auto& reject) {
QPromise<int> promise{[](const auto& resolve, const auto& reject) {
// {...}
});
}};
```
## Undefined rejection reason
@ -46,7 +46,7 @@ a promise without explicit reason, in which case, a built-in [`QPromiseUndefined
is thrown:
```cpp
QPromise<int> promise([](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
QPromise<int> promise{[](const QPromiseResolve<int>& resolve, const QPromiseReject<int>& reject) {
async_method([=](bool success, int result) {
if (success) {
resolve(result);
@ -54,7 +54,7 @@ QPromise<int> promise([](const QPromiseResolve<int>& resolve, const QPromiseReje
reject();
}
});
});
}};
```
```cpp

View File

@ -43,9 +43,9 @@ of the promise that rejected, whether or not the other promises are resolved.
QPromise<QList<QUrl>> input = {...}
auto output = input.each([](const QUrl& url, ...) {
return QPromise<void>([&](auto resolve, auto reject) {
return QPromise<void>{[&](auto resolve, auto reject) {
// process url asynchronously ...
})
}};
});
// `output` resolves as soon as all promises returned by

View File

@ -33,9 +33,9 @@ QPromise<QList<QUrl>> input = {...}
auto output = input.filter([](const QUrl& url, ...) {
return url.isValid(); // Keep only valid URLs
}).filter([](const QUrl& url, ...) {
return QPromise<bool>([&](auto resolve, auto reject) {
return QPromise<bool>{[&](auto resolve, auto reject) {
// resolve(true) if `url` is reachable, else resolve(false)
});
}};
});
// 'output' resolves as soon as all promises returned by

View File

@ -30,14 +30,14 @@ promise that rejected, whether or not the other promises are resolved.
QPromise<QList<QUrl>> input = {...}
auto output = input.map([](const QUrl& url, int index) {
return QPromise<QByteArray>([&](auto resolve, auto reject) {
return QPromise<QByteArray>{[&](auto resolve, auto reject) {
// download content at 'url' and resolve
// {...}
});
}};
}).map([](const QByteArray& value, ...) {
// process the downloaded QByteArray
// {...}
return DownloadResult(value);
return DownloadResult{value};
});
// 'output' resolves as soon as all promises returned by

View File

@ -44,9 +44,9 @@ auto input = QtPromise::resolve(QList<QUrl>{
// Concatenate the content of the given files, read asynchronously
auto output = input.reduce([](const QString& acc, const QString& cur, int idx) {
return readAsync(cur).then([=](const QString& res) {
return QString("%1;%2:%3").arg(acc).arg(idx).arg(res);
return QString{"%1;%2:%3"}.arg(acc).arg(idx).arg(res);
});
}, QString("index:text"));
}, QString{"index:text"});
// 'output' resolves as soon as all promises returned by
// 'reducer' are fulfilled or at least one is rejected.

View File

@ -16,11 +16,11 @@ Creates a `QPromise<T>` that is rejected with the given `reason` of *whatever ty
QPromise<int> compute(const QString& type)
{
if (type == "foobar") {
return QPromise<int>::reject(QString("Unknown type: %1").arg(type));
return QPromise<int>::reject(QString{"Unknown type: %1"}.arg(type));
}
return QPromise<int>([](const QPromiseResolve<int>& resolve) {
return QPromise<int>{[](const QPromiseResolve<int>& resolve) {
// {...}
});
}};
}
```

View File

@ -19,9 +19,9 @@ QPromise<int> compute(const QString& type)
return QPromise<int>::resolve(42);
}
return QPromise<int>([](const QPromiseResolve<int>& resolve) {
return QPromise<int>{[](const QPromiseResolve<int>& resolve) {
// {...}
});
}};
}
```

View File

@ -35,7 +35,7 @@ QPromise<int> input = ...
auto output = input.then([](int res) {
return QtConcurrent::run([]() {
// {...}
return QString("42");
return QString{"42"};
});
});
@ -62,10 +62,10 @@ promise.then([](int res) {
// {...}
if (!success) {
throw CustomException();
throw CustomException{};
}
return QString("42");
return QString{"42"};
});
}).fail([](const CustomException& error) {
// {...}