SST  10.0.0
StructuralSimulationToolkit
serializer.h
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009-2020 NTESS.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-NA0003525 with NTESS,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SPROCKIT_COMMON_MESSAGES_spkt_serializer_H_INCLUDED
13 #define SPROCKIT_COMMON_MESSAGES_spkt_serializer_H_INCLUDED
14 
15 //#include <sprockit/spkt_config.h>
16 #include "sst/core/serialization/serialize_packer.h"
17 #include "sst/core/serialization/serialize_sizer.h"
18 #include "sst/core/serialization/serialize_unpacker.h"
19 #include <typeinfo>
20 
21 #include <cstring>
22 #include <list>
23 #include <vector>
24 #include <map>
25 #include <set>
26 
27 namespace SST {
28 namespace Core {
29 namespace Serialization {
30 
31 /**
32  * This class is basically a wrapper for objects to declare the order in
33  * which their members should be ser/des
34  */
36 {
37 public:
38  typedef enum {
39  SIZER, PACK, UNPACK
40  } SERIALIZE_MODE;
41 
42 public:
43  serializer() :
44  mode_(SIZER) //just sizing by default
45  {
46  }
47 
49  packer() {
50  return packer_;
51  }
52 
54  unpacker() {
55  return unpacker_;
56  }
57 
59  sizer() {
60  return sizer_;
61  }
62 
63  template <class T>
64  void
65  size(T& t){
66  sizer_.size<T>(t);
67  }
68 
69  template <class T>
70  void
71  pack(T& t){
72  packer_.pack<T>(t);
73  }
74 
75  template <class T>
76  void
77  unpack(T& t){
78  unpacker_.unpack<T>(t);
79  }
80 
81  virtual
82  ~serializer() {
83  }
84 
85  SERIALIZE_MODE
86  mode() const {
87  return mode_;
88  }
89 
90  void
91  set_mode(SERIALIZE_MODE mode) {
92  mode_ = mode;
93  }
94 
95  void
96  reset(){
97  sizer_.reset();
98  packer_.reset();
99  unpacker_.reset();
100  }
101 
102  template<typename T>
103  void
104  primitive(T &t) {
105  switch(mode_) {
106  case SIZER:
107  sizer_.size(t);
108  break;
109  case PACK:
110  packer_.pack(t);
111  break;
112  case UNPACK:
113  unpacker_.unpack(t);
114  break;
115  }
116  }
117 
118  template <class T, int N>
119  void
120  array(T arr[N]){
121  switch (mode_) {
122  case SIZER: {
123  sizer_.add(sizeof(T) * N);
124  break;
125  }
126  case PACK: {
127  char* charstr = packer_.next_str(N*sizeof(T));
128  ::memcpy(charstr, arr, N*sizeof(T));
129  break;
130  }
131  case UNPACK: {
132  char* charstr = unpacker_.next_str(N*sizeof(T));
133  ::memcpy(arr, charstr, N*sizeof(T));
134  break;
135  }
136  }
137  }
138 
139  template <typename T, typename Int>
140  void
141  binary(T*& buffer, Int& size){
142  switch (mode_) {
143  case SIZER: {
144  sizer_.add(sizeof(Int));
145  sizer_.add(size);
146  break;
147  }
148  case PACK: {
149  if (buffer){
150  packer_.pack(size);
151  packer_.pack_buffer(buffer, size*sizeof(T));
152  } else {
153  Int nullsize = 0;
154  packer_.pack(nullsize);
155  }
156  break;
157  }
158  case UNPACK: {
159  unpacker_.unpack(size);
160  if (size != 0){
161  unpacker_.unpack_buffer(&buffer, size*sizeof(T));
162  } else {
163  buffer = nullptr;
164  }
165  break;
166  }
167  }
168  }
169 
170  // For void*, we get sizeof(void), which errors.
171  // Create a wrapper that casts to char* and uses above
172  template <typename Int>
173  void
174  binary(void*& buffer, Int& size){
175  char* tmp = (char*) buffer;
176  binary<char>(tmp, size);
177  buffer = tmp;
178  }
179 
180  void
181  string(std::string& str);
182 
183  void
184  start_packing(char* buffer, size_t size){
185  packer_.init(buffer, size);
186  mode_ = PACK;
187  }
188 
189  void
190  start_sizing(){
191  sizer_.reset();
192  mode_ = SIZER;
193  }
194 
195  void
196  start_unpacking(char* buffer, size_t size){
197  unpacker_.init(buffer, size);
198  mode_ = UNPACK;
199  }
200 
201  size_t
202  size() const {
203  switch (mode_){
204  case SIZER: return sizer_.size();
205  case PACK: return packer_.size();
206  case UNPACK: return unpacker_.size();
207  }
208  return 0;
209  }
210 
211 protected:
212  //only one of these is going to be valid for this spkt_serializer
213  //not very good class design, but a little more convenient
214  pvt::ser_packer packer_;
215  pvt::ser_unpacker unpacker_;
216  pvt::ser_sizer sizer_;
217  SERIALIZE_MODE mode_;
218 
219 };
220 
221 }
222 }
223 }
224 
225 #endif
226 
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
void unpack_buffer(void *buf, int size)
unpack_buffer
Definition: serializer.cc:23
Definition: serialize_packer.h:23
void pack_buffer(void *buf, int size)
pack_buffer
Definition: serializer.cc:38
Definition: serialize_sizer.h:21
Definition: serialize_unpacker.h:22