Skip to content

Commit

Permalink
Split with_pools.hpp into save and load
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed Aug 28, 2024
1 parent 6b47b15 commit c77ca34
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 163 deletions.
65 changes: 65 additions & 0 deletions immer/extra/persist/cereal/load.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <immer/extra/persist/cereal/policy.hpp>
#include <immer/extra/persist/detail/cereal/input_archive_util.hpp>
#include <immer/extra/persist/detail/cereal/pools.hpp>
#include <immer/extra/persist/detail/cereal/wrap.hpp>

namespace immer::persist {

/**
* @brief Load a value of the given type `T` from the provided stream using
* pools. By default, `cereal::JSONInputArchive` is used but a different
* `cereal` input archive can be provided.
*
* @ingroup persist-api
*/
template <class T,
class Archive = cereal::JSONInputArchive,
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>())));
using Pools = decltype(detail::generate_input_pools(TypesSet{}));

auto get_pool_name_fn = [](const auto& value) {
return Policy{}.get_pool_name(value);
};
using PoolNameFn = decltype(get_pool_name_fn);

const auto wrap =
detail::wrap_known_types(TypesSet{}, detail::wrap_for_loading);
auto pools = load_pools<Pools, Archive, PoolNameFn>(is, wrap);

auto ar = immer::persist::input_pools_cereal_archive_wrapper<Archive,
Pools,
decltype(wrap),
PoolNameFn>{
std::move(pools), wrap, is, std::forward<Args>(args)...};
auto value0 = T{};
policy.load(ar, value0);
return value0;
}

/**
* @brief Load a value of the given type `T` from the provided string using
* pools. By default, `cereal::JSONInputArchive` is used but a different
* `cereal` input archive can be provided.
*
* @ingroup persist-api
*/
template <class T,
class Archive = cereal::JSONInputArchive,
Policy<T> Policy = default_policy>
T cereal_load_with_pools(const std::string& input,
const Policy& policy = Policy{})
{
auto is = std::istringstream{input};
return cereal_load_with_pools<T, Archive>(is, policy);
}

} // namespace immer::persist
71 changes: 71 additions & 0 deletions immer/extra/persist/cereal/save.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <immer/extra/persist/cereal/policy.hpp>
#include <immer/extra/persist/detail/cereal/pools.hpp>
#include <immer/extra/persist/detail/cereal/wrap.hpp>

namespace immer::persist {

/**
* @defgroup persist-api
*/

/**
* @brief Serialize the provided value with pools using the provided policy
* outputting into the provided stream. By default, `cereal::JSONOutputArchive`
* is used but a different `cereal` output archive can be provided.
*
* @see Policy
* @ingroup persist-api
*/
template <class Archive = cereal::JSONOutputArchive,
class T,
Policy<T> Policy = default_policy,
class... Args>
void cereal_save_with_pools(std::ostream& os,
const T& value0,
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);
const auto wrap = detail::wrap_known_types(types, detail::wrap_for_saving);
using Pools = std::decay_t<decltype(pools)>;
auto get_pool_name_fn = [](const auto& value) {
return Policy{}.get_pool_name(value);
};
auto ar = immer::persist::output_pools_cereal_archive_wrapper<
Archive,
Pools,
decltype(wrap),
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.
ar.finalize();
}

/**
* @brief Serialize the provided value with pools using the provided policy. By
* default, `cereal::JSONOutputArchive` is used but a different `cereal` output
* archive can be provided.
*
* @return std::string The resulting JSON.
* @ingroup persist-api
*/
template <class Archive = cereal::JSONOutputArchive,
class T,
Policy<T> Policy = default_policy,
class... Args>
std::string cereal_save_with_pools(const T& value0,
const Policy& policy = Policy{},
Args&&... args)
{
auto os = std::ostringstream{};
cereal_save_with_pools<Archive>(
os, value0, policy, std::forward<Args>(args)...);
return os.str();
}

} // namespace immer::persist
155 changes: 0 additions & 155 deletions immer/extra/persist/cereal/with_pools.hpp

This file was deleted.

2 changes: 2 additions & 0 deletions immer/extra/persist/detail/array/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <immer/extra/cereal/immer_array.hpp>
#include <immer/extra/cereal/immer_vector.hpp>
#include <immer/extra/persist/detail/common/pool.hpp>
#include <immer/extra/persist/detail/traits.hpp>
#include <immer/extra/persist/errors.hpp>

#include <boost/hana/functional/id.hpp>

Expand Down
1 change: 1 addition & 0 deletions immer/extra/persist/detail/node_ptr.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <functional>
#include <immer/vector.hpp>

namespace immer::persist::detail {
Expand Down
28 changes: 28 additions & 0 deletions immer/extra/persist/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ namespace immer::persist {
* @defgroup persist-transform
*/

/**
* @brief Return just the pools of all the containers of the provided value
* serialized using the provided policy.
*
* @ingroup persist-transform
* @see convert_container
*/
template <typename T, Policy<T> Policy = hana_struct_auto_policy>
auto get_output_pools(const T& value0, const Policy& policy = Policy{})
{
const auto types = boost::hana::to_set(policy.get_pool_types(value0));
auto pools = detail::generate_output_pools(types);
const auto wrap = detail::wrap_known_types(types, detail::wrap_for_saving);
using Pools = std::decay_t<decltype(pools)>;

{
auto ar = output_pools_cereal_archive_wrapper<
detail::blackhole_output_archive,
Pools,
decltype(wrap),
detail::empty_name_fn>{pools, wrap};
ar(CEREAL_NVP(value0));
ar.finalize();
pools = std::move(ar).get_output_pools();
}
return pools;
}

/**
* Given output_pools and a map of transformations, produce a new type of
* input pools with those transformations applied.
Expand Down
5 changes: 3 additions & 2 deletions test/extra/persist/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ add_executable(
test_table_box_recursive.cpp
test_for_docs.cpp
${PROJECT_SOURCE_DIR}/immer/extra/persist/xxhash/xxhash_64.cpp)
target_precompile_headers(persist-tests PRIVATE
<immer/extra/persist/cereal/with_pools.hpp>)
target_precompile_headers(
persist-tests PRIVATE <immer/extra/persist/cereal/save.hpp>
<immer/extra/persist/cereal/load.hpp>)
add_dependencies(tests persist-tests)
add_test("test/persist-tests" persist-tests)
target_include_directories(persist-tests PRIVATE ${CMAKE_SOURCE_DIR})
Expand Down
2 changes: 1 addition & 1 deletion test/extra/persist/test_circular_dependency_conversion.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <catch2/catch_test_macros.hpp>

#include <immer/extra/persist/cereal/with_pools.hpp>
#include <immer/extra/persist/cereal/save.hpp>
#include <immer/extra/persist/transform.hpp>

#include "utils.hpp"
Expand Down
2 changes: 1 addition & 1 deletion test/extra/persist/test_conversion.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <catch2/catch_test_macros.hpp>

#include <immer/extra/persist/cereal/with_pools.hpp>
#include <immer/extra/persist/cereal/save.hpp>
#include <immer/extra/persist/detail/type_traverse.hpp>
#include <immer/extra/persist/transform.hpp>

Expand Down
2 changes: 1 addition & 1 deletion test/extra/persist/test_for_docs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <catch2/catch_test_macros.hpp>

#include <immer/extra/persist/cereal/with_pools.hpp>
#include <immer/extra/persist/cereal/save.hpp>
#include <immer/extra/persist/transform.hpp>

#include "utils.hpp"
Expand Down
2 changes: 1 addition & 1 deletion test/extra/persist/test_special_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "utils.hpp"

#include <boost/hana.hpp>
#include <immer/extra/persist/cereal/with_pools.hpp>
#include <immer/extra/persist/cereal/save.hpp>
#include <immer/extra/persist/xxhash/xxhash.hpp>

// to save std::pair
Expand Down
2 changes: 1 addition & 1 deletion test/extra/persist/test_special_pool_auto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "utils.hpp"

#include <immer/extra/persist/cereal/with_pools.hpp>
#include <immer/extra/persist/cereal/save.hpp>
#include <immer/extra/persist/transform.hpp>
#include <nlohmann/json.hpp>

Expand Down
Loading

0 comments on commit c77ca34

Please sign in to comment.