SST  6.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-2016 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
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  packer_.pack(size);
150  packer_.pack_buffer(buffer, size*sizeof(T));
151  break;
152  }
153  case UNPACK: {
154  unpacker_.unpack(size);
155  unpacker_.unpack_buffer(&buffer, size*sizeof(T));
156  break;
157  }
158  }
159  }
160 
161  // For void*, we get sizeof(void), which errors. Specialize for
162  // void*.
163  template <typename Int>
164  void
165  binary(void*& buffer, Int& size){
166  switch (mode_) {
167  case SIZER: {
168  sizer_.add(sizeof(Int));
169  sizer_.add(size);
170  break;
171  }
172  case PACK: {
173  packer_.pack(size);
174  packer_.pack_buffer(buffer, size);
175  break;
176  }
177  case UNPACK: {
178  unpacker_.unpack(size);
179  unpacker_.unpack_buffer(&buffer, size);
180  break;
181  }
182  }
183  }
184 
185  void
186  string(std::string& str);
187 
188  void
189  start_packing(char* buffer, size_t size){
190  packer_.init(buffer, size);
191  mode_ = PACK;
192  }
193 
194  void
195  start_sizing(){
196  sizer_.reset();
197  mode_ = SIZER;
198  }
199 
200  void
201  start_unpacking(char* buffer, size_t size){
202  unpacker_.init(buffer, size);
203  mode_ = UNPACK;
204  }
205 
206  size_t
207  size() const {
208  switch (mode_){
209  case SIZER: return sizer_.size();
210  case PACK: return packer_.size();
211  case UNPACK: return unpacker_.size();
212  }
213  }
214 
215 protected:
216  //only one of these is going to be valid for this spkt_serializer
217  //not very good class design, but a little more convenient
218  pvt::ser_packer packer_;
219  pvt::ser_unpacker unpacker_;
220  pvt::ser_sizer sizer_;
221  SERIALIZE_MODE mode_;
222 
223 };
224 
225 }
226 }
227 }
228 
229 #endif
230 
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Definition: action.cc:17
Definition: serialize_packer.h:23
Definition: serialize_sizer.h:20
Definition: serialize_unpacker.h:22