diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2017-05-12 15:44:05 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2017-05-12 15:48:16 +0200 |
commit | f7ab2dc5a54c573cd7408a68cb1e93174827719e (patch) | |
tree | 683e3cda0a007f39ac97605850af94939d66bade /options/value.hpp | |
parent | 4c4c783d023cf1bb6a8d7d883bf8d3384f7b7da1 (diff) |
options: split up value.hpp header
Also combine the traits classes and make them more useful.
Diffstat (limited to 'options/value.hpp')
-rw-r--r-- | options/value.hpp | 166 |
1 files changed, 32 insertions, 134 deletions
diff --git a/options/value.hpp b/options/value.hpp index 020cbeea..ebb2096c 100644 --- a/options/value.hpp +++ b/options/value.hpp @@ -12,153 +12,59 @@ #include "compat/util.hpp" -#include "connector.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> -#define OPENTRACK_DEFINE_SLOT(t) void setValue(t datum) { store(datum); } -#define OPENTRACK_DEFINE_SIGNAL(t) void valueChanged(t) const namespace options { namespace detail { -template<typename t> struct value_type_traits { using type = t;}; -template<> struct value_type_traits<QString> { using type = const QString&; }; -template<> struct value_type_traits<slider_value> { using type = const slider_value&; }; -template<typename u> struct value_type_traits<QList<u>> -{ - using type = const QList<u>&; -}; -template<typename t> using value_type_t = typename value_type_traits<t>::type; -} - -class OTR_OPTIONS_EXPORT base_value : public QObject -{ - Q_OBJECT - friend class ::options::detail::connector; - using comparator = bool(*)(const QVariant& val1, const QVariant& val2); -public: - QString name() const { return self_name; } - base_value(bundle b, const QString& name, comparator cmp, std::type_index type_idx); - ~base_value() override; -signals: - OPENTRACK_DEFINE_SIGNAL(double); - OPENTRACK_DEFINE_SIGNAL(float); - OPENTRACK_DEFINE_SIGNAL(int); - OPENTRACK_DEFINE_SIGNAL(bool); - OPENTRACK_DEFINE_SIGNAL(const QString&); - OPENTRACK_DEFINE_SIGNAL(const slider_value&); - OPENTRACK_DEFINE_SIGNAL(const QPointF&); - OPENTRACK_DEFINE_SIGNAL(const QVariant&); - - OPENTRACK_DEFINE_SIGNAL(const QList<double>&); - OPENTRACK_DEFINE_SIGNAL(const QList<float>&); - OPENTRACK_DEFINE_SIGNAL(const QList<int>&); - OPENTRACK_DEFINE_SIGNAL(const QList<bool>&); - OPENTRACK_DEFINE_SIGNAL(const QList<QString>&); - OPENTRACK_DEFINE_SIGNAL(const QList<slider_value>&); - OPENTRACK_DEFINE_SIGNAL(const QList<QPointF>&); -protected: - bundle b; - QString self_name; - comparator cmp; - std::type_index type_index; - - void store(const QVariant& datum) - { - b->store_kv(self_name, datum); - } - - template<typename t> - void store(const t& datum) - { - b->store_kv(self_name, QVariant::fromValue(datum)); - } +OTR_OPTIONS_EXPORT void acct_lookup(bool is_fresh); -public slots: - OPENTRACK_DEFINE_SLOT(double) - OPENTRACK_DEFINE_SLOT(int) - OPENTRACK_DEFINE_SLOT(bool) - OPENTRACK_DEFINE_SLOT(const QString&) - OPENTRACK_DEFINE_SLOT(const slider_value&) - OPENTRACK_DEFINE_SLOT(const QPointF&) - OPENTRACK_DEFINE_SLOT(const QVariant&) - - OPENTRACK_DEFINE_SLOT(const QList<double>&) - OPENTRACK_DEFINE_SLOT(const QList<float>&) - OPENTRACK_DEFINE_SLOT(const QList<int>&) - OPENTRACK_DEFINE_SLOT(const QList<bool>&) - OPENTRACK_DEFINE_SLOT(const QList<QString>&) - OPENTRACK_DEFINE_SLOT(const QList<slider_value>&) - OPENTRACK_DEFINE_SLOT(const QList<QPointF>&) - - virtual void reload() = 0; - virtual void bundle_value_changed() const = 0; - virtual void set_to_default() = 0; -}; +} // ns detail -namespace detail { template<typename t> -struct value_get_traits +class value final : public base_value { - static inline t get(const t& val, const t&) - { - return val; - } -}; + using traits = detail::value_traits<t, t, void>; + using element_type = typename traits::element_type; -template<> -struct value_get_traits<slider_value> -{ - using t = slider_value; - static inline t get(const t& val, const t& def) + static bool is_equal(const QVariant& val1, const QVariant& val2) { - return t(val.cur(), def.min(), def.max()); + return val1.value<element_type>() == val2.value<element_type>(); } -}; -template<typename t, typename Enable = void> -struct value_element_type -{ - using type = typename std::remove_reference<typename std::remove_cv<t>::type>::type; -}; - -// Qt uses int a lot in slots so use it for all enums -template<typename t> -struct value_element_type<t, typename std::enable_if<std::is_enum<t>::value>::type> -{ - using type = int; -}; - -template<> struct value_element_type<float, void> { using type = double; }; - -template<typename t> using value_element_type_t = typename value_element_type<t>::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)); -template<typename t> -class value final : public base_value -{ - static bool is_equal(const QVariant& val1, const QVariant& val2) - { - return val1.value<element_type>() == val2.value<element_type>(); + return traits::from_value(traits::from_storage(x), def); } public: - using element_type = detail::value_element_type_t<t>; - OTR_NEVER_INLINE t operator=(const t& datum) { - const element_type tmp = static_cast<element_type>(datum); - if (tmp != get()) - store(tmp); + if (datum != get()) + store(traits::to_storage(datum)); return datum; } @@ -167,13 +73,12 @@ public: 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) + 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); - if (!b->contains(name) || b->get<QVariant>(name).type() == QVariant::Invalid) - *this = def; } OTR_NEVER_INLINE @@ -181,6 +86,7 @@ public: { } + OTR_NEVER_INLINE t default_value() const { return def; @@ -193,7 +99,7 @@ public: } OTR_NEVER_INLINE - operator t() const { return get(); } + operator t() const { return std::forward<t>(get()); } OTR_NEVER_INLINE void reload() override @@ -201,34 +107,26 @@ public: *this = static_cast<t>(*this); } + OTR_NEVER_INLINE void bundle_value_changed() const override { - emit valueChanged(static_cast<detail::value_type_t<t>>(get())); + emit valueChanged(traits::to_storage(get())); } OTR_NEVER_INLINE t operator()() const { - return static_cast<t>(get()); + return get(); } template<typename u> OTR_NEVER_INLINE u to() const { - return static_cast<u>(get()); + return static_cast<u>(std::forward<t>(get())); } private: - OTR_NEVER_INLINE - t get() const - { - t val = b->contains(self_name) - ? static_cast<t>(b->get<element_type>(self_name)) - : def; - return detail::value_get_traits<t>::get(val, def); - } - const t def; }; |