Skip to content

Commit

Permalink
Less repetition with load_pools
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed May 30, 2024
1 parent f826285 commit aa97845
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 69 deletions.
41 changes: 21 additions & 20 deletions immer/extra/persist/json/json_with_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,27 @@ auto to_json_with_pool(const T& value0)
return os.str();
}

template <typename Pools, class ReloadPoolF>
auto load_pools(std::istream& is, const ReloadPoolF& reload_pool)
template <class Pools, class Archive = cereal::JSONInputArchive>
auto load_pools(std::istream& is, const auto& wrap)
{
const auto reload_pool =
[wrap](std::istream& is, Pools pools, bool ignore_pool_exceptions) {
auto restore = immer::util::istream_snapshot{is};
const auto original_pools = pools;
pools.ignore_pool_exceptions = ignore_pool_exceptions;
auto ar = json_immer_input_archive<Archive, Pools, decltype(wrap)>{
std::move(pools), wrap, is};
/**
* NOTE: Critical to clear the pools before loading into it
* again. I hit a bug when pools contained a vector and every
* load would append to it, instead of replacing the contents.
*/
pools = {};
ar(CEREAL_NVP(pools));
pools.merge_previous(original_pools);
return pools;
};

auto pools = Pools{};
if constexpr (detail::is_pool_empty<Pools>()) {
return pools;
Expand All @@ -491,29 +509,12 @@ auto load_pools(std::istream& is, const ReloadPoolF& reload_pool)
return pools;
}

constexpr auto reload_pool =
[](std::istream& is, auto pools, bool ignore_pool_exceptions) {
using Pools = std::decay_t<decltype(pools)>;
auto restore = util::istream_snapshot{is};
pools.ignore_pool_exceptions = ignore_pool_exceptions;
auto ar = json_immer_input_archive<cereal::JSONInputArchive, Pools>{
std::move(pools), is};
/**
* NOTE: Critical to clear the pools before loading into it
* again. I hit a bug when pools contained a vector and every
* load would append to it, instead of replacing the contents.
*/
pools = {};
ar(CEREAL_NVP(pools));
return pools;
};

template <typename T>
T from_json_with_pool(std::istream& is)
{
using Pools = std::decay_t<decltype(detail::generate_input_pools(
get_pools_types(std::declval<T>())))>;
auto pools = load_pools<Pools>(is, reload_pool);
auto pools = load_pools<Pools>(is, boost::hana::id);

auto ar =
immer::persist::json_immer_input_archive<cereal::JSONInputArchive,
Expand Down
24 changes: 1 addition & 23 deletions immer/extra/persist/json/json_with_pool_auto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,28 +200,6 @@ auto get_auto_pool(const T& serializable,
return pools;
}

constexpr auto reload_pool_auto = [](auto wrap) {
return [wrap](std::istream& is, auto pools, bool ignore_pool_exceptions) {
using Pools = std::decay_t<decltype(pools)>;
auto restore = immer::util::istream_snapshot{is};
const auto original_pools = pools;
pools.ignore_pool_exceptions = ignore_pool_exceptions;
auto ar = json_immer_input_archive<cereal::JSONInputArchive,
Pools,
decltype(wrap)>{
std::move(pools), wrap, is};
/**
* NOTE: Critical to clear the pools before loading into it
* again. I hit a bug when pools contained a vector and every
* load would append to it, instead of replacing the contents.
*/
pools = {};
ar(CEREAL_NVP(pools));
pools.merge_previous(original_pools);
return pools;
};
};

template <typename T, class PoolsTypes>
T from_json_with_auto_pool(std::istream& is, const PoolsTypes& pools_types)
{
Expand All @@ -231,7 +209,7 @@ T from_json_with_auto_pool(std::istream& is, const PoolsTypes& pools_types)
using Pools =
std::decay_t<decltype(detail::generate_input_pools(pools_types))>;

auto pools = load_pools<Pools>(is, reload_pool_auto(wrap));
auto pools = load_pools<Pools>(is, wrap);

auto ar =
json_immer_input_archive<cereal::JSONInputArchive,
Expand Down
32 changes: 6 additions & 26 deletions test/extra/persist/test_circular_dependency_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,36 +708,16 @@ TEST_CASE("Test circular dependency pools", "[conversion]")
}
return os.str();
};
constexpr auto reload_pool_xml = [](auto wrap) {
return [wrap](std::istream& is,
auto pools,
bool ignore_pool_exceptions) {
using Pools = std::decay_t<decltype(pools)>;
auto restore = immer::util::istream_snapshot{is};
pools.ignore_pool_exceptions = ignore_pool_exceptions;
auto ar = immer::persist::json_immer_input_archive<
cereal::XMLInputArchive,
Pools,
decltype(wrap)>{std::move(pools), wrap, is};
/**
* NOTE: Critical to clear the pools before loading into it
* again. I hit a bug when pools contained a vector and every
* load would append to it, instead of replacing the contents.
*/
pools = {};
ar(CEREAL_NVP(pools));
return pools;
};
};
constexpr auto from_xml = [reload_pool_xml](const std::string& str,
auto& value0,
const auto& names,
const auto& wrap) {
constexpr auto from_xml = [](const std::string& str,
auto& value0,
const auto& names,
const auto& wrap) {
auto is = std::istringstream{str};
using Pools = std::decay_t<
decltype(immer::persist::detail::generate_input_pools(names))>;
auto pools =
immer::persist::load_pools<Pools>(is, reload_pool_xml(wrap));
immer::persist::load_pools<Pools, cereal::XMLInputArchive>(
is, wrap);

auto ar = immer::persist::json_immer_input_archive<
cereal::XMLInputArchive,
Expand Down

0 comments on commit aa97845

Please sign in to comment.