configvalue: add getDataStaticPtr()

This commit is contained in:
Vaxry 2023-12-31 14:15:03 +01:00
parent 1210de188c
commit 5150f64f15
3 changed files with 21 additions and 2 deletions

View file

@ -71,7 +71,7 @@ namespace Hyprlang {
bool throwAllErrors = false; 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. Don't throw on a missing config file. Carry on as if nothing happened.
*/ */
bool allowMissingConfig = false; bool allowMissingConfig = false;
@ -148,10 +148,18 @@ namespace Hyprlang {
~CConfigValue(); ~CConfigValue();
/*! /*!
Return a pointer to the data. Prefer getValue(). Return a pointer to the data. Prefer getDataStaticPtr()
*/ */
void* dataPtr() const; 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. Get the contained value as an std::any.
For strings, this is a const char*. For strings, this is a const char*.

View file

@ -62,6 +62,10 @@ void* CConfigValue::dataPtr() const {
return m_pData; return m_pData;
} }
void* const* CConfigValue::getDataStaticPtr() const {
return &m_pData;
}
CConfigCustomValueType::CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* def) { CConfigCustomValueType::CConfigCustomValueType(PCONFIGCUSTOMVALUEHANDLERFUNC handler_, PCONFIGCUSTOMVALUEDESTRUCTOR dtor_, const char* def) {
handler = handler_; handler = handler_;
dtor = dtor_; dtor = dtor_;

View file

@ -140,6 +140,13 @@ int main(int argc, char** argv, char** envp) {
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor2")), 0xFF000000L); EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor2")), 0xFF000000L);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testCategory:testColor3")), 0x22ffeeffL); EXPECT(std::any_cast<int64_t>(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<int64_t*>(*PTESTINT), 123);
config.parse();
EXPECT(*reinterpret_cast<int64_t*>(*PTESTINT), 123);
// test handlers // test handlers
std::cout << " → Testing handlers\n"; std::cout << " → Testing handlers\n";
EXPECT(barrelRoll, true); EXPECT(barrelRoll, true);