SST  14.1.0
StructuralSimulationToolkit
serialize_vector.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_VECTOR_H
13 #define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_VECTOR_H
14 
15 #ifndef SST_INCLUDING_SERIALIZE_H
16 #warning \
17  "The header file sst/core/serialization/impl/serialize_vector.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 <string>
23 #include <vector>
24 
25 namespace SST {
26 namespace Core {
27 namespace Serialization {
28 
29 
30 /**
31  Class used to map std::vectors.
32  */
33 template <class T>
35 {
36 protected:
37  std::vector<T>* addr_;
38 
39 public:
40  bool isContainer() override { return true; }
41 
42  std::string getType() override { return demangle_name(typeid(std::vector<T>).name()); }
43 
44  void* getAddr() override { return addr_; }
45 
46  ObjectMapVector(std::vector<T>* addr) : ObjectMapWithChildren(), addr_(addr) {}
47 
48  ~ObjectMapVector() {}
49 };
50 
51 
52 template <class T>
53 class serialize_impl<std::vector<T>>
54 {
55  template <class A>
56  friend class serialize;
57 
58  typedef std::vector<T> Vector;
59 
60  void operator()(Vector& v, serializer& ser)
61  {
62  switch ( ser.mode() ) {
63  case serializer::SIZER:
64  {
65  size_t size = v.size();
66  ser.size(size);
67  break;
68  }
69  case serializer::PACK:
70  {
71  size_t size = v.size();
72  ser.pack(size);
73  break;
74  }
75  case serializer::UNPACK:
76  {
77  size_t s;
78  ser.unpack(s);
79  v.resize(s);
80  break;
81  }
82  case serializer::MAP:
83  // If this version of operator() is called during mapping
84  // mode, then the variable being mapped did not provide a
85  // name, which means no ObjectMap will be created.
86  break;
87  }
88 
89  for ( size_t i = 0; i < v.size(); ++i ) {
90  ser& v[i];
91  }
92  }
93 
94  void operator()(Vector& v, serializer& ser, const char* name)
95  {
96  if ( ser.mode() != serializer::MAP ) return operator()(v, ser);
97 
98  ObjectMapVector<T>* obj_map = new ObjectMapVector<T>(&v);
99  ser.mapper().map_hierarchy_start(name, obj_map);
100  for ( size_t i = 0; i < v.size(); ++i ) {
101  sst_map_object(ser, v[i], std::to_string(i).c_str());
102  }
103  ser.mapper().map_hierarchy_end();
104  }
105 };
106 
107 template <>
108 class serialize_impl<std::vector<bool>>
109 {
110  template <class A>
111  friend class serialize;
112 
113  typedef std::vector<bool> Vector;
114 
115  void operator()(Vector& v, serializer& ser)
116  {
117  switch ( ser.mode() ) {
118  case serializer::SIZER:
119  {
120  size_t size = v.size();
121  ser.size(size);
122  bool val;
123  for ( auto it = v.begin(); it != v.end(); it++ ) {
124  val = *it;
125  ser& val;
126  }
127  break;
128  }
129  case serializer::PACK:
130  {
131  size_t size = v.size();
132  ser.pack(size);
133  for ( auto it = v.begin(); it != v.end(); it++ ) {
134  bool val = *it;
135  ser& val;
136  }
137  break;
138  }
139  case serializer::UNPACK:
140  {
141  size_t s;
142  ser.unpack(s);
143  v.resize(s);
144  for ( size_t i = 0; i < v.size(); i++ ) {
145  bool val = false;
146  ser& val;
147  v[i] = val;
148  }
149  break;
150  }
151  case serializer::MAP:
152  // If this version of operator() is called during mapping
153  // mode, then the variable being mapped did not provide a
154  // name, which means no ObjectMap will be created.
155  break;
156  }
157  }
158 
159  // TODO: Add support for mapping vector<bool>. The weird way they
160  // pack the bits means we'll likely need to have a special case of
161  // ObjectMapVector<bool> that knows how to handle the packing.
162 };
163 
164 
165 } // namespace Serialization
166 } // namespace Core
167 } // namespace SST
168 
169 #endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_VECTOR_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:43
Class used to map std::vectors.
Definition: serialize_vector.h:34
Base serialize class.
Definition: serialize.h:45
Definition: action.cc:18
Serialization "gateway" object.
Definition: serialize.h:133
ObjectMap object for non-fundamental, non-container types.
Definition: objectMap.h:377
static std::string demangle_name(const char *name)
Static function to demangle type names returned from typeid<T>.name()
Definition: objectMap.cc:139
bool isContainer() override
Check to see if this ObjectMap represents a container.
Definition: serialize_vector.h:40
std::string getType() override
Get the type of the variable represented by the ObjectMap.
Definition: serialize_vector.h:42
void * getAddr() override
Get the address of the variable represented by the ObjectMap.
Definition: serialize_vector.h:44