Skip to content

Commit

Permalink
Add option to pass parameters to cereal archive
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed Aug 26, 2024
1 parent 6de8600 commit 6a8f10f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
27 changes: 18 additions & 9 deletions immer/extra/persist/cereal/with_pools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ namespace immer::persist {
*/
template <class Archive = cereal::JSONOutputArchive,
class T,
Policy<T> Policy = default_policy>
Policy<T> 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);
Expand All @@ -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>(args)...};
policy.save(ar, value0);
// Calling finalize explicitly, as it might throw on saving the pools,
// for example if pool names are not unique.
Expand All @@ -54,12 +57,15 @@ void cereal_save_with_pools(std::ostream& os,
*/
template <class Archive = cereal::JSONOutputArchive,
class T,
Policy<T> Policy = default_policy>
Policy<T> 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<Archive>(os, value0, policy);
cereal_save_with_pools<Archive>(
os, value0, policy, std::forward<Args>(args)...);
return os.str();
}

Expand All @@ -72,8 +78,11 @@ std::string cereal_save_with_pools(const T& value0,
*/
template <class T,
class Archive = cereal::JSONInputArchive,
Policy<T> Policy = default_policy>
T cereal_load_with_pools(std::istream& is, const Policy& policy = Policy{})
Policy<T> 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<T>())));
Expand All @@ -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>(args)...};
auto value0 = T{};
policy.load(ar, value0);
return value0;
Expand Down
5 changes: 4 additions & 1 deletion test/extra/persist/test_special_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "");

{
Expand Down

0 comments on commit 6a8f10f

Please sign in to comment.