Skip to content

Commit

Permalink
Support loading from streams
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed Feb 27, 2024
1 parent 78798e3 commit 9121da5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
34 changes: 24 additions & 10 deletions immer/extra/archive/json/json_with_archive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <immer/extra/archive/json/json_immer.hpp>
#include <immer/extra/archive/traits.hpp>
#include <immer/extra/io.hpp>

#include <boost/hana.hpp>

Expand Down Expand Up @@ -286,7 +287,7 @@ auto to_json_with_archive(const T& serializable)
}

template <typename T>
auto load_archives(const std::string& input)
auto load_archives(std::istream& is)
{
using Archives = std::decay_t<decltype(detail::generate_archives_load(
get_archives_types(std::declval<T>())))>;
Expand All @@ -296,13 +297,13 @@ auto load_archives(const std::string& input)
}

{
auto is = std::istringstream{input};
auto ar = cereal::JSONInputArchive{is};
auto restore = util::istream_snapshot{is};
auto ar = cereal::JSONInputArchive{is};
ar(CEREAL_NVP(archives));
}

const auto reload_archive = [&] {
auto is = std::istringstream{input};
auto restore = util::istream_snapshot{is};
auto ar =
immer::archive::json_immer_input_archive<Archives>{archives, is};
/**
Expand All @@ -328,35 +329,48 @@ auto load_archives(const std::string& input)
}

template <typename T>
T from_json_with_archive(const std::string& input)
T from_json_with_archive(std::istream& is)
{
using Archives = std::decay_t<decltype(detail::generate_archives_load(
get_archives_types(std::declval<T>())))>;
auto archives = load_archives<T>(input);
auto archives = load_archives<T>(is);

auto is = std::istringstream{input};
auto ar = immer::archive::json_immer_input_archive<Archives>{
std::move(archives), is};
auto r = T{};
ar(r);
return r;
}

template <typename T>
T from_json_with_archive(const std::string& input)
{
auto is = std::istringstream{input};
return from_json_with_archive<T>(is);
}

template <typename T, typename OldType, typename ConversionsMap>
T from_json_with_archive_with_conversion(const std::string& input,
T from_json_with_archive_with_conversion(std::istream& is,
const ConversionsMap& map)
{
// Load the archives part for the old type
auto archives_old = load_archives<OldType>(input);
auto archives_old = load_archives<OldType>(is);
auto archives = archives_old.transform(map);
using Archives = decltype(archives);

auto is = std::istringstream{input};
auto ar = immer::archive::json_immer_input_archive<Archives>{
std::move(archives), is};
auto r = T{};
ar(r);
return r;
}

template <typename T, typename OldType, typename ConversionsMap>
T from_json_with_archive_with_conversion(const std::string& input,
const ConversionsMap& map)
{
auto is = std::istringstream{input};
return from_json_with_archive_with_conversion<T, OldType>(is, map);
}

} // namespace immer::archive
37 changes: 37 additions & 0 deletions immer/extra/io.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <istream>

namespace immer::util {

/*!
* Restores the iostream state.
*
* This is particularly handy for utilities that can be used to peek or query
* properties of a document read from a stream, while leaving the stream in the
* original state.
*/
struct istream_snapshot
{
std::reference_wrapper<std::istream> stream;
std::istream::pos_type pos = stream.get().tellg();
std::istream::iostate state = stream.get().rdstate();
std::istream::iostate exceptions = stream.get().exceptions();

istream_snapshot(std::istream& is)
: stream{is}
{
}

// It is not copyable nor movable
istream_snapshot(istream_snapshot&&) = delete;

~istream_snapshot()
{
stream.get().exceptions(exceptions);
stream.get().clear(state);
stream.get().seekg(pos);
}
};

} // namespace immer::util

0 comments on commit 9121da5

Please sign in to comment.