summaryrefslogtreecommitdiffhomepage
path: root/options/value.hpp
blob: ebb2096c43ece31f01bb728cc58d55a07c5889ce (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
/* Copyright (c) 2015-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 "export.hpp"

#include "compat/util.hpp"

#include "bundle.hpp"
#include "slider.hpp"
#include "base-value.hpp"
#include "value-traits.hpp"

#include <cstdio>
#include <type_traits>
#include <typeinfo>
#include <typeindex>
#include <utility>

#include <QVariant>
#include <QString>
#include <QPointF>
#include <QList>
#include <QMutex>

namespace options {

namespace detail {

OTR_OPTIONS_EXPORT void acct_lookup(bool is_fresh);

} // ns detail

template<typename t>
class value final : public base_value
{
    using traits = detail::value_traits<t, t, void>;
    using element_type = typename traits::element_type;

    static bool is_equal(const QVariant& val1, const QVariant& val2)
    {
        return val1.value<element_type>() == val2.value<element_type>();
    }

    OTR_NEVER_INLINE
    t get() const
    {
        if (!b->contains(self_name) || b->get<QVariant>(self_name).type() == QVariant::Invalid)
            return def;

        const element_type x(b->get<element_type>(self_name));

        return traits::from_value(traits::from_storage(x), def);
    }

public:
    OTR_NEVER_INLINE
    t operator=(const t& datum)
    {
        if (datum != get())
            store(traits::to_storage(datum));
        return datum;
    }

    static constexpr const Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection;
    static constexpr const Qt::ConnectionType SAFE_CONNTYPE = Qt::QueuedConnection;

    OTR_NEVER_INLINE
    value(bundle b, const QString& name, t def) :
        base_value(b, name, &is_equal, std::type_index(typeid(element_type))),
        def(def)
    {
        QObject::connect(b.get(), SIGNAL(reloading()),
                         this, SLOT(reload()),
                         DIRECT_CONNTYPE);
    }

    OTR_NEVER_INLINE
    value(bundle b, const char* name, t def) : value(b, QString(name), def)
    {
    }

    OTR_NEVER_INLINE
    t default_value() const
    {
        return def;
    }

    OTR_NEVER_INLINE
    void set_to_default() override
    {
        *this = def;
    }

    OTR_NEVER_INLINE
    operator t() const { return std::forward<t>(get()); }

    OTR_NEVER_INLINE
    void reload() override
    {
        *this = static_cast<t>(*this);
    }

    OTR_NEVER_INLINE
    void bundle_value_changed() const override
    {
        emit valueChanged(traits::to_storage(get()));
    }

    OTR_NEVER_INLINE
    t operator()() const
    {
        return get();
    }

    template<typename u>
    OTR_NEVER_INLINE
    u to() const
    {
        return static_cast<u>(std::forward<t>(get()));
    }

private:
    const t def;
};

} // ns options