summaryrefslogtreecommitdiffhomepage
path: root/options/bundle.hpp
blob: a948ac44e5eeef0552e7534299bf013169a6c1ee (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
#pragma once

#include "group.hpp"
#include "connector.hpp"

#include <memory>
#include <tuple>
#include <map>
#include <memory>
#include <vector>

#include <QObject>
#include <QString>
#include <QVariant>
#include <QMutex>
#include <QMutexLocker>

#include <QDebug>

#include "compat/util.hpp"
#include "export.hpp"

namespace options {

namespace detail {

class OPENTRACK_OPTIONS_EXPORT bundle final : public QObject, public virtual connector
{
    Q_OBJECT
private:
    QMutex mtx;
    const QString group_name;
    group saved;
    group transient;

    bundle(const bundle&) = delete;
    bundle& operator=(const bundle&) = delete;
    QMutex* get_mtx() override { return &mtx; }

signals:
    void reloading();
    void saving() const;
public:
    bundle(const QString& group_name);
    ~bundle() override {}
    QString name() { return group_name; }
    void reload();
    void store_kv(const QString& name, const QVariant& datum);
    bool contains(const QString& name) const;
    void save();
    void save_deferred(QSettings& s);
    bool modifiedp() const;

    template<typename t>
    t get(const QString& name) const
    {
        QMutexLocker l(const_cast<QMutex*>(&mtx));
        return transient.get<t>(name);
    }
};

struct OPENTRACK_OPTIONS_EXPORT bundler
{
public:
    using k = QString;
    using v = bundle;
    using cnt = int;
    using tt = std::tuple<cnt, std::weak_ptr<v>>;
private:
    QMutex implsgl_mtx;
    std::map<k, tt> implsgl_data;
    void after_profile_changed_();
public:
    bundler();
    ~bundler();
    std::shared_ptr<v> make_bundle(const k& key);
    void bundle_decf(const k& key);
    static void refresh_all_bundles();
};

OPENTRACK_OPTIONS_EXPORT bundler& singleton();
}

using bundle_type = detail::bundle;
using bundle = std::shared_ptr<bundle_type>;

OPENTRACK_OPTIONS_EXPORT bundle make_bundle(const QString& name);

}