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