summaryrefslogtreecommitdiff
path: root/src/config/ConfigValue.hpp
diff options
context:
space:
mode:
authorVaxry <vaxry@vaxry.net>2024-03-03 18:39:20 +0000
committerVaxry <vaxry@vaxry.net>2024-03-03 18:41:38 +0000
commit063708df26e8b930f492200e1907eb47950f50e6 (patch)
tree31648b7148213ecb9c6d469148ac1c5061835d5a /src/config/ConfigValue.hpp
parent8ccbd272cc5f3a98280d4e050f456d25aee47ee5 (diff)
config: improve config value infrastructure
Diffstat (limited to 'src/config/ConfigValue.hpp')
-rw-r--r--src/config/ConfigValue.hpp72
1 files changed, 72 insertions, 0 deletions
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 <string>
+#include <typeindex>
+#include <hyprlang.hpp>
+#include "../debug/Log.hpp"
+#include "../macros.hpp"
+
+template <typename T>
+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<T>, 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<std::string>::ptr() const {
+ RASSERT(false, "Impossible to implement ptr() of CConfigValue<std::string>");
+ return nullptr;
+}
+
+template <>
+inline std::string CConfigValue<std::string>::operator*() const {
+ return std::string{*(Hyprlang::STRING*)p_};
+}
+
+template <>
+inline Hyprlang::STRING* CConfigValue<Hyprlang::STRING>::ptr() const {
+ return (Hyprlang::STRING*)p_;
+}
+
+template <>
+inline Hyprlang::STRING CConfigValue<Hyprlang::STRING>::operator*() const {
+ return *(Hyprlang::STRING*)p_;
+}
+
+template <>
+inline Hyprlang::CUSTOMTYPE* CConfigValue<Hyprlang::CUSTOMTYPE>::ptr() const {
+ return *(Hyprlang::CUSTOMTYPE* const*)p_;
+}
+
+template <>
+inline Hyprlang::CUSTOMTYPE CConfigValue<Hyprlang::CUSTOMTYPE>::operator*() const {
+ RASSERT(false, "Impossible to implement operator* of CConfigValue<Hyprlang::CUSTOMTYPE>, use ptr()");
+ return *ptr();
+} \ No newline at end of file