diff --git a/lib/libimhex/include/hex/api/content_registry/settings.hpp b/lib/libimhex/include/hex/api/content_registry/settings.hpp index 6043fa999..b4cc925e9 100644 --- a/lib/libimhex/include/hex/api/content_registry/settings.hpp +++ b/lib/libimhex/include/hex/api/content_registry/settings.hpp @@ -336,7 +336,7 @@ EXPORT_MODULE namespace hex { template requires (!(std::is_reference_v || std::is_const_v)) void write(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, T value) { - impl::getSetting(unlocalizedCategory, unlocalizedName, value) = std::move(value); + impl::getSetting(unlocalizedCategory, unlocalizedName, value) = value; impl::runOnChangeHandlers(unlocalizedCategory, unlocalizedName, value); impl::store(); @@ -353,17 +353,21 @@ EXPORT_MODULE namespace hex { requires (!(std::is_reference_v || std::is_const_v)) class SettingsVariable { public: - explicit(false) SettingsVariable(T defaultValue) : m_defaultValue(std::move(defaultValue)) { - m_onChangeId = onChange(UnlocalizedCategory.value.data(), UnlocalizedName.value.data(), [this](const SettingsValue &value) { - m_value = value.get(m_defaultValue); - }); - } + explicit(false) SettingsVariable(T defaultValue) : m_defaultValue(std::move(defaultValue)) { } + + SettingsVariable(const SettingsVariable&) = delete; + SettingsVariable& operator=(const SettingsVariable&) = delete; + + SettingsVariable(SettingsVariable&&) = delete; + SettingsVariable& operator=(SettingsVariable&&) = delete; ~SettingsVariable() { - removeOnChangeHandler(m_onChangeId); + if (m_onChangeId > 0) + removeOnChangeHandler(m_onChangeId); } [[nodiscard]] T get() const { + registerChangeHandler(); if (!m_value.has_value()) { m_value = read( UnlocalizedCategory.value.data(), @@ -376,6 +380,7 @@ EXPORT_MODULE namespace hex { } void set(T value) { + registerChangeHandler(); write( UnlocalizedCategory.value.data(), UnlocalizedName.value.data(), @@ -392,10 +397,20 @@ EXPORT_MODULE namespace hex { return *this; } + private: + void registerChangeHandler() const { + if (m_onChangeId > 0) + return; + + m_onChangeId = onChange(UnlocalizedCategory.value.data(), UnlocalizedName.value.data(), [this](const SettingsValue &value) { + m_value = value.get(m_defaultValue); + }); + } + private: mutable std::optional m_value; T m_defaultValue; - u64 m_onChangeId; + mutable u64 m_onChangeId = 0; }; }