From 6a8f10f47147358569ea6083393a211298735efc Mon Sep 17 00:00:00 2001 From: Alex Shabalin Date: Mon, 26 Aug 2024 15:34:59 +0200 Subject: [PATCH] Add option to pass parameters to cereal archive --- immer/extra/persist/cereal/with_pools.hpp | 27 +++++++++++++++-------- test/extra/persist/test_special_pool.cpp | 5 ++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/immer/extra/persist/cereal/with_pools.hpp b/immer/extra/persist/cereal/with_pools.hpp index 694dc24a..2828ddb8 100644 --- a/immer/extra/persist/cereal/with_pools.hpp +++ b/immer/extra/persist/cereal/with_pools.hpp @@ -21,10 +21,12 @@ namespace immer::persist { */ template Policy = default_policy> + Policy Policy = default_policy, + class... Args> void cereal_save_with_pools(std::ostream& os, const T& value0, - const Policy& policy = Policy{}) + const Policy& policy = Policy{}, + Args&&... args) { const auto types = boost::hana::to_set(policy.get_pool_types(value0)); auto pools = detail::generate_output_pools(types); @@ -37,7 +39,8 @@ void cereal_save_with_pools(std::ostream& os, Archive, Pools, decltype(wrap), - decltype(get_pool_name_fn)>{pools, wrap, os}; + decltype(get_pool_name_fn)>{ + pools, wrap, os, std::forward(args)...}; policy.save(ar, value0); // Calling finalize explicitly, as it might throw on saving the pools, // for example if pool names are not unique. @@ -54,12 +57,15 @@ void cereal_save_with_pools(std::ostream& os, */ template Policy = default_policy> + Policy Policy = default_policy, + class... Args> std::string cereal_save_with_pools(const T& value0, - const Policy& policy = Policy{}) + const Policy& policy = Policy{}, + Args&&... args) { auto os = std::ostringstream{}; - cereal_save_with_pools(os, value0, policy); + cereal_save_with_pools( + os, value0, policy, std::forward(args)...); return os.str(); } @@ -72,8 +78,11 @@ std::string cereal_save_with_pools(const T& value0, */ template Policy = default_policy> -T cereal_load_with_pools(std::istream& is, const Policy& policy = Policy{}) + Policy Policy = default_policy, + class... Args> +T cereal_load_with_pools(std::istream& is, + const Policy& policy = Policy{}, + Args&&... args) { using TypesSet = decltype(boost::hana::to_set(policy.get_pool_types(std::declval()))); @@ -92,7 +101,7 @@ T cereal_load_with_pools(std::istream& is, const Policy& policy = Policy{}) Pools, decltype(wrap), PoolNameFn>{ - std::move(pools), wrap, is}; + std::move(pools), wrap, is, std::forward(args)...}; auto value0 = T{}; policy.load(ar, value0); return value0; diff --git a/test/extra/persist/test_special_pool.cpp b/test/extra/persist/test_special_pool.cpp index 8438fb7f..60364078 100644 --- a/test/extra/persist/test_special_pool.cpp +++ b/test/extra/persist/test_special_pool.cpp @@ -270,7 +270,10 @@ TEST_CASE("Special pool minimal test") }, }; - const auto json_str = immer::persist::cereal_save_with_pools(test1); + const auto json_str = immer::persist::cereal_save_with_pools( + test1, + immer::persist::default_policy{}, + cereal::JSONOutputArchive::Options::NoIndent()); // REQUIRE(json_str == ""); {