RestLink 2.2.0
Powerfull Rest Client for Qt
Loading...
Searching...
No Matches
parameterlist.h
1#ifndef RESTLINK_PARAMETERLIST_H
2#define RESTLINK_PARAMETERLIST_H
3
4#include <RestLink/global.h>
5#include <RestLink/parameter.h>
6
7#include <QtCore/qlist.h>
8#include <QtCore/qvariant.h>
9
10namespace RestLink {
11
12template<typename P = Parameter>
13class RESTLINK_EXPORT ParameterList : public QList<P>
14{
15public:
17 ParameterList(const QList<P> &other);
18
19 bool contains(const QString &name) const;
20 QVariant value(const QString &name) const;
21 QVariantList values(const QString &name) const;
22 P parameter(const QString &name) const;
23
24 void setParameter(const QString &name, const QVariant &value);
25 void setParameter(const QString &name, const QVariantList &values);
26 void setParameter(const P &param);
27
28 void addParameter(const QString &name, const QVariant &value);
29 void addParameter(const QString &name, const QVariantList &values);
30 void addParameter(const P &param);
31
32 void removeParameter(const QString &name);
33 void removeParameter(const QString &name, const QVariant &value);
34 void removeParameter(const QString &name, const QVariantList &values);
35
36 QList<P> parameters() const;
37
38private:
39 P newParameter(const QString &name, const QVariantList &values) const;
40
41 typename QList<P>::ConstIterator find(const QString &name, bool *found) const;
42 typename QList<P>::Iterator find(const QString &name, bool *found);
43};
44
45template<typename P>
49
50template<typename P>
51inline ParameterList<P>::ParameterList(const QList<P> &other)
52 : QList<P>(other)
53{
54}
55
56template<typename P>
57bool ParameterList<P>::contains(const QString &name) const
58{
59 bool found = false;
60 find(name, &found);
61 return found;
62}
63
64template<typename P>
65QVariant ParameterList<P>::value(const QString &name) const
66{
67 bool found = false;
68 auto it = find(name, &found);
69 return (found && !it->values().isEmpty() ? it->values().first() : QVariant());
70}
71
72template<typename P>
73QVariantList ParameterList<P>::values(const QString &name) const
74{
75 bool found = false;
76 auto it = find(name, &found);
77 return (found ? it->values() : QVariantList());
78}
79
80template<typename P>
81P ParameterList<P>::parameter(const QString &name) const
82{
83 bool found = false;
84 auto it = find(name, &found);
85 return (found ? *it : P());
86}
87
88template<typename P>
89void ParameterList<P>::setParameter(const QString &name, const QVariant &value)
90{
91 setParameter(newParameter(name, {value}));
92}
93
94template<typename P>
95void ParameterList<P>::setParameter(const QString &name, const QVariantList &values)
96{
97 setParameter(newParameter(name, values));
98}
99
100template<typename P>
102{
103 bool found = false;
104 auto it = find(param.name(), &found);
105
106 if (found)
107 it->setValues(param.values());
108 else
109 append(param);
110}
111
112template<typename P>
113void ParameterList<P>::addParameter(const QString &name, const QVariant &value)
114{
115 addParameter(newParameter(name, {value}));
116}
117
118template<typename P>
119void ParameterList<P>::addParameter(const QString &name, const QVariantList &values)
120{
121 addParameter(newParameter(name, values));
122}
123
124template<typename P>
126{
127 bool found = false;
128 auto it = find(param.name(), &found);
129
130 if (found)
131 it->addValue(param.values());
132 else
133 this->append(param);
134}
135
136template<typename P>
137void ParameterList<P>::removeParameter(const QString &name)
138{
139 removeParameter(name, QVariantList());
140}
141
142template<typename P>
143void ParameterList<P>::removeParameter(const QString &name, const QVariant &value)
144{
145 removeParameter(name, { value });
146}
147
148template<typename P>
149void ParameterList<P>::removeParameter(const QString &name, const QVariantList &values)
150{
151 bool found = false;
152 auto it = find(name, &found);
153
154 if (found && (values.isEmpty() || it->values() == values))
155 this->removeAt(std::distance(this->begin(), it));
156}
157
158template<typename P>
159P ParameterList<P>::newParameter(const QString &name, const QVariantList &values) const
160{
161 P param;
162 param.setName(name);
163 param.setValues(values);
164 return param;
165}
166
167template<typename P>
168typename QList<P>::Iterator ParameterList<P>::find(const QString &name, bool *found)
169{
170 auto it = std::find_if(this->begin(), this->end(), [&name](const Parameter &param) {
171 return param.name() == name;
172 });
173
174 *found = (it != this->end() ? true : false);
175 return it;
176}
177
178template<typename P>
179typename QList<P>::ConstIterator ParameterList<P>::find(const QString &name, bool *found) const
180{
181 auto it = std::find_if(this->begin(), this->end(), [&name](const Parameter &param) {
182 return param.name() == name;
183 });
184
185 *found = (it != this->end() ? true : false);
186 return it;
187}
188
189} // namespace RestLink
190
191#endif // RESTLINK_PARAMETERLIST_H