/* * SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez * * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL */ #pragma once #include #include #include #include #include class OneTimeAction : public QObject { public: // the int argument is only so the compile knows to tell the constructors apart OneTimeAction(int /*unnecessary*/, const std::function &func, QObject *parent) : QObject(parent) , m_function(func) { } OneTimeAction(const std::function &func, QObject *parent) : QObject(parent) , m_function([func] { func(); return true; }) { } void trigger() { if (m_done) { return; } m_done = m_function(); deleteLater(); } private: std::function m_function; bool m_done = false; }; template static T kTransform(const Q &input, _UnaryOperation op) { T ret; ret.reserve(input.size()); for (const auto &v : input) { ret += op(v); } return ret; } template static T kTransform(const Q &input) { T ret; ret.reserve(input.size()); for (const auto &v : input) { ret += v; } return ret; } template static T kAppend(const Q &input, _UnaryOperation op) { T ret; ret.reserve(input.size()); for (const auto &v : input) { ret.append(op(v)); } return ret; } template static T kFilter(const Q &input, _UnaryOperation op) { T ret; for (const auto &v : input) { if (op(v)) { ret += v; } } return ret; } template static void kFilterInPlace(Q &input, _UnaryOperation op) { for (auto it = input.begin(); it != input.end();) { if (op(*it)) { ++it; } else { it = input.erase(it); } } } template static T kFilterTransform(Q &input, _UnaryOperation op) { T ret; for (const auto &v : input) { const std::optional result = op(v); if (result.has_value()) { ret += result.value(); } } return ret; } template static int kIndexOf(const Q &list, W func) { int i = 0; for (auto it = list.constBegin(), itEnd = list.constEnd(); it != itEnd; ++it) { if (func(*it)) { return i; } ++i; } return -1; } template static bool kContains(const Q &list, W func) { return std::any_of(list.begin(), list.end(), func); } template static bool kContainsValue(const Q &list, W value) { return std::find(list.begin(), list.end(), value) != list.end(); } template static QVector kSetToVector(const QSet &set) { QVector ret; ret.reserve(set.size()); for (auto &x : set) { ret.append(x); } return ret; } template static QList kSetToList(const QSet &set) { QList ret; ret.reserve(set.size()); for (auto &x : set) { ret.append(x); } return ret; } template static QSet kToSet(const QList &set) { return QSet(set.begin(), set.end()); } template static void kRemoveDuplicates(QList &input) { QSet ret; for (auto it = input.begin(); it != input.end();) { if (!ret.contains(*it)) { ret << *it; ++it; } else { it = input.erase(it); } } } class ElapsedDebug : private QElapsedTimer { public: ElapsedDebug(const QString &name = QStringLiteral("")) : m_name(name) { start(); } ~ElapsedDebug() { qDebug("elapsed %s: %lld!", m_name.toUtf8().constData(), elapsed()); } void step(const QString &step) { qDebug("step %s(%s): %lld!", m_name.toUtf8().constData(), qPrintable(step), elapsed()); } QString m_name; }; inline void swap(QJsonValueRef v1, QJsonValueRef v2) { QJsonValue temp(v1); v1 = QJsonValue(v2); v2 = temp; } template class KeyValueRange { public: KeyValueRange(T &data) : m_data{data} { } auto begin() { return m_data.keyValueBegin(); } auto end() { return m_data.keyValueEnd(); } private: T &m_data; };