diff --git a/include/hyprlang.hpp b/include/hyprlang.hpp index e9be3f0..4cf64a0 100644 --- a/include/hyprlang.hpp +++ b/include/hyprlang.hpp @@ -71,7 +71,7 @@ namespace Hyprlang { bool throwAllErrors = false; /*! - @since 0.2.0 + \since 0.2.0 Don't throw on a missing config file. Carry on as if nothing happened. */ bool allowMissingConfig = false; @@ -148,10 +148,18 @@ namespace Hyprlang { ~CConfigValue(); /*! - Return a pointer to the data. Prefer getValue(). + Return a pointer to the data. Prefer getDataStaticPtr() */ void* dataPtr() const; + /*! + \since 0.2.0 + Return a static pointer to the m_pData. + As long as this configValue is alive, this pointer is valid. + CConfigValues are alive as long as the owning CConfig is alive. + */ + void* const* getDataStaticPtr() const; + /*! Get the contained value as an std::any. For strings, this is a const char*. diff --git a/src/common.cpp b/src/common.cpp index cde6db1..9846ea5 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -62,6 +62,10 @@ void* CConfigValue::dataPtr() const { return m_pData; } +void* const* CConfigValue::getDataStaticPtr() const { + return &m_pData; +} + CConfigCustomValueType::CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* def) { handler = handler_; dtor = dtor_; diff --git a/tests/main.cpp b/tests/main.cpp index 18048dd..2709abd 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -140,6 +140,13 @@ int main(int argc, char** argv, char** envp) { EXPECT(std::any_cast(config.getConfigValue("testCategory:testColor2")), 0xFF000000L); EXPECT(std::any_cast(config.getConfigValue("testCategory:testColor3")), 0x22ffeeffL); + // test static values + std::cout << " → Testing static values\n"; + static auto* const PTESTINT = config.getConfigValuePtr("testInt")->getDataStaticPtr(); + EXPECT(*reinterpret_cast(*PTESTINT), 123); + config.parse(); + EXPECT(*reinterpret_cast(*PTESTINT), 123); + // test handlers std::cout << " → Testing handlers\n"; EXPECT(barrelRoll, true);