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