#pragma once #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) { _registerSetting(this->getData()); } ChatterinoSetting(const std::string &path, const Type &defaultValue) : pajlada::Settings::Setting(path, defaultValue) { _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; template class EnumSetting : public ChatterinoSetting::type> { using Underlying = typename std::underlying_type::type; public: using ChatterinoSetting::ChatterinoSetting; EnumSetting(const std::string &path, const Enum &defaultValue) : ChatterinoSetting(path, Underlying(defaultValue)) { _registerSetting(this->getData()); } template 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) { std::string enumName(magic_enum::enum_name(newValue)); auto qEnumName = QString::fromStdString(enumName); this->setValue(qEnumName.toLower()); return *this; } EnumStringSetting &operator=(QString newValue) { this->setValue(newValue.toLower()); return *this; } operator Enum() { return this->getEnum(); } Enum getEnum() { return magic_enum::enum_cast(this->getValue().toStdString(), magic_enum::case_insensitive) .value_or(this->defaultValue); } Enum defaultValue; using pajlada::Settings::Setting::operator==; using pajlada::Settings::Setting::operator!=; using pajlada::Settings::Setting::operator QString; }; } // namespace chatterino