SST  14.0.0
StructuralSimulationToolkit
serialize_map.h
1 // Copyright 2009-2024 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-2024, 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  ser& const_cast<Key&>(it->first);
43  ser & it->second;
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  ser& const_cast<Key&>(it->first);
54  ser & it->second;
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  ser& k;
66  ser& v;
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  ser& const_cast<Key&>(it->first);
94  ser & it->second;
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  ser& const_cast<Key&>(it->first);
105  ser & it->second;
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  ser& k;
117  ser& v;
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:35
Definition: action.cc:18
Serialization "gateway" object.
Definition: serialize.h:110