From ff30ccf3cbefe1456cb43d761f07bab7133b5595 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Fri, 9 Feb 2024 03:28:34 +0000 Subject: [PATCH] API: add a few convenience funcs needed by hyprland --- include/hyprlang.hpp | 38 ++++++++++++++++++++++++++++++++++++++ src/common.cpp | 2 ++ src/config.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/include/hyprlang.hpp b/include/hyprlang.hpp index f549787..158cea2 100644 --- a/include/hyprlang.hpp +++ b/include/hyprlang.hpp @@ -151,6 +151,15 @@ namespace Hyprlang { CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* defaultValue); ~CConfigCustomValueType(); + /*! + \since 0.3.0 + + get the data pointer for the custom value type. + */ + void* getData() { + return data; + } + private: PCONFIGCUSTOMVALUEHANDLERFUNC handler = nullptr; PCONFIGCUSTOMVALUEDESTRUCTOR dtor = nullptr; @@ -210,6 +219,14 @@ namespace Hyprlang { return {}; // unreachable } + /*! + \since 0.3.0 + + a flag to notify whether this value has been set explicitly by the user, + or not. + */ + bool m_bSetByUser = false; + private: // remember to also edit config.hpp if editing enum eDataType { @@ -249,6 +266,13 @@ namespace Hyprlang { */ void registerHandler(PCONFIGHANDLERFUNC func, const char* name, SHandlerOptions options); + /*! + \since 0.3.0 + + Unregister a handler. + */ + void unregisterHandler(const char* name); + /*! Commence the config state. Config becomes immutable, as in no new values may be added or removed. Required for parsing. @@ -260,6 +284,13 @@ namespace Hyprlang { */ void addSpecialCategory(const char* name, SSpecialCategoryOptions options); + /*! + \since 0.3.0 + + Remove a special category. Can be done dynamically. + */ + void removeSpecialCategory(const char* name); + /*! Add a config value to a special category. */ @@ -318,6 +349,13 @@ namespace Hyprlang { return val->getValue(); } + /*! + Check whether a special category with the provided key value exists + + \since 0.3.0 + */ + bool specialCategoryExistsForKey(const char* category, const char* key); + private: bool m_bCommenced = false; diff --git a/src/common.cpp b/src/common.cpp index eb4050a..2da2543 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -118,6 +118,8 @@ void CConfigValue::defaultFrom(SConfigDefaultValue& ref) { throw "bad defaultFrom type"; } } + + m_bSetByUser = false; } void CConfigValue::setFrom(std::any value) { diff --git a/src/config.cpp b/src/config.cpp index 213e963..07f6099 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -110,6 +110,11 @@ void CConfig::addSpecialCategory(const char* name, SSpecialCategoryOptions optio } } +void CConfig::removeSpecialCategory(const char* name) { + std::erase_if(impl->specialCategories, [name](const auto& other) { return other->name == name; }); + std::erase_if(impl->specialCategoryDescriptors, [name](const auto& other) { return other->name == name; }); +} + void CConfig::applyDefaultsToCat(SSpecialCategory& cat) { for (auto& [k, v] : cat.descriptor->defaultValues) { cat.values[k].defaultFrom(v); @@ -340,6 +345,8 @@ CParseResult CConfig::configSetValueSafe(const std::string& command, const std:: } } + VALUEIT->second.m_bSetByUser = true; + return result; } @@ -594,4 +601,22 @@ CConfigValue* CConfig::getSpecialConfigValuePtr(const char* category, const char void CConfig::registerHandler(PCONFIGHANDLERFUNC func, const char* name, SHandlerOptions options) { impl->handlers.push_back(SHandler{name, options, func}); +} + +void CConfig::unregisterHandler(const char* name) { + std::erase_if(impl->handlers, [name](const auto& other) { return other.name == name; }); +} + +bool CConfig::specialCategoryExistsForKey(const char* category, const char* key) { + for (auto& sc : impl->specialCategories) { + if (sc->isStatic) + continue; + + if (sc->name != category || std::string{std::any_cast(sc->values[sc->key].getValue())} != key) + continue; + + return true; + } + + return false; } \ No newline at end of file