libkazv
immer-array.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 
9 #include <libkazv-config.hpp>
10 
11 #include <boost/serialization/nvp.hpp>
12 #include <boost/serialization/split_free.hpp>
13 
14 #include <immer/array.hpp>
15 #include <immer/array_transient.hpp>
16 
18 {
19  template<class Archive,
20  class T,
21  class MP>
22  void save(Archive &ar, const immer::array<T, MP> &v, const unsigned int /* version */)
23  {
24  auto size = v.size();
25  ar << BOOST_SERIALIZATION_NVP(size);
26 
27  for (const auto &i : v) {
28  ar << i;
29  }
30  }
31 
32  template<class Archive,
33  class T,
34  class MP>
35  void load(Archive &ar, immer::array<T, MP> &v, const unsigned int /* version */)
36  {
37  using VecT = immer::array<T, MP>;
38  using TransientT = decltype(v.transient());
39  using SizeT = decltype(v.size());
40  using ValueT = typename VecT::value_type;
41 
42  SizeT size{};
43  ar >> BOOST_SERIALIZATION_NVP(size);
44  TransientT transient = VecT(size).transient();
45 
46  for (auto i = SizeT{}; i != size; ++i) {
47  ValueT val;
48  ar >> val;
49  transient.set(i, std::move(val));
50  }
51 
52  v = transient.persistent();
53  }
54 
55  template<class Archive,
56  class T,
57  class MP>
58  inline void serialize(Archive &ar, immer::array<T, MP> &v, const unsigned int version)
59  {
60  boost::serialization::split_free(ar, v, version);
61  }
62 }
Definition: immer-array.hpp:18
void load(Archive &ar, immer::array< T, MP > &v, const unsigned int)
Definition: immer-array.hpp:35
void serialize(Archive &ar, immer::array< T, MP > &v, const unsigned int version)
Definition: immer-array.hpp:58
void save(Archive &ar, const immer::array< T, MP > &v, const unsigned int)
Definition: immer-array.hpp:22