diff options
Diffstat (limited to 'options/value-traits.hpp')
| -rw-r--r-- | options/value-traits.hpp | 127 |
1 files changed, 100 insertions, 27 deletions
diff --git a/options/value-traits.hpp b/options/value-traits.hpp index ac5f6e5c..145cd924 100644 --- a/options/value-traits.hpp +++ b/options/value-traits.hpp @@ -1,49 +1,122 @@ -#include "export.hpp" +#pragma once #include "slider.hpp" +#include "export.hpp" -#include <QString> - +#include <cmath> #include <type_traits> -namespace options { -namespace detail { +#include <QString> + +namespace options::detail { +template<typename t> +using cv_qualified = + std::conditional_t<std::is_fundamental_v<std::remove_cvref_t<t>>, + std::remove_cvref_t<t>, + std::add_lvalue_reference_t<std::add_const_t<std::remove_cvref_t<t>>>>; template<typename t, typename u = t, typename Enable = void> struct default_value_traits { - using element_type = std::decay_t<t>; - using value_type = std::decay_t<u>; + using value_type = t; + using stored_type = u; + + static inline + value_type value_with_default(cv_qualified<value_type> val, cv_qualified<value_type>) + { + return val; + } + + static inline + value_type value_from_storage(cv_qualified<stored_type> x) + { + return static_cast<value_type>(x); + } + + static inline + stored_type storage_from_value(cv_qualified<value_type> val) + { + return static_cast<stored_type>(val); + } + + static inline + value_type value_from_qvariant(const QVariant& x) + { + return value_from_storage(storage_from_qvariant(x)); + } + + static inline + QVariant qvariant_from_value(cv_qualified<value_type> val) + { + return qvariant_from_storage(storage_from_value(val)); + } + + static constexpr inline + value_type pass_value(cv_qualified<value_type> x) + { + if constexpr(std::is_same_v<value_type, stored_type>) + return x; + else + return value_from_storage(storage_from_value(x)); + } - 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<value_type>(x); } - static inline element_type to_storage(const value_type& val) { return static_cast<element_type>(val); } + static inline + stored_type storage_from_qvariant(const QVariant& x) + { + // XXX TODO + return x.value<stored_type>(); + } + + static inline + QVariant qvariant_from_storage(cv_qualified<stored_type> val) + { + // XXX TODO + return QVariant::fromValue<stored_type>(val); + } + + static inline + bool is_equal(cv_qualified<value_type> x, cv_qualified<value_type> y) + { + return x == y; + } }; -template<typename t, typename u = t, typename Enable = void> -struct value_traits : default_value_traits<t, u, Enable> +template<typename t, typename Enable = void> +struct value_traits : default_value_traits<t> {}; + +template<> +inline +bool default_value_traits<double>::is_equal(double x, double y) { -}; + return std::fabs(x - y) < 1e-6; +} + +template<> struct value_traits<float, double> : default_value_traits<float> {}; template<> -struct value_traits<slider_value> : default_value_traits<slider_value> +inline +bool default_value_traits<float, double>::is_equal(float x, float y) { - static inline slider_value from_value(const slider_value& val, const slider_value& def) - { - return slider_value(val.cur(), def.min(), def.max()); - } -}; + return std::fabs(x - y) < 1e-6f; +} -// Qt uses int a lot in slots so use it for all enums -template<typename t> -struct value_traits<t, t, std::enable_if_t<std::is_enum_v<t>>> : public default_value_traits<int, t> +template<> +inline +slider_value default_value_traits<slider_value>::value_with_default(cv_qualified<slider_value> val, cv_qualified<slider_value> def) { -}; + return { val.cur(), def.min(), def.max() }; +} template<> -struct value_traits<float> : public default_value_traits<float, double, void> +inline +bool default_value_traits<slider_value>::is_equal(cv_qualified<slider_value> x, cv_qualified<slider_value> y) { -}; + return value_traits<double>::is_equal(x.cur(), y.cur()); +} + +// Qt uses int a lot in slots so use it for all enums +template<typename t> +struct value_traits<t, std::enable_if_t<std::is_enum_v<t>>> : default_value_traits<t, int> {}; + +} // ns options::detail -} // ns detail -} // ns options |
