SST  11.0.0
StructuralSimulationToolkit
serialize_map.h
1 // Copyright 2009-2021 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2021, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SERIALIZE_MAP_H
13 #define SERIALIZE_MAP_H
14 
15 #include <map>
16 #include <unordered_map>
17 #include "sst/core/serialization/serializer.h"
18 
19 namespace SST {
20 namespace Core {
21 namespace Serialization {
22 
23 template <class Key, class Value>
24 class serialize<std::map<Key,Value> > {
25  typedef std::map<Key,Value> Map;
26 
27 public:
28  void operator()(Map& m, serializer& ser){
29  typedef typename std::map<Key,Value>::iterator iterator;
30  switch(ser.mode()) {
31 
32  case serializer::SIZER: {
33  size_t size = m.size();
34  ser.size(size);
35  iterator it, end = m.end();
36  for (it=m.begin(); it != end; ++it){
37  //keys are const values - annoyingly
38  serialize<Key>()(const_cast<Key&>(it->first), ser);
39  serialize<Value>()(it->second, ser);
40  }
41  break;
42  }
43  case serializer::PACK: {
44  size_t size = m.size();
45  ser.pack(size);
46  iterator it, end = m.end();
47  for (it=m.begin(); it != end; ++it){
48  serialize<Key>()(const_cast<Key&>(it->first), ser);
49  serialize<Value>()(it->second, ser);
50  }
51  break;
52  }
53  case serializer::UNPACK: {
54  size_t size;
55  ser.unpack(size);
56  for (size_t i=0; i < size; ++i){
57  Key k = {};
58  Value v = {};
59  serialize<Key>()(k, ser);
60  serialize<Value>()(v, ser);
61  m[k] = v;
62  }
63  break;
64  }
65  }
66  }
67 };
68 
69 template <class Key, class Value>
70 class serialize<std::unordered_map<Key,Value> > {
71  typedef std::unordered_map<Key,Value> Map;
72 
73 public:
74  void operator()(Map& m, serializer& ser){
75  typedef typename std::unordered_map<Key,Value>::iterator iterator;
76  switch(ser.mode()) {
77 
78  case serializer::SIZER: {
79  size_t size = m.size();
80  ser.size(size);
81  iterator it, end = m.end();
82  for (it=m.begin(); it != end; ++it){
83  //keys are const values - annoyingly
84  serialize<Key>()(const_cast<Key&>(it->first), ser);
85  serialize<Value>()(it->second, ser);
86  }
87  break;
88  }
89  case serializer::PACK: {
90  size_t size = m.size();
91  ser.pack(size);
92  iterator it, end = m.end();
93  for (it=m.begin(); it != end; ++it){
94  serialize<Key>()(const_cast<Key&>(it->first), ser);
95  serialize<Value>()(it->second, ser);
96  }
97  break;
98  }
99  case serializer::UNPACK: {
100  size_t size;
101  ser.unpack(size);
102  for (size_t i=0; i < size; ++i){
103  Key k = {};
104  Value v = {};
105  serialize<Key>()(k, ser);
106  serialize<Value>()(v, ser);
107  m[k] = v;
108  }
109  break;
110  }
111  }
112  }
113 };
114 
115 }
116 }
117 }
118 
119 #endif // SERIALIZE_MAP_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Base serialize class.
Definition: serialize.h:33