Add settings transform function

This commit is contained in:
Dan Paulat 2024-06-21 20:47:53 -05:00
parent a7a30347ae
commit 45d3e45923
2 changed files with 22 additions and 6 deletions

View file

@ -30,6 +30,7 @@ public:
std::optional<T> staged_ {}; std::optional<T> staged_ {};
std::optional<T> minimum_ {}; std::optional<T> minimum_ {};
std::optional<T> maximum_ {}; std::optional<T> maximum_ {};
std::function<T(const T&)> transform_ {};
std::function<bool(const T&)> validator_ {nullptr}; std::function<bool(const T&)> validator_ {nullptr};
boost::unordered_flat_map<boost::uuids::uuid, ValueCallbackFunction> boost::unordered_flat_map<boost::uuids::uuid, ValueCallbackFunction>
@ -79,7 +80,7 @@ bool SettingsVariable<T>::SetValue(const T& value)
if (Validate(value)) if (Validate(value))
{ {
p->value_ = value; p->value_ = (p->transform_ != nullptr) ? p->transform_(value) : value;
validated = true; validated = true;
for (auto& callback : p->valueChangedCallbackFunctions_) for (auto& callback : p->valueChangedCallbackFunctions_)
@ -102,7 +103,7 @@ bool SettingsVariable<T>::SetValueOrDefault(const T& value)
if (Validate(value)) if (Validate(value))
{ {
p->value_ = value; p->value_ = (p->transform_ != nullptr) ? p->transform_(value) : value;
validated = true; validated = true;
} }
else if (p->minimum_.has_value() && value < p->minimum_) else if (p->minimum_.has_value() && value < p->minimum_)
@ -182,9 +183,11 @@ bool SettingsVariable<T>::StageValue(const T& value)
if (Validate(value)) if (Validate(value))
{ {
if (p->value_ != value) T transformed = (p->transform_ != nullptr) ? p->transform_(value) : value;
if (p->value_ != transformed)
{ {
p->staged_ = value; p->staged_ = transformed;
} }
else else
{ {
@ -195,7 +198,7 @@ bool SettingsVariable<T>::StageValue(const T& value)
for (auto& callback : p->valueStagedCallbackFunctions_) for (auto& callback : p->valueStagedCallbackFunctions_)
{ {
callback.second(value); callback.second(transformed);
} }
} }
@ -285,6 +288,12 @@ std::optional<T> SettingsVariable<T>::GetMaximum() const
return p->maximum_; return p->maximum_;
} }
template<class T>
void SettingsVariable<T>::SetTransform(std::function<T(const T&)> transform)
{
p->transform_ = transform;
}
template<class T> template<class T>
void SettingsVariable<T>::SetValidator(std::function<bool(const T&)> validator) void SettingsVariable<T>::SetValidator(std::function<bool(const T&)> validator)
{ {
@ -384,7 +393,7 @@ bool SettingsVariable<T>::Equals(const SettingsVariableBase& o) const
// This is only ever called with SettingsVariable<T>, so static_cast is safe // This is only ever called with SettingsVariable<T>, so static_cast is safe
const SettingsVariable<T>& v = static_cast<const SettingsVariable<T>&>(o); const SettingsVariable<T>& v = static_cast<const SettingsVariable<T>&>(o);
// Don't compare validator // Don't compare transform or validator
return SettingsVariableBase::Equals(o) && // return SettingsVariableBase::Equals(o) && //
p->value_ == v.p->value_ && // p->value_ == v.p->value_ && //
p->default_ == v.p->default_ && // p->default_ == v.p->default_ && //

View file

@ -165,6 +165,13 @@ public:
*/ */
void SetMaximum(const T& value); void SetMaximum(const T& value);
/**
* Sets a custom transform function for the settings variable.
*
* @param transform Transform function
*/
void SetTransform(std::function<T(const T&)> transform);
/** /**
* Sets a custom validator function for the settings variable. * Sets a custom validator function for the settings variable.
* *