aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/store.h
blob: 97e71b0eecded88aaa6d6b196e53d1eb01e84697 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "expected.h"
#include "storekey.h"

#include <QMap>
#include <QVariant>

namespace Utils {

class QtcSettings;

using KeyList = QList<Key>;

class Store : public QMap<Key, QVariant>
{
public:
    using QMap<Key, QVariant>::QMap;

    template<typename T>
    void insertValueWithDefault(const Key &key, const T &val, const T &defaultValue)
    {
        if (val != defaultValue)
            insert(key, val);
    }

    template<typename T>
    void insertValueWithDefault(const Key &key, const T &val)
    {
        if (val != T())
            insert(key, val);
    }
};

using OldStore = QMap<QByteArray, QVariant>;

QTCREATOR_UTILS_EXPORT KeyList keysFromStrings(const QStringList &list);
QTCREATOR_UTILS_EXPORT QStringList stringsFromKeys(const KeyList &list);

QTCREATOR_UTILS_EXPORT QVariant variantFromStore(const Store &store);
QTCREATOR_UTILS_EXPORT Store storeFromVariant(const QVariant &value);

QTCREATOR_UTILS_EXPORT Store storeFromMap(const QVariantMap &map);
QTCREATOR_UTILS_EXPORT QVariantMap mapFromStore(const Store &store);

QTCREATOR_UTILS_EXPORT bool isStore(const QVariant &value);

QTCREATOR_UTILS_EXPORT Key numberedKey(const Key &key, int number);

QTCREATOR_UTILS_EXPORT expected_str<Store> storeFromJson(const QByteArray &json);
QTCREATOR_UTILS_EXPORT QByteArray jsonFromStore(const Store &store);

// These recursively change type.
QTCREATOR_UTILS_EXPORT QVariant storeEntryFromMapEntry(const QVariant &value);
QTCREATOR_UTILS_EXPORT QVariant mapEntryFromStoreEntry(const QVariant &value);

// Don't use in new code.
QTCREATOR_UTILS_EXPORT Store storeFromSettings(const Key &groupKey, QtcSettings *s);
QTCREATOR_UTILS_EXPORT void storeToSettings(const Key &groupKey, QtcSettings *s, const Store &store);

} // Utils

Q_DECLARE_METATYPE(Utils::Store)
Q_DECLARE_METATYPE(Utils::OldStore)