libkazv
immer-map.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 
8 #pragma once
9 
10 #include <libkazv-config.hpp>
11 
12 #include <boost/serialization/nvp.hpp>
13 #include <boost/serialization/split_free.hpp>
14 
15 #include <immer/map.hpp>
16 
17 namespace boost::serialization
18 {
19 
20  template <class Archive, class K, class T, class H, class E, class MP>
21  void save(Archive &ar, const immer::map<K, T, H, E, MP> &map, const unsigned int /* version */)
22  {
23  auto size = map.size();
24  ar << BOOST_SERIALIZATION_NVP(size);
25  for (const auto &[k, v] : map) {
26  ar << k << v;
27  }
28  }
29 
30  template <class Archive, class K, class T, class H, class E, class MP>
31  void load(Archive &ar, immer::map<K, T, H, E, MP> &map, const unsigned int /* version */)
32  {
33  // map transient is not yet implemented
34  // using TransientT = decltype(v.transient());
35  using SizeT = decltype(map.size());
36 
37  map = {};
38 
39  SizeT size{};
40  ar >> BOOST_SERIALIZATION_NVP(size);
41 
42  for (auto i = SizeT{}; i < size; ++i) {
43  K k;
44  T v;
45  ar >> k >> v;
46  map = std::move(map).set(std::move(k), std::move(v));
47  }
48 
49  assert(size == map.size());
50  }
51 
52  template<class Archive, class K, class T, class H, class E, class MP>
53  inline void serialize(Archive &ar, immer::map<K, T, H, E, MP> &m, const unsigned int version)
54  {
55  boost::serialization::split_free(ar, m, version);
56  }
57 
58 }
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