From f7ab2dc5a54c573cd7408a68cb1e93174827719e Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 12 May 2017 15:44:05 +0200 Subject: options: split up value.hpp header Also combine the traits classes and make them more useful. --- options/base-value.cpp | 27 ++++++++ options/base-value.hpp | 85 ++++++++++++++++++++++++ options/value-traits.hpp | 50 ++++++++++++++ options/value.cpp | 35 ---------- options/value.hpp | 166 +++++++++-------------------------------------- 5 files changed, 194 insertions(+), 169 deletions(-) create mode 100644 options/base-value.cpp create mode 100644 options/base-value.hpp create mode 100644 options/value-traits.hpp delete mode 100644 options/value.cpp diff --git a/options/base-value.cpp b/options/base-value.cpp new file mode 100644 index 00000000..29f4b496 --- /dev/null +++ b/options/base-value.cpp @@ -0,0 +1,27 @@ +#include "base-value.hpp" + +using namespace options; + +base_value::base_value(bundle b, const QString& name, base_value::comparator cmp, std::type_index type_idx) : + b(b), + self_name(name), + cmp(cmp), + type_index(type_idx) +{ + b->on_value_created(name, this); +} + +base_value::~base_value() +{ + b->on_value_destructed(self_name, this); +} + +void base_value::store(const QVariant& datum) +{ + b->store_kv(self_name, datum); +} + +void ::options::detail::set_base_value_to_default(base_value* val) +{ + val->set_to_default(); +} diff --git a/options/base-value.hpp b/options/base-value.hpp new file mode 100644 index 00000000..4f90e3fc --- /dev/null +++ b/options/base-value.hpp @@ -0,0 +1,85 @@ +#pragma once + +#include "bundle.hpp" +#include "slider.hpp" +#include "connector.hpp" + +#include "export.hpp" + +#include +#include +#include +#include +#include + +#include + +#define OPENTRACK_DEFINE_SLOT(t) void setValue(t datum) { store(datum); } +#define OPENTRACK_DEFINE_SIGNAL(t) void valueChanged(t) const + +namespace options { + +class OTR_OPTIONS_EXPORT base_value : public QObject +{ + Q_OBJECT + friend class 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&); + OPENTRACK_DEFINE_SIGNAL(const QList&); + OPENTRACK_DEFINE_SIGNAL(const QList&); + OPENTRACK_DEFINE_SIGNAL(const QList&); + OPENTRACK_DEFINE_SIGNAL(const QList&); + OPENTRACK_DEFINE_SIGNAL(const QList&); + OPENTRACK_DEFINE_SIGNAL(const QList&); +protected: + bundle b; + QString self_name; + comparator cmp; + std::type_index type_index; + + void store(const QVariant& datum); + + template + void store(const t& datum) + { + b->store_kv(self_name, QVariant::fromValue(datum)); + } + +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&) + OPENTRACK_DEFINE_SLOT(const QList&) + OPENTRACK_DEFINE_SLOT(const QList&) + OPENTRACK_DEFINE_SLOT(const QList&) + OPENTRACK_DEFINE_SLOT(const QList&) + OPENTRACK_DEFINE_SLOT(const QList&) + OPENTRACK_DEFINE_SLOT(const QList&) + + virtual void reload() = 0; + virtual void bundle_value_changed() const = 0; + virtual void set_to_default() = 0; +}; + +} //ns options diff --git a/options/value-traits.hpp b/options/value-traits.hpp new file mode 100644 index 00000000..cf12649c --- /dev/null +++ b/options/value-traits.hpp @@ -0,0 +1,50 @@ +#include "export.hpp" + +#include "compat/functional.hpp" +#include "slider.hpp" + +#include + +#include + +namespace options { +namespace detail { + +template +struct default_value_traits +{ + using element_type = remove_qualifiers; + using value_type = u; + + static inline value_type from_value(const value_type& val, const value_type&) { return val; } + static inline value_type from_storage(const element_type& x) { return static_cast(x); } + static inline element_type to_storage(const value_type& val) { return static_cast(val); } +}; + +template +struct value_traits : default_value_traits +{ +}; + +template<> +struct value_traits : default_value_traits +{ + static inline slider_value from_value(const slider_value& val, const slider_value& def) + { + return slider_value(val.cur(), def.min(), def.max()); + } +}; + +// Qt uses int a lot in slots so use it for all enums +template +struct value_traits::value>> : public default_value_traits +{ +}; + +template<> +struct value_traits : public default_value_traits +{ +}; + +} // ns detail +} // ns options diff --git a/options/value.cpp b/options/value.cpp deleted file mode 100644 index d3d24a58..00000000 --- a/options/value.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (c) 2015-2016, Stanislaw Halik - - * 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. - */ - -#include "value.hpp" - -namespace options { - -base_value::base_value(bundle b, const QString& name, base_value::comparator cmp, std::type_index type_idx) : - b(b), - self_name(name), - cmp(cmp), - type_index(type_idx) -{ - b->on_value_created(name, this); -} - -base_value::~base_value() -{ - b->on_value_destructed(self_name, this); -} - -namespace detail -{ - void set_base_value_to_default(base_value* val) - { - val->set_to_default(); - } -} - -} // ns options 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 #include #include #include +#include + +#include #include #include #include +#include -#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 struct value_type_traits { using type = t;}; -template<> struct value_type_traits { using type = const QString&; }; -template<> struct value_type_traits { using type = const slider_value&; }; -template struct value_type_traits> -{ - using type = const QList&; -}; -template using value_type_t = typename value_type_traits::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&); - OPENTRACK_DEFINE_SIGNAL(const QList&); - OPENTRACK_DEFINE_SIGNAL(const QList&); - OPENTRACK_DEFINE_SIGNAL(const QList&); - OPENTRACK_DEFINE_SIGNAL(const QList&); - OPENTRACK_DEFINE_SIGNAL(const QList&); - OPENTRACK_DEFINE_SIGNAL(const QList&); -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 - 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&) - OPENTRACK_DEFINE_SLOT(const QList&) - OPENTRACK_DEFINE_SLOT(const QList&) - OPENTRACK_DEFINE_SLOT(const QList&) - OPENTRACK_DEFINE_SLOT(const QList&) - OPENTRACK_DEFINE_SLOT(const QList&) - OPENTRACK_DEFINE_SLOT(const QList&) - - virtual void reload() = 0; - virtual void bundle_value_changed() const = 0; - virtual void set_to_default() = 0; -}; +} // ns detail -namespace detail { template -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; + using element_type = typename traits::element_type; -template<> -struct value_get_traits -{ - 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() == val2.value(); } -}; -template -struct value_element_type -{ - using type = typename std::remove_reference::type>::type; -}; - -// Qt uses int a lot in slots so use it for all enums -template -struct value_element_type::value>::type> -{ - using type = int; -}; - -template<> struct value_element_type { using type = double; }; - -template using value_element_type_t = typename value_element_type::type; + OTR_NEVER_INLINE + t get() const + { + if (!b->contains(self_name) || b->get(self_name).type() == QVariant::Invalid) + return def; -} + const element_type x(b->get(self_name)); -template -class value final : public base_value -{ - static bool is_equal(const QVariant& val1, const QVariant& val2) - { - return val1.value() == val2.value(); + return traits::from_value(traits::from_storage(x), def); } public: - using element_type = detail::value_element_type_t; - OTR_NEVER_INLINE t operator=(const t& datum) { - const element_type tmp = static_cast(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(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(get()); } OTR_NEVER_INLINE void reload() override @@ -201,34 +107,26 @@ public: *this = static_cast(*this); } + OTR_NEVER_INLINE void bundle_value_changed() const override { - emit valueChanged(static_cast>(get())); + emit valueChanged(traits::to_storage(get())); } OTR_NEVER_INLINE t operator()() const { - return static_cast(get()); + return get(); } template OTR_NEVER_INLINE u to() const { - return static_cast(get()); + return static_cast(std::forward(get())); } private: - OTR_NEVER_INLINE - t get() const - { - t val = b->contains(self_name) - ? static_cast(b->get(self_name)) - : def; - return detail::value_get_traits::get(val, def); - } - const t def; }; -- cgit v1.2.3