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
cb_queue::getBuffer
auto & getBuffer()
Definition: circular_buffer.hh:388
cb_queue::clear
void clear()
Definition: circular_buffer.hh:386
openmsx::serialize_as_stl_collection::size
static constexpr int size
Definition: serialize_stl.hh:13
t
TclObject t
Definition: TclObject_test.cc:264
openmsx::serialize_as_stl_collection::loadInPlace
static constexpr bool loadInPlace
Definition: serialize_stl.hh:19
openmsx::serialize_as_collection< cb_queue< T > >::prepare
static void prepare(cb_queue< T > &q, int n)
Definition: serialize_stl.hh:57
openmsx::serialize_as_stl_collection::end
static auto end(const T &t)
Definition: serialize_stl.hh:17
serialize_core.hh
cb_queue
This implements a queue on top of circular_buffer (not part of boost).
Definition: circular_buffer.hh:345
openmsx::serialize_as_collection< std::vector< T > >::output
static auto output(std::vector< T > &v)
Definition: serialize_stl.hh:49
openmsx::serialize_as_stl_collection< std::vector< T > >::value_type
typename T::value_type value_type
Definition: serialize_stl.hh:14
openmsx::serialize_as_stl_collection::prepare
static void prepare(T &t, int)
Definition: serialize_stl.hh:20
openmsx::serialize_as_collection< std::vector< T > >::prepare
static void prepare(std::vector< T > &v, int n)
Definition: serialize_stl.hh:46
openmsx::serialize_as_stl_collection::output
static auto output(T &t)
Definition: serialize_stl.hh:23
openmsx::serialize_as_collection< cb_queue< T > >::output
static auto output(cb_queue< T > &q)
Definition: serialize_stl.hh:60
openmsx::serialize_as_stl_collection
Definition: serialize_stl.hh:12
openmsx::serialize_as_stl_collection::begin
static auto begin(const T &t)
Definition: serialize_stl.hh:16
circular_buffer.hh
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::serialize_as_collection
Definition: serialize_core.hh:235