feat(toml): add shared TOML utilities and handle ip.toml

This commit is contained in:
2026-06-27 15:06:47 +08:00
parent d93748e7fb
commit 3d4490a46b
5 changed files with 63 additions and 26 deletions

View File

@@ -70,6 +70,7 @@ private:
Argument m_argument;
void init(int argc, char** argv);
void handle_argument(int argc, char** argv);
void handle_toml();
auto init_camera();
auto init_texture();
auto init_world();

View File

@@ -1,17 +1,9 @@
#pragma once
#include "Cubed/tools/cubed_assert.hpp"
#include <toml++/toml.hpp>
#include "Cubed/tools/toml.utils.hpp"
namespace Cubed {
template <typename T>
concept TomlValueType =
std::same_as<T, int> || std::same_as<T, bool> || std::same_as<T, double> ||
std::same_as<T, const char*> || std::same_as<T, toml::date> ||
std::same_as<T, toml::time> || std::same_as<T, toml::date_time> ||
std::same_as<T, std::string>;
class Config {
public:
Config();
@@ -24,7 +16,7 @@ public:
void load_or_create_config();
void save_to_file();
template <TomlValueType T> T get(std::string_view key) const {
template <TOML::TomlValueType T> T get(std::string_view key) const {
size_t cur = 0;
auto pos = key.find('.');
const toml::table* table = &m_tbl;
@@ -61,7 +53,7 @@ public:
}
}
template <typename T> void set(std::string_view key, T&& val) {
if constexpr (!TomlValueType<std::decay_t<T>>) {
if constexpr (!TOML::TomlValueType<std::decay_t<T>>) {
static_assert(false, "Type Not Support");
}
size_t cur = 0;

View File

@@ -0,0 +1,41 @@
#pragma once
#include "Cubed/tools/log.hpp"
#include <toml++/toml.hpp>
namespace Cubed {
namespace TOML {
template <typename T>
concept TomlValueType =
std::same_as<std::decay_t<T>, int> || std::same_as<std::decay_t<T>, bool> ||
std::same_as<std::decay_t<T>, double> ||
std::same_as<std::decay_t<T>, char> ||
std::same_as<std::decay_t<T>, toml::date> ||
std::same_as<std::decay_t<T>, toml::time> ||
std::same_as<std::decay_t<T>, toml::date_time> ||
std::same_as<std::decay_t<T>, std::string>;
template <TomlValueType T>
std::optional<T> safe_get_value(const toml::table& table, std::string_view key,
const T& default_value) {
auto value = table[key].value<T>();
if (value == std::nullopt) {
Logger::warn("Key {} Is Not Find, Wiil Set the Default Value {}", key,
default_value);
value = default_value;
}
return value;
}
template <typename U>
requires std::convertible_to<U, std::string>
std::optional<std::string> safe_get_value(const toml::table& table,
std::string_view key,
U&& default_value) {
return safe_get_value<std::string>(
table, key, std::string(std::forward<U>(default_value)));
}
} // namespace TOML
} // namespace Cubed