File indexing completed on 2024-04-21 16:12:12

0001 /*
0002  *   SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QElapsedTimer>
0010 #include <QJsonValue>
0011 #include <QScopeGuard>
0012 #include <QString>
0013 #include <functional>
0014 
0015 class OneTimeAction : public QObject
0016 {
0017 public:
0018     // the int argument is only so the compile knows to tell the constructors apart
0019     OneTimeAction(int /*unnecessary*/, const std::function<bool()> &func, QObject *parent)
0020         : QObject(parent)
0021         , m_function(func)
0022     {
0023     }
0024 
0025     OneTimeAction(const std::function<void()> &func, QObject *parent)
0026         : QObject(parent)
0027         , m_function([func] {
0028             func();
0029             return true;
0030         })
0031     {
0032     }
0033 
0034     void trigger()
0035     {
0036         if (m_done)
0037             return;
0038         m_done = m_function();
0039         deleteLater();
0040     }
0041 
0042 private:
0043     std::function<bool()> m_function;
0044     bool m_done = false;
0045 };
0046 
0047 template<typename T, typename Q, typename _UnaryOperation>
0048 static T kTransform(const Q &input, _UnaryOperation op)
0049 {
0050     T ret;
0051     ret.reserve(input.size());
0052     for (const auto &v : input) {
0053         ret += op(v);
0054     }
0055     return ret;
0056 }
0057 
0058 template<typename T, typename Q>
0059 static T kTransform(const Q &input)
0060 {
0061     T ret;
0062     ret.reserve(input.size());
0063     for (const auto &v : input) {
0064         ret += v;
0065     }
0066     return ret;
0067 }
0068 
0069 template<typename T, typename Q, typename _UnaryOperation>
0070 static T kAppend(const Q &input, _UnaryOperation op)
0071 {
0072     T ret;
0073     ret.reserve(input.size());
0074     for (const auto &v : input) {
0075         ret.append(op(v));
0076     }
0077     return ret;
0078 }
0079 
0080 template<typename T, typename Q, typename _UnaryOperation>
0081 static T kFilter(const Q &input, _UnaryOperation op)
0082 {
0083     T ret;
0084     for (const auto &v : input) {
0085         if (op(v))
0086             ret += v;
0087     }
0088     return ret;
0089 }
0090 
0091 template<typename Q, typename W>
0092 static int kIndexOf(const Q &list, W func)
0093 {
0094     int i = 0;
0095     for (auto it = list.constBegin(), itEnd = list.constEnd(); it != itEnd; ++it) {
0096         if (func(*it))
0097             return i;
0098         ++i;
0099     }
0100     return -1;
0101 }
0102 
0103 template<typename Q, typename W>
0104 static bool kContains(const Q &list, W func)
0105 {
0106     return std::any_of(list.begin(), list.end(), func);
0107 }
0108 
0109 template<typename Q, typename W>
0110 static bool kContainsValue(const Q &list, W value)
0111 {
0112     return std::find(list.begin(), list.end(), value) != list.end();
0113 }
0114 
0115 template<typename T>
0116 static QVector<T> kSetToVector(const QSet<T> &set)
0117 {
0118     QVector<T> ret;
0119     ret.reserve(set.size());
0120     for (auto &x : set)
0121         ret.append(x);
0122     return ret;
0123 }
0124 template<typename T>
0125 static QList<T> kSetToList(const QSet<T> &set)
0126 {
0127     QList<T> ret;
0128     ret.reserve(set.size());
0129     for (auto &x : set)
0130         ret.append(x);
0131     return ret;
0132 }
0133 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0134 template<typename T>
0135 static QSet<T> kToSet(const QVector<T> &set)
0136 {
0137     return QSet<T>(set.begin(), set.end());
0138 }
0139 #endif
0140 template<typename T>
0141 static QSet<T> kToSet(const QList<T> &set)
0142 {
0143     return QSet<T>(set.begin(), set.end());
0144 }
0145 
0146 class ElapsedDebug : private QElapsedTimer
0147 {
0148 public:
0149     ElapsedDebug(const QString &name = QStringLiteral("<unnamed>"))
0150         : m_name(name)
0151     {
0152         start();
0153     }
0154     ~ElapsedDebug()
0155     {
0156         qDebug("elapsed %s: %lld!", m_name.toUtf8().constData(), elapsed());
0157     }
0158     void step(const QString &step)
0159     {
0160         qDebug("step %s(%s): %lld!", m_name.toUtf8().constData(), qPrintable(step), elapsed());
0161     }
0162 
0163     QString m_name;
0164 };
0165 
0166 inline void swap(QJsonValueRef v1, QJsonValueRef v2)
0167 {
0168     QJsonValue temp(v1);
0169     v1 = QJsonValue(v2);
0170     v2 = temp;
0171 }