diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | core/config/boolean.cc | 15 | ||||
| -rw-r--r-- | core/config/boolean.hh | 8 | ||||
| -rw-r--r-- | core/config/ivalue.hh | 4 | ||||
| -rw-r--r-- | core/config/number.hh | 20 | ||||
| -rw-r--r-- | core/config/string.cc | 8 | ||||
| -rw-r--r-- | core/config/string.hh | 19 | ||||
| -rw-r--r-- | core/io/cmdline.cc | 31 | ||||
| -rw-r--r-- | core/io/cmdline.hh | 8 | ||||
| -rw-r--r-- | core/io/config_map.cc | 33 | ||||
| -rw-r--r-- | core/io/config_map.hh | 12 | ||||
| -rw-r--r-- | core/resource/binfile.cc | 8 | ||||
| -rw-r--r-- | core/resource/image.cc | 8 | ||||
| -rw-r--r-- | core/resource/resource.hh | 2 | ||||
| -rw-r--r-- | core/version.cc.in | 10 | ||||
| -rw-r--r-- | core/version.hh | 16 |
16 files changed, 120 insertions, 86 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 0710274..edcab85 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,3 +1,7 @@ +execute_process(COMMAND git rev-parse --short=8 HEAD + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + OUTPUT_VARIABLE GIT_COMMIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE) configure_file("${CMAKE_CURRENT_LIST_DIR}/version.cc.in" "${CMAKE_CURRENT_LIST_DIR}/version.cc") add_library(core STATIC diff --git a/core/config/boolean.cc b/core/config/boolean.cc index 6363271..45d5a38 100644 --- a/core/config/boolean.cc +++ b/core/config/boolean.cc @@ -7,12 +7,12 @@ config::Boolean::Boolean(bool default_value) m_value = default_value; } -void config::Boolean::set(const char* value) +void config::Boolean::set(std::string_view value) { m_value = from_string(value); } -const char* config::Boolean::get(void) const +std::string_view config::Boolean::get(void) const { return to_string(m_value); } @@ -27,7 +27,7 @@ void config::Boolean::set_value(bool value) m_value = value; } -const char* config::Boolean::to_string(bool value) +std::string_view config::Boolean::to_string(bool value) { if(value) { return "true"; @@ -37,12 +37,7 @@ const char* config::Boolean::to_string(bool value) } } -bool config::Boolean::from_string(const char* value) +bool config::Boolean::from_string(std::string_view value) { - if(std::strcmp(value, "false") && !std::strcmp(value, "true")) { - return true; - } - else { - return false; - } + return value == "true" && value != "false"; } diff --git a/core/config/boolean.hh b/core/config/boolean.hh index 5285676..2e9855f 100644 --- a/core/config/boolean.hh +++ b/core/config/boolean.hh @@ -11,8 +11,8 @@ 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; + virtual void set(std::string_view value) override; + virtual std::string_view get(void) const override; bool get_value(void) const; void set_value(bool value); @@ -21,8 +21,8 @@ private: bool m_value; public: - static const char* to_string(bool value); - static bool from_string(const char* value); + static std::string_view to_string(bool value); + static bool from_string(std::string_view value); }; } // namespace config diff --git a/core/config/ivalue.hh b/core/config/ivalue.hh index cf893a5..97d87dc 100644 --- a/core/config/ivalue.hh +++ b/core/config/ivalue.hh @@ -7,8 +7,8 @@ namespace config class IValue { public: virtual ~IValue(void) = default; - virtual void set(const char* value) = 0; - virtual const char* get(void) const = 0; + virtual void set(std::string_view value) = 0; + virtual std::string_view get(void) const = 0; }; } // namespace config diff --git a/core/config/number.hh b/core/config/number.hh index 9907993..ad79770 100644 --- a/core/config/number.hh +++ b/core/config/number.hh @@ -14,8 +14,8 @@ public: 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; + virtual void set(std::string_view value) override; + virtual std::string_view get(void) const override; T get_value(void) const; void set_value(T value); @@ -79,17 +79,21 @@ inline config::Number<T>::Number(T default_value, T min_value, T max_value) } template<math::Arithmetic T> -inline void config::Number<T>::set(const char* value) +inline void config::Number<T>::set(std::string_view 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); + T parsed_value; + auto result = std::from_chars(value.data(), value.data() + value.size(), parsed_value); + + if(result.ec == std::errc()) { + m_value = std::clamp(parsed_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 +inline std::string_view config::Number<T>::get(void) const { - return m_string.c_str(); + return m_string; } template<math::Arithmetic T> diff --git a/core/config/string.cc b/core/config/string.cc index 08e8c0d..198b448 100644 --- a/core/config/string.cc +++ b/core/config/string.cc @@ -2,17 +2,17 @@ #include "core/config/string.hh" -config::String::String(const char* default_value) +config::String::String(std::string_view default_value) { m_value = default_value; } -void config::String::set(const char* value) +void config::String::set(std::string_view value) { m_value = value; } -const char* config::String::get(void) const +std::string_view config::String::get(void) const { - return m_value.c_str(); + return m_value; } diff --git a/core/config/string.hh b/core/config/string.hh index 09d14c9..772c330 100644 --- a/core/config/string.hh +++ b/core/config/string.hh @@ -8,15 +8,28 @@ namespace config { class String : public IValue { public: - explicit String(const char* default_value); + explicit String(std::string_view default_value); virtual ~String(void) = default; - virtual void set(const char* value) override; - virtual const char* get(void) const override; + virtual void set(std::string_view value) override; + virtual std::string_view get(void) const override; + + constexpr const std::string& get_value(void) const noexcept; + constexpr const char* c_str(void) const noexcept; private: std::string m_value; }; } // namespace config +constexpr const std::string& config::String::get_value(void) const noexcept +{ + return m_value; +} + +constexpr const char* config::String::c_str(void) const noexcept +{ + return m_value.c_str(); +} + #endif // CORE_CONFIG_STRING_HH diff --git a/core/io/cmdline.cc b/core/io/cmdline.cc index 963f67c..7a00f37 100644 --- a/core/io/cmdline.cc +++ b/core/io/cmdline.cc @@ -57,19 +57,30 @@ void io::cmdline::create(int argc, char** argv) } } -void io::cmdline::insert(const char* option, const char* argument) +void io::cmdline::insert(std::string_view option) { - if(argument == nullptr) { - options.insert_or_assign(option, std::string()); - } - else { - options.insert_or_assign(option, argument); + options.insert_or_assign(std::string(option), std::string()); +} + +void io::cmdline::insert(std::string_view option, std::string_view argument) +{ + options.insert_or_assign(std::string(option), std::string(argument)); +} + +std::string_view io::cmdline::get(std::string_view option, std::string_view fallback) +{ + auto it = options.find(std::string(option)); + + if(it == options.cend()) { + return fallback; } + + return it->second; } -const char* io::cmdline::get(const char* option, const char* fallback) +const char* io::cmdline::get_cstr(std::string_view option, const char* fallback) { - auto it = options.find(option); + auto it = options.find(std::string(option)); if(it == options.cend()) { return fallback; @@ -78,7 +89,7 @@ const char* io::cmdline::get(const char* option, const char* fallback) return it->second.c_str(); } -bool io::cmdline::contains(const char* option) +bool io::cmdline::contains(std::string_view option) { - return options.count(option); + return options.count(std::string(option)); } diff --git a/core/io/cmdline.hh b/core/io/cmdline.hh index d68d9ef..8b2b4ee 100644 --- a/core/io/cmdline.hh +++ b/core/io/cmdline.hh @@ -5,9 +5,11 @@ namespace io::cmdline { void create(int argc, char** argv); -void insert(const char* option, const char* argument = nullptr); -const char* get(const char* option, const char* fallback = nullptr); -bool contains(const char* option); +void insert(std::string_view option); +void insert(std::string_view option, std::string_view argument); +std::string_view get(std::string_view option, std::string_view fallback = ""); +const char* get_cstr(std::string_view option, const char* fallback = nullptr); +bool contains(std::string_view option); } // namespace io::cmdline #endif // CORE_IO_CMDLINE_HH diff --git a/core/io/config_map.cc b/core/io/config_map.cc index 2afd2a8..9fd8ff7 100644 --- a/core/io/config_map.cc +++ b/core/io/config_map.cc @@ -10,15 +10,15 @@ void io::ConfigMap::load_cmdline(void) { for(auto it : m_values) { - if(auto value = io::cmdline::get(it.first.c_str())) { + if(auto value = io::cmdline::get_cstr(it.first.c_str())) { it.second->set(value); } } } -bool io::ConfigMap::load_file(const char* path) +bool io::ConfigMap::load_file(std::string_view path) { - if(auto file = PHYSFS_openRead(path)) { + if(auto file = PHYSFS_openRead(std::string(path).c_str())) { auto source = std::string(PHYSFS_fileLength(file), char(0x00)); PHYSFS_readBytes(file, source.data(), source.size()); PHYSFS_close(file); @@ -68,13 +68,13 @@ bool io::ConfigMap::load_file(const char* path) return false; } -bool io::ConfigMap::save_file(const char* path) const +bool io::ConfigMap::save_file(std::string_view path) const { std::ostringstream stream; auto curtime = std::time(nullptr); - stream << "# Voxelius " << project_version_string << " configuration file" << std::endl; + stream << "# Voxelius " << version::semver << " configuration file" << std::endl; stream << "# Generated at: " << std::put_time(std::gmtime(&curtime), "%Y-%m-%d %H:%M:%S %z") << std::endl << std::endl; for(const auto& it : m_values) { @@ -83,7 +83,7 @@ bool io::ConfigMap::save_file(const char* path) const stream << std::endl; } - if(auto file = PHYSFS_openWrite(path)) { + if(auto file = PHYSFS_openWrite(std::string(path).c_str())) { auto source = stream.str(); PHYSFS_writeBytes(file, source.data(), source.size()); PHYSFS_close(file); @@ -93,9 +93,9 @@ bool io::ConfigMap::save_file(const char* path) const return false; } -bool io::ConfigMap::set_value(const char* name, const char* value) +bool io::ConfigMap::set_value(std::string_view name, std::string_view value) { - auto kv_pair = m_values.find(name); + auto kv_pair = m_values.find(std::string(name)); if(kv_pair != m_values.cend()) { kv_pair->second->set(value); @@ -105,25 +105,24 @@ bool io::ConfigMap::set_value(const char* name, const char* value) return false; } -const char* io::ConfigMap::get_value(const char* name) const +std::string_view io::ConfigMap::get_value(std::string_view name) const { - auto kv_pair = m_values.find(name); + auto kv_pair = m_values.find(std::string(name)); if(kv_pair != m_values.cend()) { return kv_pair->second->get(); } - else { - return nullptr; - } + + return std::string_view(); } -void io::ConfigMap::add_value(const char* name, config::IValue& vref) +void io::ConfigMap::add_value(std::string_view name, config::IValue& vref) { - m_values.insert_or_assign(name, &vref); + m_values.insert_or_assign(std::string(name), &vref); } -const config::IValue* io::ConfigMap::find(const char* name) const +const config::IValue* io::ConfigMap::find(std::string_view name) const { - auto kv_pair = m_values.find(name); + auto kv_pair = m_values.find(std::string(name)); if(kv_pair != m_values.cend()) { return kv_pair->second; diff --git a/core/io/config_map.hh b/core/io/config_map.hh index 8ff92e1..7ab7543 100644 --- a/core/io/config_map.hh +++ b/core/io/config_map.hh @@ -15,15 +15,15 @@ public: virtual ~ConfigMap(void) = default; void load_cmdline(void); - bool load_file(const char* path); - bool save_file(const char* path) const; + bool load_file(std::string_view path); + bool save_file(std::string_view path) const; - bool set_value(const char* name, const char* value); - const char* get_value(const char* name) const; + bool set_value(std::string_view name, std::string_view value); + std::string_view get_value(std::string_view name) const; - void add_value(const char* name, config::IValue& vref); + void add_value(std::string_view name, config::IValue& vref); - const config::IValue* find(const char* name) const; + const config::IValue* find(std::string_view name) const; private: std::unordered_map<std::string, config::IValue*> m_values; diff --git a/core/resource/binfile.cc b/core/resource/binfile.cc index b8e3db8..726ef9b 100644 --- a/core/resource/binfile.cc +++ b/core/resource/binfile.cc @@ -7,16 +7,16 @@ static emhash8::HashMap<std::string, resource_ptr<BinFile>> resource_map; template<> -resource_ptr<BinFile> resource::load<BinFile>(const char* name, unsigned int flags) +resource_ptr<BinFile> resource::load<BinFile>(std::string_view name, unsigned int flags) { - auto it = resource_map.find(name); + auto it = resource_map.find(std::string(name)); if(it != resource_map.cend()) { // Return an existing resource return it->second; } - auto file = PHYSFS_openRead(name); + auto file = PHYSFS_openRead(std::string(name).c_str()); if(file == nullptr) { spdlog::warn("resource: {}: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -30,7 +30,7 @@ resource_ptr<BinFile> resource::load<BinFile>(const char* name, unsigned int fla PHYSFS_readBytes(file, new_resource->buffer, new_resource->size); PHYSFS_close(file); - return resource_map.insert_or_assign(name, new_resource).first->second; + return resource_map.insert_or_assign(std::string(name), new_resource).first->second; } template<> diff --git a/core/resource/image.cc b/core/resource/image.cc index 9ff0206..8b570e4 100644 --- a/core/resource/image.cc +++ b/core/resource/image.cc @@ -23,16 +23,16 @@ static int stbi_physfs_eof(void* context) } template<> -resource_ptr<Image> resource::load<Image>(const char* name, unsigned int flags) +resource_ptr<Image> resource::load<Image>(std::string_view name, unsigned int flags) { - auto it = resource_map.find(name); + auto it = resource_map.find(std::string(name)); if(it != resource_map.cend()) { // Return an existing resource return it->second; } - auto file = PHYSFS_openRead(name); + auto file = PHYSFS_openRead(std::string(name).c_str()); if(file == nullptr) { spdlog::warn("resource: {}: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -74,7 +74,7 @@ resource_ptr<Image> resource::load<Image>(const char* name, unsigned int flags) return nullptr; } - return resource_map.insert_or_assign(name, new_resource).first->second; + return resource_map.insert_or_assign(std::string(name), new_resource).first->second; } template<> diff --git a/core/resource/resource.hh b/core/resource/resource.hh index 4946ffa..5b6b2ac 100644 --- a/core/resource/resource.hh +++ b/core/resource/resource.hh @@ -8,7 +8,7 @@ using resource_ptr = std::shared_ptr<const T>; namespace resource { template<typename T> -resource_ptr<T> load(const char* name, unsigned int flags = 0U); +resource_ptr<T> load(std::string_view name, unsigned int flags = 0U); template<typename T> void hard_cleanup(void); template<typename T> diff --git a/core/version.cc.in b/core/version.cc.in index 0c255c9..a4689d7 100644 --- a/core/version.cc.in +++ b/core/version.cc.in @@ -3,10 +3,10 @@ #include "core/version.hh" // clang-format off -const unsigned long project_version_major = ${PROJECT_VERSION_MAJOR}; -const unsigned long project_version_minor = ${PROJECT_VERSION_MINOR}; -const unsigned long project_version_patch = ${PROJECT_VERSION_PATCH}; -const unsigned long project_version_tweak = ${PROJECT_VERSION_TWEAK}; +const unsigned long version::major = ${PROJECT_VERSION_MAJOR}; +const unsigned long version::minor = ${PROJECT_VERSION_MINOR}; +const unsigned long version::patch = ${PROJECT_VERSION_PATCH}; // clang-format on -const char* project_version_string = "${PROJECT_VERSION}"; +const std::string_view version::commit = "${GIT_COMMIT_HASH}"; +const std::string_view version::semver = "${PROJECT_VERSION}"; diff --git a/core/version.hh b/core/version.hh index 627df17..b84a2fc 100644 --- a/core/version.hh +++ b/core/version.hh @@ -2,11 +2,17 @@ #define CORE_VERSION_HH 1 #pragma once -extern const unsigned long project_version_major; -extern const unsigned long project_version_minor; -extern const unsigned long project_version_patch; -extern const unsigned long project_version_tweak; +namespace version +{ +extern const unsigned long major; +extern const unsigned long minor; +extern const unsigned long patch; +} // namespace version -extern const char* project_version_string; +namespace version +{ +extern const std::string_view commit; +extern const std::string_view semver; +} // namespace version #endif // CORE_VERSION_HH |
