32#ifndef MADNESS_WORLD_CEREAL_ARCHIVE_H__INCLUDED
33#define MADNESS_WORLD_CEREAL_ARCHIVE_H__INCLUDED
39#ifdef MADNESS_HAS_CEREAL
40# if ! __has_include(<cereal/cereal.hpp>)
41# error "MADNESS_ENABLE_CEREAL is on, but cereal/cereal.hpp was not found."
45#include <cereal/cereal.hpp>
46#include <cereal/details/traits.hpp>
52template <
typename Muesli>
53class CerealOutputArchive :
public ::madness::archive::BaseOutputArchive {
54 mutable std::shared_ptr<Muesli>
58 CerealOutputArchive(Muesli &muesli) : muesli(&muesli, [](Muesli *) {}) {}
59 template <
typename Arg,
typename... RestOfArgs,
60 typename = std::enable_if_t<
61 !std::is_same<Muesli, std::decay_t<Arg>>::value>>
62 CerealOutputArchive(Arg &&arg, RestOfArgs &&... rest_of_args)
63 : muesli(new Muesli(std::forward<Arg>(
arg),
64 std::forward<RestOfArgs>(rest_of_args)...),
65 std::default_delete<Muesli>{}) {}
67 template <
class T,
class Cereal = Muesli>
68 inline std::enable_if_t<
70 !cereal::traits::is_text_archive<Cereal>::value,
72 store(
const T *t,
long n)
const {
73 const unsigned char *ptr = (
unsigned char *)t;
74 (*muesli)(cereal::binary_data(ptr,
sizeof(T) * n));
77 template <
class T,
class Cereal = Muesli>
78 inline std::enable_if_t<
80 cereal::traits::is_text_archive<Cereal>::value,
void>
81 store(
const T *t,
long n)
const {
82 for (
long i = 0; i != n; ++i)
86 void open(std::size_t hint) {}
92template <
typename Muesli>
class CerealInputArchive :
public BaseInputArchive {
93 std::shared_ptr<Muesli> muesli;
96 CerealInputArchive(Muesli &muesli) : muesli(&muesli, [](Muesli *) {}) {}
97 template <
typename Arg,
typename... RestOfArgs,
98 typename = std::enable_if_t<
99 !std::is_same<Muesli, std::decay_t<Arg>>::value>>
100 CerealInputArchive(Arg &&arg, RestOfArgs &&... rest_of_args)
101 : muesli(new Muesli(std::forward<Arg>(
arg),
102 std::forward<RestOfArgs>(rest_of_args)...),
103 std::default_delete<Muesli>{}) {}
105 template <
class T,
class Cereal = Muesli>
106 inline std::enable_if_t<
108 !cereal::traits::is_text_archive<Cereal>::value,
110 load(T *t,
long n)
const {
111 (*muesli)(cereal::binary_data(t,
sizeof(T) * n));
114 template <
class T,
class Cereal = Muesli>
115 inline std::enable_if_t<
117 cereal::traits::is_text_archive<Cereal>::value,
119 load(T *t,
long n)
const {
120 for (
long i = 0; i != n; ++i)
124 void open(std::size_t hint) {}
125 void rewind()
const {}
130template <
typename Muesli>
132 archive::CerealInputArchive<Muesli>,
133 std::enable_if_t<cereal::traits::is_text_archive<Muesli>::value>>
135template <
typename Muesli>
137 archive::CerealOutputArchive<Muesli>,
138 std::enable_if_t<cereal::traits::is_text_archive<Muesli>::value>>
141template <
typename Muesli,
typename T>
143 archive::CerealOutputArchive<Muesli>, T,
144 std::enable_if_t<(is_trivially_serializable<T>::value &&
145 !cereal::traits::is_text_archive<Muesli>::value) ||
146 (cereal::traits::detail::count_output_serializers<T, Muesli>::value != 0 &&
147 cereal::traits::is_text_archive<Muesli>::value)>>
149template <
typename Muesli,
typename T>
151 archive::CerealInputArchive<Muesli>, T,
153 (is_trivially_serializable<T>::value &&
154 !cereal::traits::is_text_archive<Muesli>::value) ||
155 (cereal::traits::detail::count_input_serializers<T, Muesli>::value != 0 &&
156 cereal::traits::is_text_archive<Muesli>::value)>>
159template <
typename Muesli>
161template <
typename Muesli>
166struct is_archive<
archive::CerealOutputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
168struct is_archive<
archive::CerealInputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
170struct is_output_archive<
archive::CerealOutputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
172struct is_input_archive<
archive::CerealInputArchive<T>, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
174struct is_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
176struct is_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
178struct is_output_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::OutputArchiveBase, T>>> : std::true_type {};
180struct is_input_archive<T, std::enable_if_t<std::is_base_of_v<cereal::detail::InputArchiveBase, T>>> : std::true_type {};
Interface templates for the archives (serialization).
Tensor< typename Tensor< T >::scalar_type > arg(const Tensor< T > &t)
Return a new tensor holding the argument of each element of t (complex types only).
Definition tensor.h:2772
void load(World &world, real_function_6d &f, std::string filename)
Definition helium_exact.cc:369
Namespace for I/O tools.
Definition madness.h:49
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
Checks if T is an archive type.
Definition type_traits.h:506
Definition cereal_archive.h:187
Definition type_traits.h:451
Checks if T is an output archive type.
Definition type_traits.h:539
This trait types tests if Archive is a text archive.
Definition type_traits.h:682
static const bool value
Definition type_traits.h:229