SST/macro
serialize_map.h
Go to the documentation of this file.
1 #ifndef SERIALIZE_MAP_H
2 #define SERIALIZE_MAP_H
3 
4 #include <map>
5 #include <sprockit/unordered.h>
6 #include <sprockit/serializer.h>
7 
8 namespace sprockit {
9 
10 namespace pvt {
11 
12 template <class Map, class Key, class Value>
13 void
15 {
16  typedef typename Map::iterator iterator;
17  switch(ser.mode())
18  {
19  case serializer::SIZER: {
20  size_t size = m.size();
21  ser.size(size);
22  iterator it, end = m.end();
23  for (it=m.begin(); it != end; ++it){
24  //keys are const values - annoyingly
25  serialize<Key>()(const_cast<Key&>(it->first), ser);
26  serialize<Value>()(it->second, ser);
27  }
28  break;
29  }
30  case serializer::PACK: {
31  size_t size = m.size();
32  ser.pack(size);
33  iterator it, end = m.end();
34  for (it=m.begin(); it != end; ++it){
35  serialize<Key>()(const_cast<Key&>(it->first), ser);
36  serialize<Value>()(it->second, ser);
37  }
38  break;
39  }
40  case serializer::UNPACK: {
41  size_t size;
42  ser.unpack(size);
43  for (int i=0; i < size; ++i){
44  Key k;
45  Value v;
46  serialize<Key>()(k, ser);
47  serialize<Value>()(v, ser);
48  m[k] = v;
49  }
50  break;
51  }
52  }
53 }
54 
55 } //end ns private
56 
57 template <class Key, class Value>
58 class serialize<std::map<Key,Value> > {
59  typedef std::map<Key,Value> Map;
60  public:
61  void operator()(Map& m, serializer& ser){
62  pvt::serialize_map<Map,Key,Value>(m,ser);
63  }
64 };
65 
66 #if !SPKT_ENABLE_ORDERED_MAP
67 template <class Key, class Value>
68 class serialize<spkt_unordered_map<Key,Value> > {
69  typedef spkt_unordered_map<Key,Value> Map;
70  public:
71  void operator()(Map& m, serializer& ser){
72  pvt::serialize_map<Map,Key,Value>(m,ser);
73  }
74 };
75 #endif
76 
77 }
78 
79 #endif // SERIALIZE_MAP_H
void unpack(T &t)
Definition: serializer.h:75
void operator()(Map &m, serializer &ser)
Definition: serialize_map.h:61
SERIALIZE_MODE mode() const
Definition: serializer.h:84
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:33
void serialize_map(Map &m, serializer &ser)
Definition: serialize_map.h:14