use string_view instead of string& in Mod

This commit is contained in:
dankmeme01 2024-01-09 14:47:11 +01:00
parent 32cd4c40f1
commit dcaa408bc1
4 changed files with 46 additions and 43 deletions

View file

@ -119,9 +119,9 @@ namespace geode {
bool hasSettings() const; bool hasSettings() const;
std::vector<std::string> getSettingKeys() const; std::vector<std::string> getSettingKeys() const;
bool hasSetting(std::string const& key) const; bool hasSetting(std::string_view const key) const;
std::optional<Setting> getSettingDefinition(std::string const& key) const; std::optional<Setting> getSettingDefinition(std::string_view const key) const;
SettingValue* getSetting(std::string const& key) const; SettingValue* getSetting(std::string_view const key) const;
/** /**
* Register a custom setting's value class. See Mod::addCustomSetting * Register a custom setting's value class. See Mod::addCustomSetting
@ -133,7 +133,7 @@ namespace geode {
* @param value The SettingValue class that shall handle this setting * @param value The SettingValue class that shall handle this setting
* @see addCustomSetting * @see addCustomSetting
*/ */
void registerCustomSetting(std::string const& key, std::unique_ptr<SettingValue> value); void registerCustomSetting(std::string_view const key, std::unique_ptr<SettingValue> value);
/** /**
* Register a custom setting's value class. The new SettingValue class * Register a custom setting's value class. The new SettingValue class
* will be created in-place using `std::make_unique`. See * will be created in-place using `std::make_unique`. See
@ -147,14 +147,14 @@ namespace geode {
* } * }
*/ */
template <class T, class V> template <class T, class V>
void addCustomSetting(std::string const& key, V const& value) { void addCustomSetting(std::string_view const key, V const& value) {
this->registerCustomSetting(key, std::make_unique<T>(key, this->getID(), value)); this->registerCustomSetting(key, std::make_unique<T>(key, this->getID(), value));
} }
matjson::Value& getSaveContainer(); matjson::Value& getSaveContainer();
template <class T> template <class T>
T getSettingValue(std::string const& key) const { T getSettingValue(std::string_view const key) const {
if (auto sett = this->getSetting(key)) { if (auto sett = this->getSetting(key)) {
return SettingValueSetter<T>::get(sett); return SettingValueSetter<T>::get(sett);
} }
@ -162,7 +162,7 @@ namespace geode {
} }
template <class T> template <class T>
T setSettingValue(std::string const& key, T const& value) { T setSettingValue(std::string_view const key, T const& value) {
if (auto sett = this->getSetting(key)) { if (auto sett = this->getSetting(key)) {
auto old = this->getSettingValue<T>(key); auto old = this->getSettingValue<T>(key);
SettingValueSetter<T>::set(sett, value); SettingValueSetter<T>::set(sett, value);
@ -171,10 +171,10 @@ namespace geode {
return T(); return T();
} }
bool hasSavedValue(std::string const& key); bool hasSavedValue(std::string_view const key);
template <class T> template <class T>
T getSavedValue(std::string const& key) { T getSavedValue(std::string_view const key) {
auto& saved = this->getSaveContainer(); auto& saved = this->getSaveContainer();
if (saved.contains(key)) { if (saved.contains(key)) {
try { try {
@ -188,7 +188,7 @@ namespace geode {
} }
template <class T> template <class T>
T getSavedValue(std::string const& key, T const& defaultValue) { T getSavedValue(std::string_view const key, T const& defaultValue) {
auto& saved = this->getSaveContainer(); auto& saved = this->getSaveContainer();
if (saved.contains(key)) { if (saved.contains(key)) {
try { try {
@ -210,7 +210,7 @@ namespace geode {
* @returns The old value * @returns The old value
*/ */
template <class T> template <class T>
T setSavedValue(std::string const& key, T const& value) { T setSavedValue(std::string_view const key, T const& value) {
auto& saved = this->getSaveContainer(); auto& saved = this->getSaveContainer();
auto old = this->getSavedValue<T>(key); auto old = this->getSavedValue<T>(key);
saved[key] = value; saved[key] = value;
@ -335,7 +335,7 @@ namespace geode {
* Check whether or not this Mod * Check whether or not this Mod
* depends on another mod * depends on another mod
*/ */
bool depends(std::string const& id) const; bool depends(std::string_view const id) const;
/** /**
* Check whether all the required * Check whether all the required

View file

@ -105,19 +105,19 @@ std::vector<std::string> Mod::getSettingKeys() const {
return m_impl->getSettingKeys(); return m_impl->getSettingKeys();
} }
bool Mod::hasSetting(std::string const& key) const { bool Mod::hasSetting(std::string_view const key) const {
return m_impl->hasSetting(key); return m_impl->hasSetting(key);
} }
std::optional<Setting> Mod::getSettingDefinition(std::string const& key) const { std::optional<Setting> Mod::getSettingDefinition(std::string_view const key) const {
return m_impl->getSettingDefinition(key); return m_impl->getSettingDefinition(key);
} }
SettingValue* Mod::getSetting(std::string const& key) const { SettingValue* Mod::getSetting(std::string_view const key) const {
return m_impl->getSetting(key); return m_impl->getSetting(key);
} }
void Mod::registerCustomSetting(std::string const& key, std::unique_ptr<SettingValue> value) { void Mod::registerCustomSetting(std::string_view const key, std::unique_ptr<SettingValue> value) {
return m_impl->registerCustomSetting(key, std::move(value)); return m_impl->registerCustomSetting(key, std::move(value));
} }
@ -172,7 +172,7 @@ ModRequestedAction Mod::getRequestedAction() const {
return m_impl->getRequestedAction(); return m_impl->getRequestedAction();
} }
bool Mod::depends(std::string const& id) const { bool Mod::depends(std::string_view const id) const {
return m_impl->depends(id); return m_impl->depends(id);
} }
@ -200,7 +200,7 @@ void Mod::setLoggingEnabled(bool enabled) {
m_impl->setLoggingEnabled(enabled); m_impl->setLoggingEnabled(enabled);
} }
bool Mod::hasSavedValue(std::string const& key) { bool Mod::hasSavedValue(std::string_view const key) {
return this->getSaveContainer().contains(key); return this->getSaveContainer().contains(key);
} }

View file

@ -258,13 +258,14 @@ void Mod::Impl::setupSettings() {
} }
} }
void Mod::Impl::registerCustomSetting(std::string const& key, std::unique_ptr<SettingValue> value) { void Mod::Impl::registerCustomSetting(std::string_view const key, std::unique_ptr<SettingValue> value) {
if (!m_settings.count(key)) { auto keystr = std::string(key);
if (!m_settings.count(keystr)) {
// load data // load data
if (m_savedSettingsData.contains(key)) { if (m_savedSettingsData.contains(key)) {
value->load(m_savedSettingsData[key]); value->load(m_savedSettingsData[key]);
} }
m_settings.emplace(key, std::move(value)); m_settings.emplace(keystr, std::move(value));
} }
} }
@ -280,7 +281,7 @@ std::vector<std::string> Mod::Impl::getSettingKeys() const {
return keys; return keys;
} }
std::optional<Setting> Mod::Impl::getSettingDefinition(std::string const& key) const { std::optional<Setting> Mod::Impl::getSettingDefinition(std::string_view const key) const {
for (auto& setting : m_metadata.getSettings()) { for (auto& setting : m_metadata.getSettings()) {
if (setting.first == key) { if (setting.first == key) {
return setting.second; return setting.second;
@ -289,14 +290,16 @@ std::optional<Setting> Mod::Impl::getSettingDefinition(std::string const& key) c
return std::nullopt; return std::nullopt;
} }
SettingValue* Mod::Impl::getSetting(std::string const& key) const { SettingValue* Mod::Impl::getSetting(std::string_view const key) const {
if (m_settings.count(key)) { auto keystr = std::string(key);
return m_settings.at(key).get();
if (m_settings.count(keystr)) {
return m_settings.at(keystr).get();
} }
return nullptr; return nullptr;
} }
bool Mod::Impl::hasSetting(std::string const& key) const { bool Mod::Impl::hasSetting(std::string_view const key) const {
for (auto& setting : m_metadata.getSettings()) { for (auto& setting : m_metadata.getSettings()) {
if (setting.first == key) { if (setting.first == key) {
return true; return true;
@ -449,7 +452,7 @@ bool Mod::Impl::hasUnresolvedIncompatibilities() const {
return false; return false;
} }
bool Mod::Impl::depends(std::string const& id) const { bool Mod::Impl::depends(std::string_view const id) const {
return utils::ranges::contains(m_metadata.getDependencies(), [id](ModMetadata::Dependency const& t) { return utils::ranges::contains(m_metadata.getDependencies(), [id](ModMetadata::Dependency const& t) {
return t.id == id; return t.id == id;
}); });

View file

@ -109,10 +109,10 @@ namespace geode {
bool hasSettings() const; bool hasSettings() const;
std::vector<std::string> getSettingKeys() const; std::vector<std::string> getSettingKeys() const;
bool hasSetting(std::string const& key) const; bool hasSetting(std::string_view const key) const;
std::optional<Setting> getSettingDefinition(std::string const& key) const; std::optional<Setting> getSettingDefinition(std::string_view const key) const;
SettingValue* getSetting(std::string const& key) const; SettingValue* getSetting(std::string_view const key) const;
void registerCustomSetting(std::string const& key, std::unique_ptr<SettingValue> value); void registerCustomSetting(std::string_view const key, std::unique_ptr<SettingValue> value);
std::vector<Hook*> getHooks() const; std::vector<Hook*> getHooks() const;
Result<Hook*> addHook(Hook* hook); Result<Hook*> addHook(Hook* hook);
@ -129,7 +129,7 @@ namespace geode {
// 1.3.0 additions // 1.3.0 additions
ModRequestedAction getRequestedAction() const; ModRequestedAction getRequestedAction() const;
bool depends(std::string const& id) const; bool depends(std::string_view const id) const;
Result<> updateDependencies(); Result<> updateDependencies();
bool hasUnresolvedDependencies() const; bool hasUnresolvedDependencies() const;
bool hasUnresolvedIncompatibilities() const; bool hasUnresolvedIncompatibilities() const;