From 063708df26e8b930f492200e1907eb47950f50e6 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sun, 3 Mar 2024 18:39:20 +0000 Subject: config: improve config value infrastructure --- src/config/ConfigValue.hpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/config/ConfigValue.hpp (limited to 'src/config/ConfigValue.hpp') diff --git a/src/config/ConfigValue.hpp b/src/config/ConfigValue.hpp new file mode 100644 index 00000000..6017a7cd --- /dev/null +++ b/src/config/ConfigValue.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include +#include +#include +#include "../debug/Log.hpp" +#include "../macros.hpp" + +template +class CConfigValue { + public: + CConfigValue(const std::string& val) { + const auto PVHYPRLANG = g_pConfigManager->getHyprlangConfigValuePtr(val.c_str()); + + p_ = PVHYPRLANG->getDataStaticPtr(); + +#ifdef HYPRLAND_DEBUG + // verify type + const auto ANY = PVHYPRLANG->getValue(); + const auto TYPE = std::type_index(ANY.type()); + + // exceptions + const bool STRINGEX = (typeid(T) == typeid(std::string) && TYPE == typeid(Hyprlang::STRING)); + const bool CUSTOMEX = (typeid(T) == typeid(Hyprlang::CUSTOMTYPE) && (TYPE == typeid(Hyprlang::CUSTOMTYPE*) || TYPE == typeid(void*) /* dunno why it does this? */)); + + RASSERT(typeid(T) == TYPE || STRINGEX || CUSTOMEX, "Mismatched type in CConfigValue, got {} but has {}", typeid(T).name(), TYPE.name()); +#endif + } + + T* ptr() const { + return *(T* const*)p_; + } + + T operator*() const { + return *ptr(); + } + + private: + void* const* p_ = nullptr; +}; + +template <> +inline std::string* CConfigValue::ptr() const { + RASSERT(false, "Impossible to implement ptr() of CConfigValue"); + return nullptr; +} + +template <> +inline std::string CConfigValue::operator*() const { + return std::string{*(Hyprlang::STRING*)p_}; +} + +template <> +inline Hyprlang::STRING* CConfigValue::ptr() const { + return (Hyprlang::STRING*)p_; +} + +template <> +inline Hyprlang::STRING CConfigValue::operator*() const { + return *(Hyprlang::STRING*)p_; +} + +template <> +inline Hyprlang::CUSTOMTYPE* CConfigValue::ptr() const { + return *(Hyprlang::CUSTOMTYPE* const*)p_; +} + +template <> +inline Hyprlang::CUSTOMTYPE CConfigValue::operator*() const { + RASSERT(false, "Impossible to implement operator* of CConfigValue, use ptr()"); + return *ptr(); +} \ No newline at end of file -- cgit v1.2.3