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
|
/* 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 "bundle.hpp"
#include "slider.hpp"
#include "base-value.hpp"
#include "value-traits.hpp"
#include "compat/macros.hpp"
#include <type_traits>
#include <utility>
#include <QMetaType>
namespace options::detail {
template<typename t>
class dereference_wrapper final
{
t x;
public:
constexpr t* operator->() { return &x; }
constexpr explicit dereference_wrapper(t&& x) : x(x) {}
};
template<typename t, typename...> /*MSVC workaround*/ static constexpr bool is_enum_v = std::is_enum_v<t>;
} // ns options::detail
namespace options {
template<typename t>
class value final : public value_
{
static_assert(std::is_same_v<t, std::remove_cvref_t<t>>);
mutable QMutex mtx;
const t def;
mutable t cached_value;
using traits = detail::value_traits<t>;
never_inline
auto get() const noexcept
{
if (!b->contains(self_name))
return traits::pass_value(def);
QVariant variant = b->get_variant(self_name);
if (variant.isNull() || !variant.isValid())
return traits::pass_value(def);
return traits::pass_value(traits::value_with_default(traits::value_from_qvariant(variant), def));
}
never_inline
void store_variant(QVariant&& value) noexcept override
{
if (traits::is_equal(get(), traits::value_from_qvariant(value)))
return;
if (is_null())
return;
if (value.isValid() && !value.isNull())
b->store_kv(self_name, value);
else
b->store_kv(self_name, traits::qvariant_from_value(def));
}
never_inline
void store_variant(const QVariant& value) noexcept override
{
QVariant copy{value};
store_variant(std::move(copy));
}
bool is_null() const
{
return self_name.isEmpty() || b->name().isEmpty();
}
public:
using signal_sig = typename value_::signal_sig_<t>;
using slot_sig = typename value_::slot_sig_<t>;
QVariant get_variant() const noexcept override
{
if (QVariant ret{b->get_variant(self_name)}; ret.isValid() && !ret.isNull())
return ret;
return traits::qvariant_from_value(def);
}
never_inline
void notify_() const override
{
auto x = get();
{
QMutexLocker l(&mtx);
cached_value = x;
}
maybe_trace("notify +");
emit valueChanged(traits::storage_from_value(x));
maybe_trace("notify -");
}
never_inline
void notify() const override
{
if (is_null())
return;
auto x = get();
{
QMutexLocker l(&mtx);
if (traits::is_equal(x, cached_value))
{
//maybe_trace("notify ~");
return;
}
else
{
cached_value = x;
l.unlock();
maybe_trace("notify +");
emit valueChanged(traits::storage_from_value(x));
maybe_trace("notify -");
}
}
}
auto& operator=(t&& datum) noexcept
{
if (is_null())
return *this;
store_variant(traits::qvariant_from_value(traits::pass_value(datum)));
return *this;
}
auto& operator=(const t& datum) noexcept
{
if (is_null())
return *this;
t copy{datum};
*this = std::move(copy);
return *this;
}
auto& operator=(const value<t>& datum) noexcept
{
*this = *datum;
return *this;
}
static constexpr Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection;
static constexpr Qt::ConnectionType SAFE_CONNTYPE = Qt::QueuedConnection;
value(bundle b, const QString& name, t def) noexcept : value_(b, name), def(std::move(def)), cached_value{get()}
{
}
//value(const value<t>& other) noexcept : value{other.b, other.self_name, other.def} {}
value(const value<t>&) = delete;
t default_value() const
{
return def;
}
void set_to_default() noexcept override
{
*this = def;
}
operator t() const { return get(); }
template<typename u>
explicit force_inline operator u() const { return to<u>(); }
auto operator->() const noexcept
{
return detail::dereference_wrapper<t>{get()};
}
force_inline auto operator()() const noexcept { return get(); }
force_inline auto operator*() const noexcept { return get(); }
template<typename u>
u to() const noexcept
{
return static_cast<u>(get());
}
template<typename Q, typename F>
QMetaObject::Connection
connect_to(Q* qobject, F&& writer, Qt::ConnectionType conn = Qt::QueuedConnection) {
return QObject::connect(this, static_cast<signal_sig>(&value<t>::valueChanged), qobject, std::forward<F>(writer), conn);
}
template<typename Q, typename F>
QMetaObject::Connection
connect_from(Q* qobject, F&& reader, Qt::ConnectionType conn = Qt::DirectConnection) {
return QObject::connect(qobject, std::forward<F>(reader), this, static_cast<slot_sig>(&value<t>::setValue), conn);
}
template<typename Q, typename F, typename G>
QMetaObject::Connection
connect_from(Q* qobject, F&& reader, G&& fn, Qt::ConnectionType conn = Qt::DirectConnection) {
return QObject::connect(qobject, std::forward<F>(reader), this, std::forward<G>(fn), conn);
}
};
#if !defined OTR_OPTIONS_INST_VALUE
# define OTR_OPTIONS_INST_VALUE OTR_TEMPLATE_IMPORT
#endif
OTR_OPTIONS_INST_VALUE(value<double>)
OTR_OPTIONS_INST_VALUE(value<float>)
OTR_OPTIONS_INST_VALUE(value<int>)
OTR_OPTIONS_INST_VALUE(value<bool>)
OTR_OPTIONS_INST_VALUE(value<QString>)
OTR_OPTIONS_INST_VALUE(value<slider_value>)
OTR_OPTIONS_INST_VALUE(value<QPointF>)
OTR_OPTIONS_INST_VALUE(value<QVariant>)
OTR_OPTIONS_INST_VALUE(value<QList<double>>)
OTR_OPTIONS_INST_VALUE(value<QList<float>>)
OTR_OPTIONS_INST_VALUE(value<QList<int>>)
OTR_OPTIONS_INST_VALUE(value<QList<bool>>)
OTR_OPTIONS_INST_VALUE(value<QList<QString>>)
OTR_OPTIONS_INST_VALUE(value<QList<slider_value>>)
OTR_OPTIONS_INST_VALUE(value<QList<QPointF>>)
OTR_OPTIONS_INST_VALUE(value<QList<QVariant>>)
} // ns options
|