Skip to content

Commit

Permalink
Unified custom archives
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed May 28, 2024
1 parent cf00054 commit e187769
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 270 deletions.
62 changes: 45 additions & 17 deletions immer/extra/persist/json/json_immer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <cereal/archives/json.hpp>

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

/**
* Special types of archives, working with JSON, that support providing extra
* context (Pools) to serialize immer data structures.
Expand Down Expand Up @@ -50,24 +52,34 @@ struct blackhole_output_archive
* Adapted from cereal/archives/adapters.hpp
*/

template <class Previous, class Pools>
template <class Previous, class Pools, class WrapF = boost::hana::id_t>
class json_immer_output_archive
: public cereal::OutputArchive<json_immer_output_archive<Previous, Pools>>
: public cereal::OutputArchive<
json_immer_output_archive<Previous, Pools, WrapF>>
, public cereal::traits::TextArchive
{
public:
template <class... Args>
explicit json_immer_output_archive(Args&&... args)
: cereal::OutputArchive<
json_immer_output_archive<Previous, Pools>>{this}
requires std::is_same_v<WrapF, boost::hana::id_t>
: cereal::OutputArchive<json_immer_output_archive>{this}
, previous{std::forward<Args>(args)...}
{
}

template <class... Args>
json_immer_output_archive(Pools pools_, Args&&... args)
: cereal::OutputArchive<
json_immer_output_archive<Previous, Pools>>{this}
requires std::is_same_v<WrapF, boost::hana::id_t>
: cereal::OutputArchive<json_immer_output_archive>{this}
, previous{std::forward<Args>(args)...}
, pools{std::move(pools_)}
{
}

template <class... Args>
json_immer_output_archive(Pools pools_, WrapF wrap_, Args&&... args)
: cereal::OutputArchive<json_immer_output_archive>{this}
, wrap{std::move(wrap_)}
, previous{std::forward<Args>(args)...}
, pools{std::move(pools_)}
{
Expand Down Expand Up @@ -103,7 +115,7 @@ class json_immer_output_archive
cereal::NameValuePair<T> const& t)
{
ar.previous.setNextName(t.name);
ar(t.value);
ar(ar.wrap(t.value));
}

friend void CEREAL_SAVE_FUNCTION_NAME(json_immer_output_archive& ar,
Expand Down Expand Up @@ -141,19 +153,31 @@ class json_immer_output_archive
}

private:
WrapF wrap;
Previous previous;
Pools pools;
};

template <class Previous, class Pools>
template <class Previous, class Pools, class WrapF = boost::hana::id_t>
class json_immer_input_archive
: public cereal::InputArchive<json_immer_input_archive<Previous, Pools>>
: public cereal::InputArchive<
json_immer_input_archive<Previous, Pools, WrapF>>
, public cereal::traits::TextArchive
{
public:
template <class... Args>
json_immer_input_archive(Pools pools_, Args&&... args)
: cereal::InputArchive<json_immer_input_archive<Previous, Pools>>{this}
requires std::is_same_v<WrapF, boost::hana::id_t>
: cereal::InputArchive<json_immer_input_archive>{this}
, previous{std::forward<Args>(args)...}
, pools{std::move(pools_)}
{
}

template <class... Args>
json_immer_input_archive(Pools pools_, WrapF wrap_, Args&&... args)
: cereal::InputArchive<json_immer_input_archive>{this}
, wrap{std::move(wrap_)}
, previous{std::forward<Args>(args)...}
, pools{std::move(pools_)}
{
Expand Down Expand Up @@ -183,7 +207,8 @@ class json_immer_input_archive
cereal::NameValuePair<T>& t)
{
ar.previous.setNextName(t.name);
ar(t.value);
auto&& wrapped = ar.wrap(t.value);
ar(wrapped);
}

friend void CEREAL_LOAD_FUNCTION_NAME(json_immer_input_archive& ar,
Expand Down Expand Up @@ -220,6 +245,7 @@ class json_immer_input_archive
}

private:
WrapF wrap;
Previous previous;
Pools pools;
};
Expand All @@ -230,17 +256,19 @@ class json_immer_input_archive
namespace cereal {
namespace traits {
namespace detail {
template <class Previous, class Pools>
template <class Previous, class Pools, class WrapF>
struct get_output_from_input<
immer::persist::json_immer_input_archive<Previous, Pools>>
immer::persist::json_immer_input_archive<Previous, Pools, WrapF>>
{
using type = immer::persist::json_immer_output_archive<Previous, Pools>;
using type =
immer::persist::json_immer_output_archive<Previous, Pools, WrapF>;
};
template <class Previous, class Pools>
template <class Previous, class Pools, class WrapF>
struct get_input_from_output<
immer::persist::json_immer_output_archive<Previous, Pools>>
immer::persist::json_immer_output_archive<Previous, Pools, WrapF>>
{
using type = immer::persist::json_immer_input_archive<Previous, Pools>;
using type =
immer::persist::json_immer_input_archive<Previous, Pools, WrapF>;
};
} // namespace detail
} // namespace traits
Expand Down
203 changes: 0 additions & 203 deletions immer/extra/persist/json/json_immer_auto.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions immer/extra/persist/json/json_with_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ constexpr bool is_pool_empty()
}

// Recursively serializes the pools but not calling finalize
template <class Previous, class Pools, class SavePoolF>
void save_pools_impl(json_immer_output_archive<Previous, Pools>& ar,
template <class Previous, class Pools, class WrapF, class SavePoolF>
void save_pools_impl(json_immer_output_archive<Previous, Pools, WrapF>& ar,
const SavePoolF& save_pool)
{
using Names = typename Pools::names_t;
Expand Down
Loading

0 comments on commit e187769

Please sign in to comment.