mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 19:10:06 +00:00
Settings variable
This commit is contained in:
parent
65fd47968a
commit
db665ed9ec
5 changed files with 266 additions and 12 deletions
159
scwx-qt/source/scwx/qt/settings/settings_variable.cpp
Normal file
159
scwx-qt/source/scwx/qt/settings/settings_variable.cpp
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
#include <scwx/qt/settings/settings_variable.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
{
|
||||
namespace settings
|
||||
{
|
||||
|
||||
template class SettingsVariable<bool>;
|
||||
template class SettingsVariable<int64_t>;
|
||||
template class SettingsVariable<std::string>;
|
||||
template class SettingsVariable<std::vector<int64_t>>;
|
||||
|
||||
static const std::string logPrefix_ = "scwx::qt::settings::settings_variable";
|
||||
|
||||
template<class T>
|
||||
class SettingsVariable<T>::Impl
|
||||
{
|
||||
public:
|
||||
explicit Impl(const std::string& name) : name_ {name} {}
|
||||
|
||||
~Impl() {}
|
||||
|
||||
bool Validate(const T& value);
|
||||
|
||||
const std::string name_;
|
||||
T value_ {};
|
||||
T default_ {};
|
||||
std::optional<T> staged_ {};
|
||||
std::optional<T> minimum_ {};
|
||||
std::optional<T> maximum_ {};
|
||||
std::function<bool(const T&)> validator_ {nullptr};
|
||||
};
|
||||
|
||||
template<class T>
|
||||
SettingsVariable<T>::SettingsVariable(const std::string& name) :
|
||||
p(std::make_unique<Impl>(name))
|
||||
{
|
||||
}
|
||||
template<class T>
|
||||
SettingsVariable<T>::~SettingsVariable() = default;
|
||||
|
||||
template<class T>
|
||||
SettingsVariable<T>::SettingsVariable(SettingsVariable&&) noexcept = default;
|
||||
template<class T>
|
||||
SettingsVariable<T>&
|
||||
SettingsVariable<T>::operator=(SettingsVariable&&) noexcept = default;
|
||||
|
||||
template<class T>
|
||||
std::string SettingsVariable<T>::name() const
|
||||
{
|
||||
return p->name_;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T SettingsVariable<T>::GetValue() const
|
||||
{
|
||||
return p->value_;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool SettingsVariable<T>::SetValue(const T& value)
|
||||
{
|
||||
bool validated = false;
|
||||
|
||||
if (p->Validate(value))
|
||||
{
|
||||
p->value_ = value;
|
||||
validated = true;
|
||||
}
|
||||
|
||||
return validated;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool SettingsVariable<T>::SetValueOrDefault(const T& value)
|
||||
{
|
||||
bool validated = false;
|
||||
|
||||
if (p->Validate(value))
|
||||
{
|
||||
p->value_ = value;
|
||||
validated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->value_ = p->default_;
|
||||
}
|
||||
|
||||
return validated;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool SettingsVariable<T>::StageValue(const T& value)
|
||||
{
|
||||
bool validated = false;
|
||||
|
||||
if (p->Validate(value))
|
||||
{
|
||||
p->staged_ = value;
|
||||
validated = true;
|
||||
}
|
||||
|
||||
return validated;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsVariable<T>::Commit()
|
||||
{
|
||||
if (p->staged_.has_value())
|
||||
{
|
||||
p->value_ = std::move(p->staged_.value());
|
||||
p->staged_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T SettingsVariable<T>::GetDefault() const
|
||||
{
|
||||
return p->default_;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsVariable<T>::SetDefault(const T& value)
|
||||
{
|
||||
p->default_ = value;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsVariable<T>::SetMinimum(const T& value)
|
||||
{
|
||||
p->minimum_ = value;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsVariable<T>::SetMaximum(const T& value)
|
||||
{
|
||||
p->maximum_ = value;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsVariable<T>::SetValidator(std::function<bool(const T&)> validator)
|
||||
{
|
||||
p->validator_ = validator;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool SettingsVariable<T>::Impl::Validate(const T& value)
|
||||
{
|
||||
return ((!minimum_.has_value() || value >= minimum_) && // Validate minimum
|
||||
(!maximum_.has_value() || value <= maximum_) && // Validate maximum
|
||||
(validator_ == nullptr || validator_(value))); // User-validation
|
||||
}
|
||||
|
||||
} // namespace settings
|
||||
} // namespace qt
|
||||
} // namespace scwx
|
||||
Loading…
Add table
Add a link
Reference in a new issue