/
map.hpp
126 lines (107 loc) · 4.06 KB
/
map.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2015 Anton Bikineev
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef HPX_SERIALIZATION_MAP_HPP
#define HPX_SERIALIZATION_MAP_HPP
#include <hpx/config.hpp>
#include <hpx/runtime/serialization/input_archive.hpp>
#include <hpx/runtime/serialization/output_archive.hpp>
#include <hpx/traits/is_bitwise_serializable.hpp>
#include <map>
#include <boost/mpl/and.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/add_reference.hpp>
namespace hpx
{
namespace traits
{
template <class Key, class Value>
struct is_bitwise_serializable<std::pair<Key, Value> >:
boost::mpl::and_<
is_bitwise_serializable<Key>,
is_bitwise_serializable<Value>
> {};
}
namespace serialization
{
namespace detail
{
template <class Key, class Value>
void load_pair_impl(input_archive& ar, std::pair<Key, Value>& t,
boost::mpl::false_)
{
ar >> const_cast<
typename boost::add_reference<
typename boost::remove_const<Key>::type
>::type>(t.first);
ar >> t.second;
}
template <class Key, class Value>
void load_pair_impl(input_archive& ar, std::pair<Key, Value>& t,
boost::mpl::true_)
{
if (ar.disable_array_optimization())
load_pair_impl(ar, t, boost::mpl::false_());
else
load_binary(ar, &t, sizeof(std::pair<Key, Value>));
}
template <class Key, class Value>
void save_pair_impl(output_archive& ar, const std::pair<Key, Value>& t,
boost::mpl::false_)
{
ar << t.first;
ar << t.second;
}
template <class Key, class Value>
void save_pair_impl(output_archive& ar, const std::pair<Key, Value>& t,
boost::mpl::true_)
{
if (ar.disable_array_optimization())
save_pair_impl(ar, t, boost::mpl::false_());
else
save_binary(ar, &t, sizeof(std::pair<Key, Value>));
}
} // namespace detail
template <class Key, class Value>
void serialize(input_archive& ar, std::pair<Key, Value>& t, unsigned)
{
typedef std::pair<Key, Value> pair_type;
typedef typename traits::is_bitwise_serializable<pair_type> optimized;
detail::load_pair_impl(ar, t, optimized());
}
template <class Key, class Value>
void serialize(output_archive& ar, const std::pair<Key, Value>& t, unsigned)
{
typedef std::pair<Key, Value> pair_type;
typedef typename traits::is_bitwise_serializable<pair_type> optimized;
detail::save_pair_impl(ar, t, optimized());
}
template <class Key, class Value, class Comp, class Alloc>
void serialize(input_archive& ar, std::map<Key, Value, Comp, Alloc>& t, unsigned)
{
typedef typename std::map<Key, Value, Comp, Alloc>::size_type size_type;
typedef typename std::map<Key, Value, Comp, Alloc>::value_type value_type;
size_type size;
ar >> size; //-V128
for (size_type i = 0; i < size; ++i)
{
value_type v;
ar >> v;
t.insert(t.end(), std::move(v));
}
}
template <class Key, class Value, class Comp, class Alloc>
void serialize(output_archive& ar,
const std::map<Key, Value, Comp, Alloc>& t, unsigned)
{
typedef typename std::map<Key, Value, Comp, Alloc>::value_type value_type;
ar << t.size(); //-V128
for(const value_type& val : t)
{
ar << val;
}
}
}
}
#endif