summaryrefslogtreecommitdiffhomepage
path: root/options/value-traits.hpp
blob: ac5f6e5c689ca5daf7680a1022f17686db641b06 (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
#include "export.hpp"

#include "slider.hpp"

#include <QString>

#include <type_traits>

namespace options {
namespace detail {

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>;

    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); }
};

template<typename t, typename u = t, typename Enable = void>
struct value_traits : default_value_traits<t, u, Enable>
{
};

template<>
struct value_traits<slider_value> : default_value_traits<slider_value>
{
    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<typename t>
struct value_traits<t, t, std::enable_if_t<std::is_enum_v<t>>> : public default_value_traits<int, t>
{
};

template<>
struct value_traits<float> : public default_value_traits<float, double, void>
{
};

} // ns detail
} // ns options