diff options
| author | untodesu <kirill@untode.su> | 2025-07-01 03:08:39 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-07-01 03:08:39 +0500 |
| commit | 458e0005690ea9d579588a0a12368fc2c2c9a93a (patch) | |
| tree | 588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /core/config | |
| parent | c7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff) | |
| download | voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2 voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip | |
I hyper-focued on refactoring again
- I put a cool-sounding "we are number one" remix on repeat and straight
up grinded the entire repository to a better state until 03:09 AM. I
guess I have something wrong in my brain that makes me do this shit
Diffstat (limited to 'core/config')
| -rw-r--r-- | core/config/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | core/config/boolean.cc | 46 | ||||
| -rw-r--r-- | core/config/boolean.hh | 29 | ||||
| -rw-r--r-- | core/config/ivalue.hh | 15 | ||||
| -rw-r--r-- | core/config/number.hh | 129 | ||||
| -rw-r--r-- | core/config/string.cc | 18 | ||||
| -rw-r--r-- | core/config/string.hh | 22 |
7 files changed, 266 insertions, 0 deletions
diff --git a/core/config/CMakeLists.txt b/core/config/CMakeLists.txt new file mode 100644 index 0000000..39752d4 --- /dev/null +++ b/core/config/CMakeLists.txt @@ -0,0 +1,7 @@ +target_sources(core PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/boolean.cc" + "${CMAKE_CURRENT_LIST_DIR}/boolean.hh" + "${CMAKE_CURRENT_LIST_DIR}/ivalue.hh" + "${CMAKE_CURRENT_LIST_DIR}/number.hh" + "${CMAKE_CURRENT_LIST_DIR}/string.cc" + "${CMAKE_CURRENT_LIST_DIR}/string.hh") diff --git a/core/config/boolean.cc b/core/config/boolean.cc new file mode 100644 index 0000000..a8d670c --- /dev/null +++ b/core/config/boolean.cc @@ -0,0 +1,46 @@ +#include "core/pch.hh" + +#include "core/config/boolean.hh" + +config::Boolean::Boolean(bool default_value) +{ + m_value = default_value; +} + +void config::Boolean::set(const char* value) +{ + m_value = from_string(value); +} + +const char* config::Boolean::get(void) const +{ + return to_string(m_value); +} + +bool config::Boolean::get_value(void) const +{ + return m_value; +} + +void config::Boolean::set_value(bool value) +{ + m_value = value; +} + +const char* config::Boolean::to_string(bool value) +{ + if(value) { + return "true"; + } else { + return "false"; + } +} + +bool config::Boolean::from_string(const char* value) +{ + if(std::strcmp(value, "false") && !std::strcmp(value, "true")) { + return true; + } else { + return false; + } +} diff --git a/core/config/boolean.hh b/core/config/boolean.hh new file mode 100644 index 0000000..5285676 --- /dev/null +++ b/core/config/boolean.hh @@ -0,0 +1,29 @@ +#ifndef CORE_CONFIG_BOOLEAN_HH +#define CORE_CONFIG_BOOLEAN_HH 1 +#pragma once + +#include "core/config/ivalue.hh" + +namespace config +{ +class Boolean : public IValue { +public: + explicit Boolean(bool default_value = false); + virtual ~Boolean(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; + +public: + static const char* to_string(bool value); + static bool from_string(const char* value); +}; +} // namespace config + +#endif // CORE_CONFIG_BOOLEAN_HH diff --git a/core/config/ivalue.hh b/core/config/ivalue.hh new file mode 100644 index 0000000..cf893a5 --- /dev/null +++ b/core/config/ivalue.hh @@ -0,0 +1,15 @@ +#ifndef CORE_CONFIG_IVALUE_HH +#define CORE_CONFIG_IVALUE_HH 1 +#pragma once + +namespace config +{ +class IValue { +public: + virtual ~IValue(void) = default; + virtual void set(const char* value) = 0; + virtual const char* get(void) const = 0; +}; +} // namespace config + +#endif // CORE_CONFIG_IVALUE_HH diff --git a/core/config/number.hh b/core/config/number.hh new file mode 100644 index 0000000..9907993 --- /dev/null +++ b/core/config/number.hh @@ -0,0 +1,129 @@ +#ifndef CORE_CONFIG_NUMBER_HH +#define CORE_CONFIG_NUMBER_HH 1 +#pragma once + +#include "core/config/ivalue.hh" +#include "core/math/concepts.hh" + +namespace config +{ +template<math::Arithmetic T> +class Number : public IValue { +public: + explicit Number(T default_value = T(0)); + explicit Number(T default_value, T min_value, T max_value); + virtual ~Number(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; +}; +} // namespace config + +namespace config +{ +class Int final : public Number<int> { +public: + using Number<int>::Number; +}; + +class Float final : public Number<float> { +public: + using Number<float>::Number; +}; + +class Unsigned final : public Number<unsigned int> { +public: + using Number<unsigned int>::Number; +}; + +class Unsigned64 final : public Number<std::uint64_t> { +public: + using Number<std::uint64_t>::Number; +}; + +class SizeType final : public Number<std::size_t> { +public: + using Number<std::size_t>::Number; +}; +} // namespace config + +template<math::Arithmetic T> +inline config::Number<T>::Number(T default_value) +{ + m_value = default_value; + m_min_value = std::numeric_limits<T>::lowest(); + m_max_value = std::numeric_limits<T>::max(); + m_string = std::to_string(default_value); +} + +template<math::Arithmetic T> +inline config::Number<T>::Number(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<math::Arithmetic T> +inline void config::Number<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<math::Arithmetic T> +inline const char* config::Number<T>::get(void) const +{ + return m_string.c_str(); +} + +template<math::Arithmetic T> +inline T config::Number<T>::get_value(void) const +{ + return m_value; +} + +template<math::Arithmetic T> +inline void config::Number<T>::set_value(T value) +{ + m_value = std::clamp(value, m_min_value, m_max_value); + m_string = std::to_string(m_value); +} + +template<math::Arithmetic T> +inline T config::Number<T>::get_min_value(void) const +{ + return m_min_value; +} + +template<math::Arithmetic T> +inline T config::Number<T>::get_max_value(void) const +{ + return m_max_value; +} + +template<math::Arithmetic T> +inline void config::Number<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_NUMBER_HH diff --git a/core/config/string.cc b/core/config/string.cc new file mode 100644 index 0000000..08e8c0d --- /dev/null +++ b/core/config/string.cc @@ -0,0 +1,18 @@ +#include "core/pch.hh" + +#include "core/config/string.hh" + +config::String::String(const char* default_value) +{ + m_value = default_value; +} + +void config::String::set(const char* value) +{ + m_value = value; +} + +const char* config::String::get(void) const +{ + return m_value.c_str(); +} diff --git a/core/config/string.hh b/core/config/string.hh new file mode 100644 index 0000000..09d14c9 --- /dev/null +++ b/core/config/string.hh @@ -0,0 +1,22 @@ +#ifndef CORE_CONFIG_STRING_HH +#define CORE_CONFIG_STRING_HH 1 +#pragma once + +#include "core/config/ivalue.hh" + +namespace config +{ +class String : public IValue { +public: + explicit String(const char* default_value); + virtual ~String(void) = default; + + virtual void set(const char* value) override; + virtual const char* get(void) const override; + +private: + std::string m_value; +}; +} // namespace config + +#endif // CORE_CONFIG_STRING_HH |
