Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions include/REX/REX/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,39 @@ namespace REX::JSON

template <class Store = SettingStore>
using Str = Setting<std::string, Store>;

template <class T, class Store = SettingStore>
using SettingA = Setting<std::vector<T>, Store>;

template <class Store = SettingStore>
using BoolA = SettingA<bool, Store>;

template <class Store = SettingStore>
using F32A = SettingA<float, Store>;

template <class Store = SettingStore>
using F64A = SettingA<double, Store>;

template <class Store = SettingStore>
using I8A = SettingA<std::int8_t, Store>;

template <class Store = SettingStore>
using I16A = SettingA<std::int16_t, Store>;

template <class Store = SettingStore>
using I32A = SettingA<std::int32_t, Store>;

template <class Store = SettingStore>
using U8A = SettingA<std::uint8_t, Store>;

template <class Store = SettingStore>
using U16A = SettingA<std::uint16_t, Store>;

template <class Store = SettingStore>
using U32A = SettingA<std::uint32_t, Store>;

template <class Store = SettingStore>
using StrA = SettingA<std::string, Store>;
}

template <class T, class S, class CharT>
Expand Down
65 changes: 39 additions & 26 deletions src/REX/REX/JSON.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "REX/REX/JSON.h"

#include "REX/REX/LOG.h"

#ifdef COMMONLIB_OPTION_JSON
# include <nlohmann/json.hpp>
# include <glaze/glaze.hpp>

namespace REX::JSON
{
Expand All @@ -16,11 +14,12 @@ namespace REX::JSON
T& a_value,
T& a_valueDefault)
{
const auto& json = *static_cast<nlohmann::json*>(a_data);
if (a_path[0] == '/') {
a_value = json.value<T>(nlohmann::json::json_pointer(a_path.data()), a_valueDefault);
const auto& json = *static_cast<glz::generic*>(a_data);
if (a_path[0] != '/') {
const auto path = std::format("/{}"sv, a_path);
a_value = glz::get<T>(json, path).value_or(a_valueDefault);
} else {
a_value = json.value<T>(a_path, a_valueDefault);
a_value = glz::get<T>(json, a_path).value_or(a_valueDefault);
}
}

Expand All @@ -34,18 +33,29 @@ namespace REX::JSON
template void SettingLoad<std::int16_t>(void*, path_t, std::int16_t&, std::int16_t&);
template void SettingLoad<std::int32_t>(void*, path_t, std::int32_t&, std::int32_t&);
template void SettingLoad<std::string>(void*, path_t, std::string&, std::string&);
template void SettingLoad<std::vector<bool>>(void*, path_t, std::vector<bool>&, std::vector<bool>&);
template void SettingLoad<std::vector<float>>(void*, path_t, std::vector<float>&, std::vector<float>&);
template void SettingLoad<std::vector<double>>(void*, path_t, std::vector<double>&, std::vector<double>&);
template void SettingLoad<std::vector<std::uint8_t>>(void*, path_t, std::vector<std::uint8_t>&, std::vector<std::uint8_t>&);
template void SettingLoad<std::vector<std::uint16_t>>(void*, path_t, std::vector<std::uint16_t>&, std::vector<std::uint16_t>&);
template void SettingLoad<std::vector<std::uint32_t>>(void*, path_t, std::vector<std::uint32_t>&, std::vector<std::uint32_t>&);
template void SettingLoad<std::vector<std::int8_t>>(void*, path_t, std::vector<std::int8_t>&, std::vector<std::int8_t>&);
template void SettingLoad<std::vector<std::int16_t>>(void*, path_t, std::vector<std::int16_t>&, std::vector<std::int16_t>&);
template void SettingLoad<std::vector<std::int32_t>>(void*, path_t, std::vector<std::int32_t>&, std::vector<std::int32_t>&);
template void SettingLoad<std::vector<std::string>>(void*, path_t, std::vector<std::string>&, std::vector<std::string>&);

template <class T>
void SettingSave<T>(
void* a_data,
path_t a_path,
T& a_value)
{
auto& json = *static_cast<nlohmann::json*>(a_data);
if (a_path[0] == '/') {
json[nlohmann::json::json_pointer(a_path.data())] = a_value;
auto& json = *static_cast<glz::generic*>(a_data);
if (a_path[0] != '/') {
const auto path = std::format("/{}"sv, a_path);
glz::set(json, path, a_value);
} else {
json[a_path] = a_value;
glz::set(json, a_path, a_value);
}
}

Expand All @@ -59,49 +69,52 @@ namespace REX::JSON
template void SettingSave<std::int16_t>(void*, path_t, std::int16_t&);
template void SettingSave<std::int32_t>(void*, path_t, std::int32_t&);
template void SettingSave<std::string>(void*, path_t, std::string&);
template void SettingSave<std::vector<bool>>(void*, path_t, std::vector<bool>&);
template void SettingSave<std::vector<float>>(void*, path_t, std::vector<float>&);
template void SettingSave<std::vector<double>>(void*, path_t, std::vector<double>&);
template void SettingSave<std::vector<std::uint8_t>>(void*, path_t, std::vector<std::uint8_t>&);
template void SettingSave<std::vector<std::uint16_t>>(void*, path_t, std::vector<std::uint16_t>&);
template void SettingSave<std::vector<std::uint32_t>>(void*, path_t, std::vector<std::uint32_t>&);
template void SettingSave<std::vector<std::int8_t>>(void*, path_t, std::vector<std::int8_t>&);
template void SettingSave<std::vector<std::int16_t>>(void*, path_t, std::vector<std::int16_t>&);
template void SettingSave<std::vector<std::int32_t>>(void*, path_t, std::vector<std::int32_t>&);
template void SettingSave<std::vector<std::string>>(void*, path_t, std::vector<std::string>&);
}

void SettingStore::Load()
{
if (std::filesystem::exists(m_fileBase)) {
std::ifstream file{ m_fileBase.data() };
try {
auto result = nlohmann::json::parse(file);
glz::generic result{};
if (!glz::read_file_json(result, m_fileBase, std::string{})) {
for (auto setting : m_settings) {
setting->Load(&result, true);
}
} catch (const std::exception& e) {
REX::ERROR("{}", e.what());
}
}

if (std::filesystem::exists(m_fileUser)) {
std::ifstream file{ m_fileUser.data() };
try {
auto result = nlohmann::json::parse(file);
glz::generic result{};
if (!glz::read_file_json(result, m_fileUser, std::string{})) {
for (auto setting : m_settings) {
setting->Load(&result, false);
}
} catch (const std::exception& e) {
REX::ERROR("{}", e.what());
}
}
}

void SettingStore::Save()
{
nlohmann::json output{};
glz::generic output{};
if (std::filesystem::exists(m_fileBase)) {
std::ifstream file{ m_fileBase.data() };
output = nlohmann::json::parse(file);
(void)glz::read_file_json(output, m_fileBase, std::string{});
}

for (auto& setting : m_settings) {
setting->Save(&output);
}

std::ofstream file{ m_fileBase.data(), std::ios::trunc };
file << std::setw(4) << output;
constexpr glz::opts opts{ .prettify = true, .indentation_width = 4 };
(void)glz::write_file_json<opts>(output, m_fileBase, std::string{});
}
}
#endif
4 changes: 2 additions & 2 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ add_requireconfs("*.cmake", { configs = { override = true, system = false } })

-- add config packages
if has_config("commonlib_ini") then add_requires("simpleini") end
if has_config("commonlib_json") then add_requires("nlohmann_json") end
if has_config("commonlib_json") then add_requires("glaze") end
if has_config("commonlib_toml") then add_requires("toml11") end
if has_config("commonlib_xbyak") then add_requires("xbyak") end

Expand All @@ -57,7 +57,7 @@ target("commonlib-shared", function()
end

if has_config("commonlib_json") then
add_packages("nlohmann_json", { public = true })
add_packages("glaze", { public = true })
add_defines("COMMONLIB_OPTION_JSON=1", { public = true })
end

Expand Down