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