Branch data Line data Source code
1 : : // Copyright (C) 2021 The Qt Company Ltd.
2 : : // Copyright (C) 2019 Luxoft Sweden AB
3 : : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4 : :
5 : : #ifndef CONFIGCACHE_H
6 : : #define CONFIGCACHE_H
7 : :
8 : : #include <functional>
9 : : #include <array>
10 : :
11 : : #include <QtCore/QPair>
12 : : #include <QtCore/QVector>
13 : : #include <QtCore/QStringList>
14 : : #include <QtCore/QVariant>
15 : : #include <QtCore/QIODevice>
16 : : #include <QtAppManCommon/global.h>
17 : :
18 : : QT_BEGIN_NAMESPACE_AM
19 : :
20 : : class ConfigCachePrivate;
21 : :
22 : : template <typename T> class ConfigCacheAdaptor
23 : : {
24 : : public:
25 : : T *loadFromSource(QIODevice *source, const QString &fileName);
26 : : T *loadFromCache(QDataStream &ds);
27 : : void saveToCache(QDataStream &ds, const T *t);
28 : : void merge(T *to, const T *from) { *to = *from; }
29 : : };
30 : :
31 : : class AbstractConfigCache
32 : : {
33 : : public:
34 : : enum Option {
35 : : None = 0x0,
36 : : MergedResult = 0x1,
37 : : NoCache = 0x2,
38 : : ClearCache = 0x4,
39 : : IgnoreBroken = 0x8,
40 : : };
41 : : Q_DECLARE_FLAGS(Options, Option)
42 : :
43 : : AbstractConfigCache(const QStringList &configFiles, const QString &cacheBaseName,
44 : : std::array<char, 4> typeId, quint32 version = 0, Options options = None);
45 : : virtual ~AbstractConfigCache();
46 : :
47 : : virtual void parse();
48 : :
49 : : void *takeMergedResult() const;
50 : : void *takeResult(int index) const;
51 : : void *takeResult(const QString &rawFile) const;
52 : :
53 : : void clear();
54 : :
55 : : // mainly for debugging and auto tests
56 : : bool parseReadFromCache() const;
57 : : bool parseWroteToCache() const;
58 : : QString cacheFilePath() const;
59 : :
60 : : protected:
61 : : virtual void *loadFromSource(QIODevice *source, const QString &fileName) = 0;
62 : : virtual void preProcessSourceContent(QByteArray &sourceContent, const QString &fileName) = 0;
63 : : virtual void *loadFromCache(QDataStream &ds) = 0;
64 : : virtual void saveToCache(QDataStream &ds, const void *t) = 0;
65 : : virtual void merge(void *to, const void *from) = 0;
66 : : virtual void *clone(void *t) = 0;
67 : : virtual void destruct(void *t) = 0;
68 : :
69 : : private:
70 : : Q_DISABLE_COPY_MOVE(AbstractConfigCache)
71 : :
72 : : ConfigCachePrivate *d;
73 : : };
74 : :
75 : : template <typename T, typename ADAPTOR = ConfigCacheAdaptor<T>> class ConfigCache : public AbstractConfigCache
76 : : {
77 : : public:
78 : : using AbstractConfigCache::Option;
79 : : using AbstractConfigCache::Options;
80 : :
81 : 135 : ConfigCache(const QStringList &configFiles, const QString &cacheBaseName, std::array<char, 4> typeId,
82 : : quint32 typeVersion = 0, Options options = None)
83 : : : AbstractConfigCache(configFiles, cacheBaseName, typeId, typeVersion, options)
84 [ + - + - : 135 : , m_adaptor()
+ - + - +
- + - ]
85 : : { }
86 : :
87 : 135 : ~ConfigCache() override
88 : : {
89 : 135 : clear();
90 : 135 : }
91 : :
92 : 135 : void parse() override
93 : : {
94 [ + - + - : 135 : AbstractConfigCache::parse();
+ - + - +
- - + ]
95 : 134 : }
96 : :
97 : 59 : T *takeMergedResult() const
98 : : {
99 [ + - + - ]: 59 : return static_cast<T *>(AbstractConfigCache::takeMergedResult());
100 : : }
101 : 85 : T *takeResult(int index) const
102 : : {
103 [ + - + - ]: 85 : return static_cast<T *>(AbstractConfigCache::takeResult(index));
104 : : }
105 : : T *takeResult(const QString &yamlFile) const
106 : : {
107 : : return static_cast<T *>(AbstractConfigCache::takeResult(yamlFile));
108 : : }
109 : :
110 : : protected:
111 : 151 : void *loadFromSource(QIODevice *source, const QString &fileName) override
112 : 151 : { return m_adaptor.loadFromSource(source, fileName); }
113 : 158 : void preProcessSourceContent(QByteArray &sourceContent, const QString &fileName) override
114 : 158 : { m_adaptor.preProcessSourceContent(sourceContent, fileName); }
115 : 11 : void *loadFromCache(QDataStream &ds) override
116 : 11 : { return m_adaptor.loadFromCache(ds); }
117 : 96 : void saveToCache(QDataStream &ds, const void *t) override
118 : 96 : { m_adaptor.saveToCache(ds, static_cast<const T *>(t)); }
119 : 8 : void merge(void *to, const void *from) override
120 : 8 : { m_adaptor.merge(static_cast<T *>(to), static_cast<const T *>(from)); }
121 : 57 : void *clone(void *t) override
122 : 57 : { return doClone(static_cast<T *>(t)); }
123 : 486 : void destruct(void *t) override
124 [ + + ]: 486 : { delete static_cast<T *>(t); }
125 : :
126 : : private:
127 : : ADAPTOR m_adaptor;
128 : :
129 : : template <typename U = T> typename std::enable_if<std::is_copy_constructible<U>::value, void *>::type
130 : 57 : doClone(T *t)
131 [ + - ]: 57 : { return new T(*static_cast<T *>(t)); }
132 : : template <typename U = T> typename std::enable_if<!std::is_copy_constructible<U>::value, void *>::type
133 : : doClone(T *)
134 : : { Q_ASSERT_X(false, "ConfigCache::clone", "trying to clone non-copy-constructible content"); return nullptr; }
135 : :
136 : : Q_DISABLE_COPY_MOVE(ConfigCache)
137 : : };
138 : :
139 : : Q_DECLARE_OPERATORS_FOR_FLAGS(AbstractConfigCache::Options)
140 : :
141 : : QT_END_NAMESPACE_AM
142 : :
143 : : #endif // CONFIGCACHE_H
|