-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split with_pools.hpp into save and load
- Loading branch information
1 parent
6b47b15
commit c77ca34
Showing
14 changed files
with
178 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.