File indexing completed on 2024-09-01 03:44:14
0001 /* 0002 This file is part of the KContacts framework. 0003 SPDX-FileCopyrightText: 2021 Ahmad Samir <a.samirh78@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef PARAMETERMAP_P_H 0009 #define PARAMETERMAP_P_H 0010 0011 #include <QDataStream> 0012 #include <QMap> 0013 #include <QStringList> 0014 0015 #include <algorithm> 0016 #include <vector> 0017 0018 namespace KContacts 0019 { 0020 struct ParameterData { 0021 QString param; 0022 QStringList paramValues; 0023 0024 bool operator<(const ParameterData &other) const 0025 { 0026 return param < other.param; 0027 } 0028 }; 0029 0030 class ParameterMap : public std::vector<ParameterData> 0031 { 0032 public: 0033 inline const_iterator findParam(const QString ¶m) const 0034 { 0035 return std::find_if(cbegin(), cend(), [¶m](const ParameterData &info) { 0036 return info.param == param; 0037 }); 0038 } 0039 0040 inline iterator findParam(const QString ¶m) 0041 { 0042 return std::find_if(begin(), end(), [¶m](const ParameterData &info) { 0043 return info.param == param; 0044 }); 0045 } 0046 0047 /** 0048 * Inserts @p newdata and keeps the vector sorted, the latter needs to 0049 * be sorted for comparisons. 0050 */ 0051 inline iterator insertParam(ParameterData &&newdata) 0052 { 0053 auto dIt = std::lower_bound(begin(), end(), newdata); 0054 return insert(dIt, newdata); 0055 } 0056 0057 inline QString toString() const 0058 { 0059 if (empty()) { 0060 return {}; 0061 } 0062 0063 QString str; 0064 for (const auto &[p, list] : *this) { 0065 str += QStringLiteral("%1 %2").arg(p, list.join(QLatin1Char(','))); 0066 } 0067 0068 return QLatin1String(" parameters: ") + str + QLatin1Char('\n'); 0069 } 0070 0071 static ParameterMap fromQMap(const QMap<QString, QStringList> ¶ms) 0072 { 0073 ParameterMap vec; 0074 vec.reserve(params.size()); 0075 for (auto it = params.begin(), endIt = params.cend(); it != endIt; ++it) { 0076 vec.push_back({it.key(), it.value()}); 0077 } 0078 return vec; 0079 } 0080 0081 inline QMap<QString, QStringList> toQMap() const 0082 { 0083 QMap<QString, QStringList> map; 0084 for (const auto &[name, list] : *this) { 0085 map.insert(name, list); 0086 } 0087 return map; 0088 } 0089 }; 0090 0091 inline bool operator==(const ParameterData &a, const ParameterData &b) 0092 { 0093 return std::tie(a.param, a.paramValues) == std::tie(b.param, b.paramValues); 0094 } 0095 0096 inline QDataStream &operator<<(QDataStream &s, const ParameterMap &map) 0097 { 0098 s << static_cast<quint32>(map.size()); 0099 for (const auto &[p, list] : map) { 0100 s << p << list; 0101 } 0102 return s; 0103 } 0104 0105 inline QDataStream &operator>>(QDataStream &s, ParameterMap &map) 0106 { 0107 map.clear(); 0108 quint32 n; 0109 s >> n; 0110 for (quint32 i = 0; i < n; ++i) { 0111 QString p; 0112 QStringList lst; 0113 s >> p >> lst; 0114 if (s.status() != QDataStream::Ok) { 0115 map.clear(); 0116 break; 0117 } 0118 map.push_back({p, lst}); 0119 } 0120 0121 return s; 0122 } 0123 } 0124 0125 #endif