Skip to content

Commit

Permalink
Introduce Algorithms header with transform() function
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed Mar 21, 2024
1 parent dcf6279 commit 9d728b6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 56 deletions.
23 changes: 23 additions & 0 deletions src/base/Algorithms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2024 Linus Jahn <[email protected]>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#ifndef ALGORITHMS_H
#define ALGORITHMS_H

#include <algorithm>

Check failure on line 8 in src/base/Algorithms.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/base/Algorithms.h:8:10 [clang-diagnostic-error]

'algorithm' file not found

namespace QXmpp::Private {

template<typename OutputVector, typename InputVector, typename Converter>
auto transform(InputVector &input, Converter convert)
{
OutputVector output;
output.reserve(input.size());
std::transform(input.begin(), input.end(), std::back_inserter(output), std::forward<Converter>(convert));
return output;
}

} // namespace QXmpp::Private

#endif // ALGORITHMS_H
8 changes: 4 additions & 4 deletions src/base/QXmppFileMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "QXmppUtils.h"
#include "QXmppUtils_p.h"

#include "Algorithms.h"

#include <utility>

#include <QDateTime>
Expand Down Expand Up @@ -79,10 +81,8 @@ QVector<QDomElement> allChildElements(const QDomElement &el, const QString &name
template<typename Func>
QVector<std::invoke_result_t<Func, QDomElement>> forAllChildElements(const QDomElement &el, const QString &name, Func func)
{
auto elements = allChildElements(el, name);
QVector<std::invoke_result_t<Func, QDomElement>> out;
std::transform(elements.begin(), elements.end(), std::back_inserter(out), func);
return out;
return transform<std::vector<std::invoke_result_t<Func, QDomElement>>>(
allChildElements(el, name), std::forward<Func>(func));
}

template<typename ElementType>
Expand Down
18 changes: 5 additions & 13 deletions src/base/QXmppHashing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "QXmppHash.h"
#include "QXmppHashing_p.h"

#include "Algorithms.h"

#include <QCryptographicHash>
#include <QFuture>
#include <QFutureInterface>
Expand Down Expand Up @@ -167,16 +169,6 @@ uint16_t QXmpp::Private::hashPriority(HashAlgorithm algorithm)
return 0;
}

template<typename T, typename Converter>
auto transform(std::vector<T> &input, Converter convert)
{
using Output = std::decay_t<decltype(convert(input.front()))>;
std::vector<Output> output;
output.reserve(input.size());
std::transform(input.begin(), input.end(), std::back_inserter(output), std::move(convert));
return output;
}

auto makeReadyResult(HashingResult::Result result, std::unique_ptr<QIODevice> device)
{
return makeReadyFuture<HashingResultPtr>(std::make_shared<HashingResult>(std::move(result), std::move(device)));
Expand Down Expand Up @@ -263,7 +255,7 @@ class HashGenerator : public QObject
std::function<bool()> isCancelled)
{
// convert to QCryptographicHash::Algorithm for hashing
auto qtAlgorithms = transform(algorithms, [](auto algorithm) {
auto qtAlgorithms = transform<std::vector<QCryptographicHash::Algorithm>>(algorithms, [](auto algorithm) {
auto converted = toCryptograhicHashAlgorithm(algorithm);
Q_ASSERT_X(converted.has_value(), "calculate hashes", "Must only be called with algorithms supported by QCryptographicHash");
return *converted;
Expand Down Expand Up @@ -304,7 +296,7 @@ class HashGenerator : public QObject
m_isCancelled(std::move(isCancelled))
{
// create hash processors
m_hashProcessors = transform(algorithms, [this](auto algorithm) {
m_hashProcessors = transform<std::vector<HashProcessor>>(algorithms, [this](auto algorithm) {
return HashProcessor(this, algorithm);
});

Expand Down Expand Up @@ -389,7 +381,7 @@ class HashGenerator : public QObject

void finish()
{
auto hashes = transform(m_hashProcessors, [](auto &processor) {
auto hashes = transform<std::vector<QXmppHash>>(m_hashProcessors, [](auto &processor) {
QXmppHash hash;
hash.setAlgorithm(toHashAlgorithm(processor.algorithm));
hash.setHash(processor.hash->result());
Expand Down
24 changes: 6 additions & 18 deletions src/client/QXmppFileSharingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "QXmppBitsOfBinaryContentId.h"
#include "QXmppBitsOfBinaryData.h"
#include "QXmppClient.h"
#include "QXmppFileMetadata.h"
#include "QXmppFileShare.h"
#include "QXmppFutureUtils_p.h"
Expand All @@ -16,6 +15,8 @@
#include "QXmppUploadRequestManager.h"
#include "QXmppUtils_p.h"

#include "Algorithms.h"

#include <any>
#include <unordered_map>
#include <utility>
Expand Down Expand Up @@ -43,16 +44,6 @@ static std::vector<HashAlgorithm> hashAlgorithms()
#endif
}

template<typename T, typename Converter>
auto transform(T &input, Converter convert)
{
using Output = std::decay_t<decltype(convert(input.front()))>;
std::vector<Output> output;
output.reserve(input.size());
std::transform(input.begin(), input.end(), std::back_inserter(output), std::move(convert));
return output;
}

class QXmppFileUploadPrivate
{
public:
Expand Down Expand Up @@ -475,12 +466,9 @@ std::shared_ptr<QXmppFileUpload> QXmppFileSharingManager::uploadFile(std::shared
auto &hashValue = hashResult->result;
if (std::holds_alternative<std::vector<QXmppHash>>(hashValue)) {
const auto &hashesVector = std::get<std::vector<QXmppHash>>(hashValue);
QVector<QXmppHash> hashes;
hashes.reserve(hashesVector.size());
std::transform(hashesVector.begin(), hashesVector.end(),
std::back_inserter(hashes), [](auto &&hash) {
return hash;
});
auto hashes = transform<QVector<QXmppHash>>(hashesVector, [](auto &&hash) {
return hash;
});
upload->d->metadata.setHashes(hashes);
upload->d->success = true;
} else if (std::holds_alternative<Cancelled>(hashValue)) {
Expand Down Expand Up @@ -564,7 +552,7 @@ std::shared_ptr<QXmppFileDownload> QXmppFileSharingManager::downloadFile(

download->d->hashesFuture = verifyHashes(
std::move(file),
transform(download->d->hashes, [](auto hash) { return hash; }));
transform<std::vector<QXmppHash>>(download->d->hashes, [](auto hash) { return hash; }));

await(download->d->hashesFuture, this, [download](HashVerificationResultPtr hashResult) {
auto convert = overloaded {
Expand Down
16 changes: 4 additions & 12 deletions src/client/QXmppMamManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@
#include "QXmppUtils.h"
#include "QXmppUtils_p.h"

#include "Algorithms.h"

#include <unordered_map>

#include <QDomElement>

using namespace QXmpp;
using namespace QXmpp::Private;

template<typename T, typename Converter>
auto transform(const T &input, Converter convert)
{
using Output = std::decay_t<decltype(convert(*input.begin()))>;
QVector<Output> output;
output.reserve(input.size());
std::transform(input.begin(), input.end(), std::back_inserter(output), std::move(convert));
return output;
}

template<typename T>
auto sum(const T &c)
{
Expand Down Expand Up @@ -322,7 +314,7 @@ QXmppTask<QXmppMamManager::RetrieveResult> QXmppMamManager::retrieveMessages(con
state.processedMessages.resize(state.messages.size());

// check for encrypted messages (once)
auto messagesEncrypted = transform(state.messages, [&](const auto &m) {
auto messagesEncrypted = transform<std::vector<bool>>(state.messages, [&](const auto &m) {
return e2eeExt->isEncrypted(m.element);
});
auto encryptedCount = sum(messagesEncrypted);
Expand Down Expand Up @@ -367,7 +359,7 @@ QXmppTask<QXmppMamManager::RetrieveResult> QXmppMamManager::retrieveMessages(con
}

// for the case without decryption, finish here
state.processedMessages = transform(state.messages, [](const auto &m) {
state.processedMessages = transform<QVector<QXmppMessage>>(state.messages, [](const auto &m) {
return parseMamMessage(m, Unencrypted);
});
state.finish();
Expand Down
12 changes: 5 additions & 7 deletions src/client/QXmppPubSubManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "QXmppUtils.h"
#include "QXmppUtils_p.h"

#include "Algorithms.h"

#include <QDomElement>

using namespace QXmpp::Private;
Expand Down Expand Up @@ -1037,14 +1039,10 @@ auto QXmppPubSubManager::publishItems(PubSubIqBase &&request) -> QXmppTask<Publi

return chainIq(client()->sendIq(std::move(request)), this,
[](const PubSubIq<> &iq) -> PublishItemsResult {
const auto itemToId = [](const QXmppPubSubBaseItem &item) {
return item.id();
};

const auto items = iq.items();
QVector<QString> ids(items.size());
std::transform(items.begin(), items.end(), ids.begin(), itemToId);
return ids;
return transform<QVector<QString>>(items, [](const auto &item) {
return item.id();
});
});
}
/// \endcond
6 changes: 4 additions & 2 deletions tests/qxmpphttpuploadmanager/tst_qxmpphttpuploadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#include "QXmppTlsManager_p.h"
#include "QXmppUploadRequestManager.h"

#include "Algorithms.h"
#include "IntegrationTesting.h"
#include "TestClient.h"
#include "util.h"

#include <QMimeDatabase>

using namespace QXmpp::Private;

static const auto UPLOAD_SERVICE_NAME = QStringLiteral("upload.montague.tld");
constexpr quint64 MAX_FILE_SIZE = 500UL * 1024UL * 1024UL;

Expand Down Expand Up @@ -378,8 +381,7 @@ void tst_QXmppHttpUploadManager::testUpload()
// get server items
auto items = expectVariant<QList<DiscoItem>>(wait(disco->requestDiscoItems(test.configuration().domain()).toFuture(this)));
// request disco info for each item
std::vector<QXmppTask<DiscoInfoResult>> infoFutures;
std::transform(items.cbegin(), items.cend(), std::back_inserter(infoFutures), [disco](const auto &item) {
auto infoFutures = transform<std::vector<QXmppTask<DiscoInfoResult>>>(items, [disco](const auto &item) {
return disco->requestDiscoInfo(item.jid(), item.node());
});
auto uploadServiceJid = [&]() {
Expand Down

0 comments on commit 9d728b6

Please sign in to comment.