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 <deque>
7#include <iterator>
8#include <vector>
9
10namespace openmsx {
11
12template<typename T> struct serialize_as_stl_collection : std::true_type
13{
14 static constexpr int size = -1; // variable size
15 using value_type = typename T::value_type;
16 // save
17 static auto begin(const T& t) { return t.begin(); }
18 static auto end (const T& t) { return t.end(); }
19 // load
20 static constexpr bool loadInPlace = false;
21 static void prepare(T& t, int /*n*/) {
22 t.clear();
23 }
24 static auto output(T& t) {
25 return std::back_inserter(t);
26 }
27};
28
29//template<typename T> struct serialize_as_collection<std::list<T>>
30// : serialize_as_stl_collection<std::list<T>> {};
31
32template<typename T> struct serialize_as_collection<std::deque<T>>
33 : serialize_as_stl_collection<std::deque<T>> {};
34
35//template<typename T1, typename T2> struct serialize_as_collection<std::map<T1, T2>>
36// : serialize_as_stl_collection<std::map<T1, T2>> {};
37
38template<typename T> struct serialize_as_collection<std::vector<T>>
39 : serialize_as_stl_collection<std::vector<T>>
40{
41 // Override load-part from base class.
42 // Don't load vectors in-place, even though it's technically possible
43 // and slightly more efficient. This is done to keep the correct vector
44 // size at all intermediate steps. This may be important in case an
45 // exception occurs during loading.
46 static constexpr bool loadInPlace = false;
47 static void prepare(std::vector<T>& v, int n) {
48 v.clear(); v.reserve(n);
49 }
50 static auto output(std::vector<T>& v) {
51 return std::back_inserter(v);
52 }
53};
54
55template<typename T> struct serialize_as_collection<cb_queue<T>>
56 : serialize_as_stl_collection<cb_queue<T>>
57{
58 static void prepare(cb_queue<T>& q, int n) {
59 q.clear(); q.getBuffer().set_capacity(n);
60 }
61 static auto output(cb_queue<T>& q) {
62 return std::back_inserter(q.getBuffer());
63 }
64};
65
66} // namespace openmsx
67
68#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:9
STL namespace.
static void prepare(cb_queue< T > &q, int n)
static void prepare(std::vector< T > &v, int n)