diff --git a/libdiscover/utils.h b/libdiscover/utils.h index d58aa0f2..6066a2a4 100644 --- a/libdiscover/utils.h +++ b/libdiscover/utils.h @@ -1,134 +1,145 @@ /*************************************************************************** * Copyright ?? 2012 Aleix Pol Gonzalez * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * ***************************************************************************/ #ifndef UTILS_H #define UTILS_H #include #include #include class OneTimeAction : public QObject { public: OneTimeAction(const std::function &func, QObject* parent) : QObject(parent), m_function(func) {} void trigger() { m_function(); deleteLater(); } private: std::function m_function; }; 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 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 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 QVector kSetToVector(const QSet & set) { QVector ret; ret.reserve(set.size()); for(auto &x: set) ret.append(x); return ret; } template static QSet kToSet(const QVector & set) { #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) return QSet(set.begin(), set.end()); #else QSet ret; ret.reserve(set.size()); for(auto &x: set) ret.insert(x); return ret; #endif } template static QSet kToSet(const QList & set) { #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) return QSet(set.begin(), set.end()); #else return set.toSet(); #endif } 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; }; class CallOnDestroy : public QObject { public: CallOnDestroy(std::function f) : m_func(std::move(f)) {} ~CallOnDestroy() { m_func(); } private: std::function m_func; }; #endif