SST  14.1.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_IMPL_SERIALIZE_MAP_H
13 #define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_MAP_H
14 
15 #ifndef SST_INCLUDING_SERIALIZE_H
16 #warning \
17  "The header file sst/core/serialization/impl/serialize_map.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serialize.h"
18 #endif
19 
20 #include "sst/core/serialization/serializer.h"
21 
22 #include <map>
23 #include <string>
24 #include <unordered_map>
25 
26 namespace SST {
27 namespace Core {
28 namespace Serialization {
29 
30 /**
31  Class used to map std::map
32  */
33 
34 template <class Key, class Value>
35 class serialize_impl<std::map<Key, Value>>
36 {
37  typedef std::map<Key, Value> Map;
38 
39 public:
40  void operator()(Map& m, serializer& ser)
41  {
42  typedef typename std::map<Key, Value>::iterator iterator;
43  switch ( ser.mode() ) {
44 
45  case serializer::SIZER:
46  {
47  size_t size = m.size();
48  ser.size(size);
49  iterator it, end = m.end();
50  for ( it = m.begin(); it != end; ++it ) {
51  // keys are const values - annoyingly
52  ser& const_cast<Key&>(it->first);
53  ser & it->second;
54  }
55  break;
56  }
57  case serializer::PACK:
58  {
59  size_t size = m.size();
60  ser.pack(size);
61  iterator it, end = m.end();
62  for ( it = m.begin(); it != end; ++it ) {
63  ser& const_cast<Key&>(it->first);
64  ser & it->second;
65  }
66  break;
67  }
68  case serializer::UNPACK:
69  {
70  size_t size;
71  ser.unpack(size);
72  for ( size_t i = 0; i < size; ++i ) {
73  Key k = {};
74  Value v = {};
75  ser& k;
76  ser& v;
77  m[k] = v;
78  }
79  break;
80  }
81  case serializer::MAP:
82  // The version of function not called in mapping mode
83  break;
84  }
85  }
86 
87  void operator()(Map& UNUSED(m), serializer& UNUSED(ser), const char* UNUSED(name))
88  {
89  // TODO: Add support for mapping mode
90  }
91 };
92 
93 template <class Key, class Value>
94 class serialize<std::unordered_map<Key, Value>>
95 {
96  typedef std::unordered_map<Key, Value> Map;
97 
98 public:
99  void operator()(Map& m, serializer& ser)
100  {
101  typedef typename std::unordered_map<Key, Value>::iterator iterator;
102  switch ( ser.mode() ) {
103 
104  case serializer::SIZER:
105  {
106  size_t size = m.size();
107  ser.size(size);
108  iterator it, end = m.end();
109  for ( it = m.begin(); it != end; ++it ) {
110  // keys are const values - annoyingly
111  ser& const_cast<Key&>(it->first);
112  ser & it->second;
113  }
114  break;
115  }
116  case serializer::PACK:
117  {
118  size_t size = m.size();
119  ser.pack(size);
120  iterator it, end = m.end();
121  for ( it = m.begin(); it != end; ++it ) {
122  ser& const_cast<Key&>(it->first);
123  ser & it->second;
124  }
125  break;
126  }
127  case serializer::UNPACK:
128  {
129  size_t size;
130  ser.unpack(size);
131  for ( size_t i = 0; i < size; ++i ) {
132  Key k = {};
133  Value v = {};
134  ser& k;
135  ser& v;
136  m[k] = v;
137  }
138  break;
139  }
140  case serializer::MAP:
141  // The version of function not called in mapping mode
142  break;
143  }
144  }
145 
146  void operator()(Map& UNUSED(m), serializer& UNUSED(ser), const char* UNUSED(name))
147  {
148  // TODO: Add support for mapping mode
149  }
150 };
151 
152 } // namespace Serialization
153 } // namespace Core
154 } // namespace SST
155 
156 #endif // SST_CORE_SERIALIZATION_IMPL_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:43
Base serialize class.
Definition: serialize.h:45
Definition: action.cc:18
Serialization "gateway" object.
Definition: serialize.h:133