aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/tools/persistence.h
blob: ecabf9523a2469f2d735d907be6371881e4e7562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QBS_PERSISTENCE
#define QBS_PERSISTENCE

#include "persistentobject.h"
#include <logging/logger.h>

#include <QtCore/qdatastream.h>
#include <QtCore/qprocess.h>
#include <QtCore/qsharedpointer.h>
#include <QtCore/qstring.h>
#include <QtCore/qvariant.h>
#include <QtCore/qvector.h>

#include <type_traits>

namespace qbs {
namespace Internal {

class PersistentPool
{
public:
    PersistentPool(Logger &logger);
    ~PersistentPool();

    class HeadData
    {
    public:
        QVariantMap projectConfig;
    };

    // We need a helper class template, because we require partial specialization for some of
    // the aggregate types, which is not possible with function templates.
    // The generic implementation assumes that T is of class type and has load() and store()
    // member functions.
    template<typename T, typename Enable = void> struct Helper
    {
        static void store(const T &object, PersistentPool *pool) { object.store(*pool); }
        static void load(T &object, PersistentPool *pool) { object.load(*pool); }
    };

    template<typename T> void store(const T &value) { Helper<T>().store(value, this); }
    template<typename T> void load(T &value) { Helper<T>().load(value, this); }
    template<typename T> T load() {
        T tmp;
        Helper<T>().load(tmp, this);
        return tmp;
    }

    void load(const QString &filePath);
    void setupWriteStream(const QString &filePath);
    void finalizeWriteStream();
    void closeStream();
    void clear();

    const HeadData &headData() const { return m_headData; }
    void setHeadData(const HeadData &hd) { m_headData = hd; }

private:
    typedef int PersistentObjectId;

    template <typename T> T *idLoad();
    template <class T> QSharedPointer<T> idLoadS();
    template <class T> T *loadRaw(PersistentObjectId id);
    template <class T> QSharedPointer<T> load(PersistentObjectId id);

    void storePersistentObject(const PersistentObject *object);

    void storeVariant(const QVariant &variant);
    QVariant loadVariant();

    void storeString(const QString &t);
    QString loadString(int id);
    QString idLoadString();

    QDataStream m_stream;
    HeadData m_headData;
    QVector<PersistentObject *> m_loadedRaw;
    QVector<QSharedPointer<PersistentObject> > m_loaded;
    QHash<const PersistentObject *, int> m_storageIndices;
    PersistentObjectId m_lastStoredObjectId;

    QVector<QString> m_stringStorage;
    QHash<QString, int> m_inverseStringStorage;
    PersistentObjectId m_lastStoredStringId;
    Logger &m_logger;
};

template <typename T> inline T *PersistentPool::idLoad()
{
    PersistentObjectId id;
    m_stream >> id;
    return loadRaw<T>(id);
}

template <class T> inline QSharedPointer<T> PersistentPool::idLoadS()
{
    PersistentObjectId id;
    m_stream >> id;
    return load<T>(id);
}

template <class T> inline T *PersistentPool::loadRaw(PersistentObjectId id)
{
    if (id < 0)
        return 0;

    if (id < m_loadedRaw.count()) {
        PersistentObject *obj = m_loadedRaw.value(id);
        return dynamic_cast<T*>(obj);
    }

    int i = m_loadedRaw.count();
    m_loadedRaw.resize(id + 1);
    for (; i < m_loadedRaw.count(); ++i)
        m_loadedRaw[i] = 0;

    T * const t = new T;
    PersistentObject * const po = t;
    m_loadedRaw[id] = po;
    po->load(*this);
    return t;
}

template <class T> inline QSharedPointer<T> PersistentPool::load(PersistentObjectId id)
{
    if (id < 0)
        return QSharedPointer<T>();

    if (id < m_loaded.count()) {
        QSharedPointer<PersistentObject> obj = m_loaded.value(id);
        return obj.dynamicCast<T>();
    }

    m_loaded.resize(id + 1);
    const QSharedPointer<T> t = T::create();
    m_loaded[id] = t;
    PersistentObject * const po = t.data();
    po->load(*this);
    return t;
}

/***** Specializations of Helper class *****/

template<typename T>
struct PersistentPool::Helper<T, typename std::enable_if<std::is_integral<T>::value>::type>
{
    static void store(const T &value, PersistentPool *pool) { pool->m_stream << value; }
    static void load(T &value, PersistentPool *pool) { pool->m_stream >> value; }
};

// TODO: Use constexpr function once we require MSVC 2015.
template<typename T> struct IsPersistentObject
{
    static const bool value = std::is_base_of<PersistentObject, T>::value;
};

template<typename T>
struct PersistentPool::Helper<QSharedPointer<T>,
                              typename std::enable_if<IsPersistentObject<T>::value>::type>
{
    static void store(const QSharedPointer<T> &value, PersistentPool *pool)
    {
        pool->store(value.data());
    }
    static void load(QSharedPointer<T> &value, PersistentPool *pool)
    {
        value = pool->idLoadS<typename std::remove_const<T>::type>();
    }
};

template<typename T>
struct PersistentPool::Helper<T *, typename std::enable_if<IsPersistentObject<T>::value>::type>
{
    static void store(const T *value, PersistentPool *pool) { pool->storePersistentObject(value); }
    void load(T* &value, PersistentPool *pool) { value = pool->idLoad<T>(); }
};

template<> struct PersistentPool::Helper<QString>
{
    static void store(const QString &s, PersistentPool *pool) { pool->storeString(s); }
    static void load(QString &s, PersistentPool *pool) { s = pool->idLoadString(); }
};

template<> struct PersistentPool::Helper<QVariant>
{
    static void store(const QVariant &v, PersistentPool *pool) { pool->storeVariant(v); }
    static void load(QVariant &v, PersistentPool *pool) { v = pool->loadVariant(); }
};

template<> struct PersistentPool::Helper<QProcessEnvironment>
{
    static void store(const QProcessEnvironment &env, PersistentPool *pool)
    {
        const QStringList &keys = env.keys();
        pool->store(keys.count());
        for (const QString &key : keys) {
            pool->store(key);
            pool->store(env.value(key));
        }
    }
    static void load(QProcessEnvironment &env, PersistentPool *pool)
    {
        const int count = pool->load<int>();
        for (int i = 0; i < count; ++i) {
            const auto &key = pool->load<QString>();
            const auto &value = pool->load<QString>();
            env.insert(key, value);
        }
    }
};
template<typename T, typename U> struct PersistentPool::Helper<QPair<T, U>>
{
    static void store(const QPair<T, U> &pair, PersistentPool *pool)
    {
        pool->store(pair.first);
        pool->store(pair.second);
    }
    static void load(QPair<T, U> &pair, PersistentPool *pool)
    {
        pool->load(pair.first);
        pool->load(pair.second);
    }
};

class ArtifactSet;
class FileTags;
template<typename T> struct IsSimpleContainer { static const bool value = false; };
template<> struct IsSimpleContainer<ArtifactSet> { static const bool value = true; };
template<> struct IsSimpleContainer<FileTags> { static const bool value = true; };
template<> struct IsSimpleContainer<QStringList> { static const bool value = true; };
template<typename T> struct IsSimpleContainer<QVector<T>> { static const bool value = true; };
template<typename T> struct IsSimpleContainer<QList<T>> { static const bool value = true; };
template<typename T> struct IsSimpleContainer<QSet<T>> { static const bool value = true; };

template<typename T>
struct PersistentPool::Helper<T, typename std::enable_if<IsSimpleContainer<T>::value>::type>
{
    static void store(const T &container, PersistentPool *pool)
    {
        pool->store(container.count());
        for (auto it = container.cbegin(); it != container.cend(); ++it)
            pool->store(*it);
    }
    static void load(T &container, PersistentPool *pool)
    {
        const int count = pool->load<int>();
        container.clear();
        container.reserve(count);
        for (int i = count; --i >= 0;)
            container += pool->load<typename T::value_type>();
    }
};

template<typename T> struct IsKeyValueContainer { static const bool value = false; };
template<typename K, typename V> struct IsKeyValueContainer<QMap<K, V>>
{
    static const bool value = true;
};
template<typename K, typename V> struct IsKeyValueContainer<QHash<K, V>>
{
    static const bool value = true;
};

template<typename T>
struct PersistentPool::Helper<T, typename std::enable_if<IsKeyValueContainer<T>::value>::type>
{
    static void store(const T &container, PersistentPool *pool)
    {
        pool->store(container.count());
        for (auto it = container.cbegin(); it != container.cend(); ++it) {
            pool->store(it.key());
            pool->store(it.value());
        }
    }
    static void load(T &container, PersistentPool *pool)
    {
        container.clear();
        const int count = pool->load<int>();
        for (int i = 0; i < count; ++i) {
            const auto &key = pool->load<typename T::key_type>();
            const auto &value = pool->load<typename T::mapped_type>();
            container.insert(key, value);
        }
    }
};

} // namespace Internal
} // namespace qbs

#endif