summaryrefslogtreecommitdiffhomepage
path: root/options/bundle.hpp
blob: 98291fc5cd072ac5887d210cd3b43e5cd06a9ba4 (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
/* Copyright (c) 2013-2016, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#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 "export.hpp"

namespace options::detail {
    class bundle;
} // ns options::detail

namespace options {
    using bundle_ = detail::bundle;
    using bundle = std::shared_ptr<bundle_>;
    OTR_OPTIONS_EXPORT std::shared_ptr<detail::bundle> make_bundle(const QString& name);
} // ns options

namespace options::detail {

struct bundler;

class OTR_OPTIONS_EXPORT bundle final : public QObject, public connector
{
    Q_OBJECT

    class OTR_OPTIONS_EXPORT mutex final : public QMutex
    {
    public:
        explicit mutex(QMutex::RecursionMode mode) : QMutex(mode) {}
        operator QMutex*() const { return const_cast<QMutex*>(static_cast<const QMutex*>(this)); }
    };

private:
    mutex mtx;
    const QString group_name;
    group saved;
    group transient;

signals:
    void reloading();
    void saving() const;
    void changed() const;

public:
    bundle(const bundle&) = delete;
    bundle(bundle&&) = delete;
    bundle& operator=(bundle&&) = delete;
    bundle& operator=(const bundle&) = delete;
    QMutex* get_mtx() const override;

    cc_noinline explicit bundle(const QString& group_name);
    cc_noinline ~bundle() override;
    QString name() const { return group_name; }
    cc_noinline void store_kv(const QString& name, const QVariant& datum);
    cc_noinline bool contains(const QString& name) const;
    cc_noinline bool is_modified() const;

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

public slots:
    void save();
    void reload();
    void set_all_to_default();
};

struct OTR_OPTIONS_EXPORT bundler final
{
    using k = QString;
    using v = bundle;
    using weak = std::weak_ptr<v>;
    using shared = std::shared_ptr<v>;

private:
    QMutex implsgl_mtx { QMutex::Recursive };
    std::map<k, weak> implsgl_data;
    void after_profile_changed_();

public:
    static void refresh_all_bundles();

private:
    friend OTR_OPTIONS_EXPORT
    std::shared_ptr<v> options::make_bundle(const QString& name);

    std::shared_ptr<v> make_bundle_(const k& key);

    static bundler& bundler_singleton();

    bundler();
    ~bundler();
};

void set_base_value_to_default(value_* val);

} // ns options::detail