SST  12.0.1
StructuralSimulationToolkit
serialize_map.h
1 // Copyright 2009-2022 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-2022, 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 SST_CORE_SERIALIZATION_SERIALIZE_MAP_H
13 #define SST_CORE_SERIALIZATION_SERIALIZE_MAP_H
14 
15 #include "sst/core/serialization/serializer.h"
16 
17 #include <map>
18 #include <unordered_map>
19 
20 namespace SST {
21 namespace Core {
22 namespace Serialization {
23 
24 template <class Key, class Value>
25 class serialize<std::map<Key, Value>>
26 {
27  typedef std::map<Key, Value> Map;
28 
29 public:
30  void operator()(Map& m, serializer& ser)
31  {
32  typedef typename std::map<Key, Value>::iterator iterator;
33  switch ( ser.mode() ) {
34 
35  case serializer::SIZER:
36  {
37  size_t size = m.size();
38  ser.size(size);
39  iterator it, end = m.end();
40  for ( it = m.begin(); it != end; ++it ) {
41  // keys are const values - annoyingly
42  serialize<Key>()(const_cast<Key&>(it->first), ser);
43  serialize<Value>()(it->second, ser);
44  }
45  break;
46  }
47  case serializer::PACK:
48  {
49  size_t size = m.size();
50  ser.pack(size);
51  iterator it, end = m.end();
52  for ( it = m.begin(); it != end; ++it ) {
53  serialize<Key>()(const_cast<Key&>(it->first), ser);
54  serialize<Value>()(it->second, ser);
55  }
56  break;
57  }
58  case serializer::UNPACK:
59  {
60  size_t size;
61  ser.unpack(size);
62  for ( size_t i = 0; i < size; ++i ) {
63  Key k = {};
64  Value v = {};
65  serialize<Key>()(k, ser);
66  serialize<Value>()(v, ser);
67  m[k] = v;
68  }
69  break;
70  }
71  }
72  }
73 };
74 
75 template <class Key, class Value>
76 class serialize<std::unordered_map<Key, Value>>
77 {
78  typedef std::unordered_map<Key, Value> Map;
79 
80 public:
81  void operator()(Map& m, serializer& ser)
82  {
83  typedef typename std::unordered_map<Key, Value>::iterator iterator;
84  switch ( ser.mode() ) {
85 
86  case serializer::SIZER:
87  {
88  size_t size = m.size();
89  ser.size(size);
90  iterator it, end = m.end();
91  for ( it = m.begin(); it != end; ++it ) {
92  // keys are const values - annoyingly
93  serialize<Key>()(const_cast<Key&>(it->first), ser);
94  serialize<Value>()(it->second, ser);
95  }
96  break;
97  }
98  case serializer::PACK:
99  {
100  size_t size = m.size();
101  ser.pack(size);
102  iterator it, end = m.end();
103  for ( it = m.begin(); it != end; ++it ) {
104  serialize<Key>()(const_cast<Key&>(it->first), ser);
105  serialize<Value>()(it->second, ser);
106  }
107  break;
108  }
109  case serializer::UNPACK:
110  {
111  size_t size;
112  ser.unpack(size);
113  for ( size_t i = 0; i < size; ++i ) {
114  Key k = {};
115  Value v = {};
116  serialize<Key>()(k, ser);
117  serialize<Value>()(v, ser);
118  m[k] = v;
119  }
120  break;
121  }
122  }
123  }
124 };
125 
126 } // namespace Serialization
127 } // namespace Core
128 } // namespace SST
129 
130 #endif // SST_CORE_SERIALIZATION_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:34
Base serialize class.
Definition: serialize.h:31