summaryrefslogtreecommitdiffhomepage
path: root/opentrack-compat/options.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-06-12 23:00:55 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-06-14 18:14:46 +0200
commit9ed8d925822b2aee15899487e822480abb88541e (patch)
treeb1927153b6d5726c9f84b4a9310796569d3eec8b /opentrack-compat/options.hpp
parent0ba585a1d91b812158277c09aa890cedbc080790 (diff)
compat/options: use traits to prevent slider min/max persistence
Without it, the serialized min and max member vars were set in stone despite further code changes. Now only the current value is persisted. Add clamp for cur/min/max slider values. Store default value as t rather than underlying_t since it's always been casted anyway. Add trivial comment, update copyright.
Diffstat (limited to 'opentrack-compat/options.hpp')
-rw-r--r--opentrack-compat/options.hpp36
1 files changed, 30 insertions, 6 deletions
diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp
index e2c04929..17ab093c 100644
--- a/opentrack-compat/options.hpp
+++ b/opentrack-compat/options.hpp
@@ -1,10 +1,12 @@
-/* Copyright (c) 2013-2015 Stanislaw Halik
+/* Copyright (c) 2013-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.
*/
+// XXX TODO this header is too long
+
#pragma once
#include <memory>
@@ -243,6 +245,27 @@ namespace options {
virtual void reload() = 0;
};
+ namespace detail {
+ template<typename t>
+ struct value_get_traits
+ {
+ static inline t get(const t& val, const t&)
+ {
+ return val;
+ }
+ };
+
+ template<>
+ struct value_get_traits<slider_value>
+ {
+ using t = slider_value;
+ static inline t get(const t& val, const t& def)
+ {
+ return t(val.cur(), def.min(), def.max());
+ }
+ };
+ }
+
template<typename t_>
class value : public base_value
{
@@ -269,7 +292,7 @@ namespace options {
static constexpr const Qt::ConnectionType DIRECT_CONNTYPE = Qt::AutoConnection;
static constexpr const Qt::ConnectionType SAFE_CONNTYPE = Qt::QueuedConnection;
- value(pbundle b, const QString& name, t def) : base_value(b, name), def(static_cast<underlying_t>(def))
+ value(pbundle b, const QString& name, t def) : base_value(b, name), def(def)
{
QObject::connect(b.get(), SIGNAL(reloading()),
this, SLOT(reload()),
@@ -284,7 +307,10 @@ namespace options {
operator t() const
{
- return static_cast<t>(b->contains(self_name) ? b->get<underlying_t>(self_name) : def);
+ t val = b->contains(self_name)
+ ? static_cast<t>(b->get<underlying_t>(self_name))
+ : def;
+ return detail::value_get_traits<t>::get(val, def);
}
void reload() override
@@ -292,7 +318,7 @@ namespace options {
*this = static_cast<t>(*this);
}
private:
- underlying_t def;
+ t def;
};
struct OPENTRACK_COMPAT_EXPORT opts
@@ -470,8 +496,6 @@ namespace options {
slider_value sv(v);
- using std::max;
-
const double sv_max = sv.max();
const double sv_min = sv.min();
const double sv_c = sv_max - sv_min;