OXYGENHUB
◆ main openmsx / src / serialize_stl.hh
2.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef SERIALIZE_STL_HH
2
#define SERIALIZE_STL_HH
3
4
#include "serialize_core.hh"
5
6
#include "circular_buffer.hh"
7
#include "static_vector.hh"
8
9
#include <deque>
10
#include <iterator>
11
#include <vector>
12
13
namespace openmsx {
14
15
template<typename T> struct serialize_as_stl_collection : std::true_type
16
{
17
	static constexpr int size = -1; // variable size
18
	using value_type = typename T::value_type;
19
	// save
20
	static auto begin(const T& t) { return t.begin(); }
21
	static auto end  (const T& t) { return t.end();   }
22
	// load
23
	static constexpr bool loadInPlace = false;
24
	static void prepare(T& t, int /*n*/) {
25
		t.clear();
26
	}
27
	static auto output(T& t) {
28
		return std::back_inserter(t);
29
	}
30
};
31
32
//template<typename T> struct serialize_as_collection<std::list<T>>
33
//	: serialize_as_stl_collection<std::list<T>> {};
34
35
template<typename T> struct serialize_as_collection<std::deque<T>>
36
	: serialize_as_stl_collection<std::deque<T>> {};
37
38
//template<typename T1, typename T2> struct serialize_as_collection<std::map<T1, T2>>
39
//	: serialize_as_stl_collection<std::map<T1, T2>> {};
40
41
template<typename T> struct serialize_as_collection<std::vector<T>>
42
	: serialize_as_stl_collection<std::vector<T>>
43
{
44
	// Override load-part from base class.
45
	// Don't load vectors in-place, even though it's technically possible
46
	// and slightly more efficient. This is done to keep the correct vector
47
	// size at all intermediate steps. This may be important in case an
48
	// exception occurs during loading.
49
	static constexpr bool loadInPlace = false;
50
	static void prepare(std::vector<T>& v, int n) {
51
		v.clear(); v.reserve(n);
52
	}
53
	static auto output(std::vector<T>& v) {
54
		return std::back_inserter(v);
55
	}
56
};
57
58
template<typename T> struct serialize_as_collection<cb_queue<T>>
59
	: serialize_as_stl_collection<cb_queue<T>>
60
{
61
	static void prepare(cb_queue<T>& q, int n) {
62
		q.clear(); q.getBuffer().set_capacity(n);
63
	}
64
	static auto output(cb_queue<T>& q) {
65
		return std::back_inserter(q.getBuffer());
66
	}
67
};
68
69
template<typename T, size_t N> struct serialize_as_collection<static_vector<T, N>>
70
	: serialize_as_stl_collection<static_vector<T, N>> {};
71
72
} // namespace openmsx
73
74
#endif