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