### Error String Parsing Example Source: https://github.com/toge/glz-util/blob/main/README.md This example demonstrates how to parse error payloads from Glaze when configuration loading fails. ```cpp #include #include #include "glz-util/env.hpp" struct EnvParseError { std::string env; std::string value; std::string detail; }; template <> struct glz::meta { using T = EnvParseError; static constexpr auto value = glz::object( "env", &T::env, "value", &T::value, "detail", &T::detail ); }; int main() { auto cfg = glz_util::from_env(); if (!cfg) { EnvParseError parsed{}; if (auto ec = glz::read_json(parsed, cfg.error()); ec) { std::cerr << "failed to parse error payload: " << cfg.error() << '\n'; return 1; } std::cerr << "env=" << parsed.env << " value=" << parsed.value << " detail=" << parsed.detail << '\n'; return 1; } } ``` -------------------------------- ### Loading with options (e.g., JSON) Source: https://github.com/toge/glz-util/blob/main/README.md Example of loading configuration with specific options, such as JSON format, using `glz_util::from_env`. ```cpp auto cfg = glz_util::from_env(); if (!cfg) { // エラー時はstd::unexpected // cfg.error() で詳細を取得できます } ``` -------------------------------- ### st_tree and Glaze Interoperability Source: https://github.com/toge/glz-util/blob/main/README.md Example demonstrating the interoperability between st_tree and Glaze. ```cpp #include #include #include "st_tree.h" #include "glz-util/st_tree.hpp" int main() { auto buffer = R"(["root",[["left",[]],["right",[["leaf",[]]]]])"; auto target = st_tree_wrapper{}; if (auto ec = glz::read_json(target, buffer); ec) { return 1; } auto const& raw = target.raw(); std::cout << raw.root().data() << '\n'; std::cout << raw.root()[1][0].data() << '\n'; auto json = std::string{}; if (auto ec = glz::write_json(target, json); ec) { return 1; } std::cout << json << '\n'; } ``` -------------------------------- ### Getting the difference between struct members Source: https://github.com/toge/glz-util/blob/main/README.md This example demonstrates how to compare two structs of the same type and get the changed fields using `glz_util::diff_members`. ```cpp #include #include "glz-util/diff.hpp" struct Config { int timeout = 30; std::string host = "localhost"; }; int main() { auto const before = Config{}; auto const after = Config{.timeout = 60, .host = "example.com"}; auto const diffs = glz_util::diff_members(before, after); // diffs[0] = { "timeout", "30", "60" } // diffs[1] = { "host", "\"localhost\"", "\"example.com\"" } auto json = std::string{}; if (auto const ec = glz::write_json(diffs, json); ec) { return 1; } } ``` -------------------------------- ### CMake Usage with ZFP Wrapper Source: https://github.com/toge/glz-util/blob/main/README.md Example of linking glz-util and ZFP libraries in CMake. ```cmake project(your_project LANGUAGES C CXX) find_package(glz-util CONFIG REQUIRED) find_package(zfp CONFIG REQUIRED) target_link_libraries(your_target PRIVATE glz-util::glz-util zfp::zfp ) ``` -------------------------------- ### ZFP and Glaze Interoperability Source: https://github.com/toge/glz-util/blob/main/README.md Example showing how to use ZFP array wrappers with Glaze for JSON serialization/deserialization. ```cpp #include #include #include #include "glz-util/zfp.hpp" int main() { auto buffer = R"([[1.0,2.0],[3.0,4.0]])"; auto target = zfp_array2_wrapper{16.0}; if (auto ec = glz::read_json(target, buffer); ec) { return 1; } auto const& raw = target.raw(); std::cout << raw.size_x() << "x" << raw.size_y() << '\n'; auto json = std::string{}; if (auto ec = glz::write_json(target, json); ec) { return 1; } std::cout << json << '\n'; } ``` -------------------------------- ### CMake Usage Source: https://github.com/toge/glz-util/blob/main/README.md Example of how to link the glz-util library in a CMake project. ```cmake find_package(glz-util CONFIG REQUIRED) target_link_libraries(your_target PRIVATE glz-util::glz-util) ``` -------------------------------- ### Loading a struct from command line arguments Source: https://github.com/toge/glz-util/blob/main/README.md This example shows how to load configuration from command line arguments into a C++ struct using `glz_util::from_args`. ```cpp #include #include "glz-util/args.hpp" struct AppConfig { int port = 0; float ratio = 0.0f; std::string message; bool enabled = false; }; template <> struct glz::meta { using T = AppConfig; static constexpr auto value = glz::object( "port", &T::port, "ratio", &T::ratio, "message", &T::message, "enabled", &T::enabled ); }; int main(int argc, char** argv) { auto cfg = glz_util::from_args(argc, argv); if (!cfg) { if (cfg.error().is_help_requested()) { return 0; } return 1; } auto const& value = *cfg; } ``` -------------------------------- ### Loading a struct from environment variables Source: https://github.com/toge/glz-util/blob/main/README.md This example demonstrates how to load configuration from environment variables into a C++ struct using `glz_util::from_env`. ```cpp #include #include "glz-util/env.hpp" struct AppConfig { int number1 = 0; float number2 = 0.0f; std::string message; }; template <> struct glz::meta { using T = AppConfig; static constexpr auto value = glz::object( "NUMBER_1", &T::number1, "NUMBER_2", &T::number2, "MESSAGE", &T::message ); }; int main() { // 例: export NUMBER_1=42 auto cfg = glz_util::from_env(); if (!cfg) { // 例: エラーメッセージを出力して終了 // std::cerr << cfg.error() << std::endl; return 1; } auto const& value = *cfg; } ``` -------------------------------- ### HAT-trie and Glaze Interoperability Source: https://github.com/toge/glz-util/blob/main/README.md Example showing how to use tsl::htrie_map with Glaze for JSON parsing. ```cpp #include #include "tsl/htrie_map.h" #include "glz-util/tsl-hat-trie.hpp" int main() { auto buffer = R"({\"123\":\"hello\",\"987\":\"world\"})"; auto target = htrie_map_wrapper{}; glz::read_json(target, buffer); auto const& target2 = target.raw(); std::cout << target2.at("123") << '\n'; // => "hello" std::cout << target2.at("987") << '\n'; // => "world" } ``` -------------------------------- ### Using generated struct with Glaze Source: https://github.com/toge/glz-util/blob/main/README.md Example of reading JSON data into a struct generated by json_schema_codegen. ```cpp #include "app_config.hpp" int main() { AppConfig config{}; std::string buffer; if (auto ec = glz::read_file_json(config, "config.json", buffer); ec) { return 1; } } ``` -------------------------------- ### CMakeLists.txt Configuration Source: https://github.com/toge/glz-util/blob/main/test/CMakeLists.txt This snippet shows the CMake configuration for building the glz-util project and its tests. ```cmake find_package(Catch2 REQUIRED CONFIG) enable_language(C) find_package(zfp CONFIG REQUIRED) find_path(ST_TREE_INCLUDE_DIRS "st_tree.h") find_path(TSL_HAT_TRIE_INCLUDE_DIRS "tsl/array-hash/array_growth_policy.h") file(GLOB test_src test_*.cpp) add_executable(all_test main.cpp ${test_src}) target_link_libraries(all_test PRIVATE Catch2::Catch2 Catch2::Catch2WithMain glz-util::glz-util zfp::zfp ) target_include_directories(all_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/util ${ST_TREE_INCLUDE_DIRS} ${TSL_HAT_TRIE_INCLUDE_DIRS} ) target_compile_features(all_test PRIVATE ${STD_CPP}) add_test(NAME all_test COMMAND all_test -r junit) ``` -------------------------------- ### Generate struct from JSON Schema Source: https://github.com/toge/glz-util/blob/main/README.md Command-line usage for json_schema_codegen to generate C++ header files from JSON Schema. ```bash json_schema_codegen schema.json --root AppConfig --output app_config.hpp ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.