QEloquent 1.1.0
Qt most flexible ORM.
Loading...
Searching...
No Matches
datamap.h
1#ifndef QELOQUENT_DATAMAP_H
2#define QELOQUENT_DATAMAP_H
3
4#include <QEloquent/global.h>
5#include <QEloquent/listproxy.h>
6
7#include <QPair>
8#include <QVector>
9#include <QHash>
10#include <QVariant>
11
12namespace QEloquent {
13
14using DataMapKey = QString;
15using DataMapValue = QVariant;
16using DataMapPair = QPair<DataMapKey, DataMapValue>;
17
18class QELOQUENT_EXPORT DataMap : public AbstractListProxy<DataMapPair>
19{
20public:
21 using Key = DataMapKey;
22 using Value = DataMapValue;
23 using Pair = QPair<Key, Value>;
24
25 DataMap();
26 DataMap(const std::initializer_list<Pair> &data);
27 DataMap(DataMap &&other) = default;
28 DataMap(const DataMap &other) = default;
29 DataMap& operator=(const DataMap &) = default;
30 DataMap& operator=(DataMap &&) = default;
31
32 QList<Key> keys() const;
33 QList<Value> values() const;
34
35 bool contains(const Key &key) const;
36 Value value(const Key &key) const;
37 void insert(const Key &key, const Value &value);
38 void insert(const Key &key, const DataMap &map);
39 void insert(const Key &key, const QList<DataMap> &maps);
40 void insert(const DataMap &map);
41 void remove(const Key &key);
42 void clear();
43
44 void forEach(const std::function<void(const Pair &, const DataMap &)> &callback, int depth = 1) const;
45 void forEach(const std::function<void(Pair &)> &callback, int depth = 1);
46
47 void removeIf(const std::function<bool(const Pair &)> &pred);
48
49 QVariantMap toVariantMap() const;
50 static DataMap fromVariantMap(const QVariantMap &map);
51
52 void computeIndexes();
53
54 const QVector<Pair> &intenalData() const;
55 QVector<Pair> &intenalData();
56
57 static DataMap fromVariant(const QVariant &var);
58
59protected:
60 const QVector<Pair> *constList() const override;
61 QVector<Pair> *mutableList() override;
62
63private:
64 static QHash<Key, int> generateIndex(const QVector<Pair> &data);
65
66 QVector<Pair> m_data;
67 QHash<Key, int> m_index;
68};
69
70} // namespace QEloquent
71
72Q_DECLARE_METATYPE(QEloquent::DataMap)
73
74QELOQUENT_EXPORT QDebug operator<<(QDebug debug, const QEloquent::DataMap &map);
75
76QELOQUENT_EXPORT QDataStream &operator<<(QDataStream &out, const QEloquent::DataMap &map);
77QELOQUENT_EXPORT QDataStream &operator>>(QDataStream &in, QEloquent::DataMap &map);
78
79#endif // QELOQUENT_DATAMAP_H