-
Notifications
You must be signed in to change notification settings - Fork 3
/
boost_serialize_vector_uniqueptr.h
68 lines (54 loc) · 2.07 KB
/
boost_serialize_vector_uniqueptr.h
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* boost_serialize_vector_uniqueptr.h
*
* Created on: Apr 16, 2013
* Author: gerlach
*/
#ifndef BOOST_SERIALIZE_VECTOR_UNIQUEPTR_H_
#define BOOST_SERIALIZE_VECTOR_UNIQUEPTR_H_
//Boost serialization for std::vector<std::unique_ptr<T>>, based on
// http://stackoverflow.com/questions/13347776/boost-serialization-of-stl-collection-of-std-unique-ptrs
//NOTE: do not include boost/serialization/vector.hpp //is this note still relevant?
#include <memory>
#include <vector>
#include <boost/serialization/nvp.hpp>
namespace boost { namespace serialization {
template<class Archive, class T, class Allocator>
inline void save(Archive& ar,
const std::vector<std::unique_ptr<T>, Allocator>& vec,
const uint32_t /*version*/) {
collection_size_type count = vec.size();
ar << BOOST_SERIALIZATION_NVP(count);
for (auto it = vec.begin(), end = vec.end(); it != end; ++it)
ar << boost::serialization::make_nvp("item", (*it));
}
template<class Archive, class T, class Allocator>
inline void load(Archive& ar,
std::vector<std::unique_ptr<T>, Allocator>& vec,
const uint32_t /*version*/) {
collection_size_type count;
ar >> BOOST_SERIALIZATION_NVP(count);
vec.clear();
vec.reserve(count);
while( count-- > 0 ) {
std::unique_ptr<T> i;
ar >> boost::serialization::make_nvp("item", i);
vec.push_back(std::move(i)); // use std::move
}
}
template<class Archive, class T, class Allocator>
inline void serialize(Archive& ar,
std::vector<std::unique_ptr<T>, Allocator>& t,
const uint32_t file_version) {
boost::serialization::split_free(ar, t, file_version);
}
} } // namespace boost::serialization
#endif /* BOOST_SERIALIZE_VECTOR_UNIQUEPTR_H_ */