API: add a few convenience funcs needed by hyprland

This commit is contained in:
Vaxry 2024-02-09 03:28:34 +00:00
parent 26d2638f74
commit ff30ccf3cb
3 changed files with 65 additions and 0 deletions

View file

@ -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;

View file

@ -118,6 +118,8 @@ void CConfigValue::defaultFrom(SConfigDefaultValue& ref) {
throw "bad defaultFrom type";
}
}
m_bSetByUser = false;
}
void CConfigValue::setFrom(std::any value) {

View file

@ -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<const char*>(sc->values[sc->key].getValue())} != key)
continue;
return true;
}
return false;
}