summaryrefslogtreecommitdiffstats
path: root/src/core/config.hh
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-06-29 22:24:42 +0500
committeruntodesu <kirill@untode.su>2025-06-29 22:24:42 +0500
commit6cd00aacfa22fed6a54a9b812f6b069ad16feec0 (patch)
treeb77f4e665da3dd235cdb01e7e6ea78c1c02ecf2e /src/core/config.hh
parentf440914e1ae453768d09383f332bc7844e0a700e (diff)
downloadvoxelius-6cd00aacfa22fed6a54a9b812f6b069ad16feec0.tar.bz2
voxelius-6cd00aacfa22fed6a54a9b812f6b069ad16feec0.zip
Move game sources into src subdirectory
Diffstat (limited to 'src/core/config.hh')
-rw-r--r--src/core/config.hh182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/core/config.hh b/src/core/config.hh
new file mode 100644
index 0000000..a7f8500
--- /dev/null
+++ b/src/core/config.hh
@@ -0,0 +1,182 @@
+#ifndef CORE_CONFIG_HH
+#define CORE_CONFIG_HH 1
+#pragma once
+
+#include "core/concepts.hh"
+#include "core/macros.hh"
+
+class IConfigValue {
+public:
+ virtual ~IConfigValue(void) = default;
+ virtual void set(const char* value) = 0;
+ virtual const char* get(void) const = 0;
+};
+
+class ConfigBoolean final : public IConfigValue {
+public:
+ explicit ConfigBoolean(bool default_value = false);
+ virtual ~ConfigBoolean(void) = default;
+
+ virtual void set(const char* value) override;
+ virtual const char* get(void) const override;
+
+ bool get_value(void) const;
+ void set_value(bool value);
+
+private:
+ bool m_value;
+ std::string m_string;
+
+public:
+ static const char* to_string(bool value);
+ static bool from_string(const char* value);
+};
+
+template<vx::Arithmetic T>
+class ConfigNumber : public IConfigValue {
+public:
+ explicit ConfigNumber(T default_value = T(0));
+ explicit ConfigNumber(T default_value, T min_value, T max_value);
+ virtual ~ConfigNumber(void) = default;
+
+ virtual void set(const char* value) override;
+ virtual const char* get(void) const override;
+
+ T get_value(void) const;
+ void set_value(T value);
+
+ T get_min_value(void) const;
+ T get_max_value(void) const;
+ void set_limits(T min_value, T max_value);
+
+private:
+ T m_value;
+ T m_min_value;
+ T m_max_value;
+ std::string m_string;
+};
+
+class ConfigInt final : public ConfigNumber<int> {
+public:
+ using ConfigNumber<int>::ConfigNumber;
+};
+
+class ConfigFloat final : public ConfigNumber<float> {
+public:
+ using ConfigNumber<float>::ConfigNumber;
+};
+
+class ConfigUnsigned final : public ConfigNumber<unsigned int> {
+public:
+ using ConfigNumber<unsigned int>::ConfigNumber;
+};
+
+class ConfigUnsigned64 final : public ConfigNumber<std::uint64_t> {
+public:
+ using ConfigNumber<std::uint64_t>::ConfigNumber;
+};
+
+class ConfigSizeType final : public ConfigNumber<std::size_t> {
+public:
+ using ConfigNumber<std::size_t>::ConfigNumber;
+};
+
+class ConfigString final : public IConfigValue {
+public:
+ explicit ConfigString(const char* default_value);
+ virtual ~ConfigString(void) = default;
+
+ virtual void set(const char* value) override;
+ virtual const char* get(void) const override;
+
+private:
+ std::string m_value;
+};
+
+class Config final {
+public:
+ DECLARE_DEFAULT_CONSTRUCTOR(Config);
+ virtual ~Config(void) = default;
+
+ void load_cmdline(void);
+ bool load_file(const char* path);
+ bool save_file(const char* path) const;
+
+ bool set_value(const char* name, const char* value);
+ const char* get_value(const char* name) const;
+
+ void add_value(const char* name, IConfigValue& vref);
+
+ const IConfigValue* find(const char* name) const;
+
+private:
+ std::unordered_map<std::string, IConfigValue*> m_values;
+};
+
+template<vx::Arithmetic T>
+inline ConfigNumber<T>::ConfigNumber(T default_value)
+{
+ m_value = default_value;
+ m_min_value = std::numeric_limits<T>::min();
+ m_max_value = std::numeric_limits<T>::max();
+ m_string = std::to_string(default_value);
+}
+
+template<vx::Arithmetic T>
+inline ConfigNumber<T>::ConfigNumber(T default_value, T min_value, T max_value)
+{
+ m_value = default_value;
+ m_min_value = min_value;
+ m_max_value = max_value;
+ m_string = std::to_string(default_value);
+}
+
+template<vx::Arithmetic T>
+inline void ConfigNumber<T>::set(const char* value)
+{
+ std::istringstream(value) >> m_value;
+ m_value = std::clamp(m_value, m_min_value, m_max_value);
+ m_string = std::to_string(m_value);
+}
+
+template<vx::Arithmetic T>
+inline const char* ConfigNumber<T>::get(void) const
+{
+ return m_string.c_str();
+}
+
+template<vx::Arithmetic T>
+inline T ConfigNumber<T>::get_value(void) const
+{
+ return m_value;
+}
+
+template<vx::Arithmetic T>
+inline void ConfigNumber<T>::set_value(T value)
+{
+ m_value = std::clamp(value, m_min_value, m_max_value);
+ m_string = std::to_string(m_value);
+}
+
+template<vx::Arithmetic T>
+inline T ConfigNumber<T>::get_min_value(void) const
+{
+ return m_min_value;
+}
+
+template<vx::Arithmetic T>
+inline T ConfigNumber<T>::get_max_value(void) const
+{
+ return m_max_value;
+}
+
+template<vx::Arithmetic T>
+inline void ConfigNumber<T>::set_limits(T min_value, T max_value)
+{
+ m_min_value = min_value;
+ m_max_value = max_value;
+ m_value = std::clamp(m_value, m_min_value, m_max_value);
+ m_string = std::to_string(m_value);
+}
+
+#endif /* CORE_CONFIG_HH */