// SPDX-FileCopyrightText: 2019 Contributors to Chatterino // // SPDX-License-Identifier: MIT #pragma once #include "util/QMagicEnum.hpp" #include #include #include #include #include #include namespace chatterino { void _registerSetting(std::weak_ptr setting); template class ChatterinoSetting : public pajlada::Settings::Setting { public: ChatterinoSetting(const std::string &path) : pajlada::Settings::Setting( path, pajlada::Settings::SettingOption::CompareBeforeSet) { _registerSetting(this->getData()); } ChatterinoSetting(const std::string &path, const Type &defaultValue) : pajlada::Settings::Setting( path, defaultValue, pajlada::Settings::SettingOption::CompareBeforeSet) { _registerSetting(this->getData()); } template ChatterinoSetting &operator=(const T2 &newValue) { this->setValue(newValue); return *this; } ChatterinoSetting &operator=(Type &&newValue) noexcept { pajlada::Settings::Setting::operator=(newValue); return *this; } using pajlada::Settings::Setting::operator==; using pajlada::Settings::Setting::operator!=; using pajlada::Settings::Setting::operator Type; }; using BoolSetting = ChatterinoSetting; using FloatSetting = ChatterinoSetting; using DoubleSetting = ChatterinoSetting; using IntSetting = ChatterinoSetting; using StringSetting = ChatterinoSetting; using QStringSetting = ChatterinoSetting; using QSizeSetting = ChatterinoSetting; /// Accepts any enum and saves the enum value as an integer /// /// e.g. for enum class {Foo = 2, Bar = 6}, Foo would be saved as 2 and Bar would be saved as 6 template class EnumSetting : public ChatterinoSetting> { using Underlying = std::underlying_type_t; public: using ChatterinoSetting::ChatterinoSetting; EnumSetting(const std::string &path, const Enum &defaultValue) : ChatterinoSetting(path, Underlying(defaultValue)) { _registerSetting(this->getData()); } EnumSetting &operator=(Enum newValue) { this->setValue(Underlying(newValue)); return *this; } operator Enum() { return Enum(this->getValue()); } Enum getEnum() { return Enum(this->getValue()); } }; /** * Setters in this class allow for bad values, it's only the enum-specific getters that are protected. * If you get a QString from this setting, it will be the raw value from the settings file. * Use the explicit Enum conversions or getEnum to get a typed check with a default **/ template class EnumStringSetting : public pajlada::Settings::Setting { public: EnumStringSetting(const std::string &path, const Enum &defaultValue_) : pajlada::Settings::Setting(path) , defaultValue(defaultValue_) { _registerSetting(this->getData()); } template EnumStringSetting &operator=(Enum newValue) { this->setValue(qmagicenum::enumNameString(newValue).toLower()); return *this; } EnumStringSetting &operator=(QString newValue) { this->setValue(newValue.toLower()); return *this; } operator Enum() { return this->getEnum(); } Enum getEnum() const { return qmagicenum::enumCast(this->getValue(), qmagicenum::CASE_INSENSITIVE) .value_or(this->defaultValue); } Enum defaultValue; using pajlada::Settings::Setting::operator==; using pajlada::Settings::Setting::operator!=; using pajlada::Settings::Setting::operator QString; }; template struct IsChatterinoSettingT : std::false_type { }; template struct IsChatterinoSettingT> : std::true_type { }; template struct IsChatterinoSettingT> : std::true_type { }; template concept IsChatterinoSetting = IsChatterinoSettingT::value; } // namespace chatterino