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