#include #include namespace scwx { namespace qt { namespace settings { template class SettingsVariable; template class SettingsVariable; template class SettingsVariable; template class SettingsVariable>; static const std::string logPrefix_ = "scwx::qt::settings::settings_variable"; template class SettingsVariable::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 staged_ {}; std::optional minimum_ {}; std::optional maximum_ {}; std::function validator_ {nullptr}; }; template SettingsVariable::SettingsVariable(const std::string& name) : p(std::make_unique(name)) { } template SettingsVariable::~SettingsVariable() = default; template SettingsVariable::SettingsVariable(SettingsVariable&&) noexcept = default; template SettingsVariable& SettingsVariable::operator=(SettingsVariable&&) noexcept = default; template std::string SettingsVariable::name() const { return p->name_; } template T SettingsVariable::GetValue() const { return p->value_; } template bool SettingsVariable::SetValue(const T& value) { bool validated = false; if (p->Validate(value)) { p->value_ = value; validated = true; } return validated; } template bool SettingsVariable::SetValueOrDefault(const T& value) { bool validated = false; if (p->Validate(value)) { p->value_ = value; validated = true; } else { p->value_ = p->default_; } return validated; } template bool SettingsVariable::StageValue(const T& value) { bool validated = false; if (p->Validate(value)) { p->staged_ = value; validated = true; } return validated; } template void SettingsVariable::Commit() { if (p->staged_.has_value()) { p->value_ = std::move(p->staged_.value()); p->staged_.reset(); } } template T SettingsVariable::GetDefault() const { return p->default_; } template void SettingsVariable::SetDefault(const T& value) { p->default_ = value; } template void SettingsVariable::SetMinimum(const T& value) { p->minimum_ = value; } template void SettingsVariable::SetMaximum(const T& value) { p->maximum_ = value; } template void SettingsVariable::SetValidator(std::function validator) { p->validator_ = validator; } template bool SettingsVariable::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