SST/macro
serializer.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 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>
19 #include <typeinfo>
20 
21 #include <cstring>
22 #include <list>
23 #include <vector>
24 #include <map>
25 #include <set>
26 
27 namespace sprockit {
28 
29 /**
30  * This class is basically a wrapper for objects to declare the order in
31  * which their members should be ser/des
32  */
34 {
35  public:
36  typedef enum {
39 
40  public:
42  mode_(SIZER) //just sizing by default
43  {
44  }
45 
47  packer() {
48  return packer_;
49  }
50 
53  return unpacker_;
54  }
55 
57  sizer() {
58  return sizer_;
59  }
60 
61  template <class T>
62  void
63  size(T& t){
64  sizer_.size<T>(t);
65  }
66 
67  template <class T>
68  void
69  pack(T& t){
70  packer_.pack<T>(t);
71  }
72 
73  template <class T>
74  void
75  unpack(T& t){
76  unpacker_.unpack<T>(t);
77  }
78 
79  virtual
81  }
82 
83  SERIALIZE_MODE
84  mode() const {
85  return mode_;
86  }
87 
88  void
89  set_mode(SERIALIZE_MODE mode) {
90  mode_ = mode;
91  }
92 
93  void
94  reset(){
95  sizer_.reset();
96  packer_.reset();
97  unpacker_.reset();
98  }
99 
100  template<typename T>
101  void
102  primitive(T &t) {
103  switch(mode_)
104  {
105  case SIZER:
106  sizer_.size(t);
107  break;
108  case PACK:
109  packer_.pack(t);
110  break;
111  case UNPACK:
112  unpacker_.unpack(t);
113  break;
114  }
115  }
116 
117  template <class T, int N>
118  void
119  array(T arr[N]){
120  switch (mode_)
121  {
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  {
144  case SIZER: {
145  sizer_.add(sizeof(Int));
146  sizer_.add(size);
147  break;
148  }
149  case PACK: {
150  if (buffer && size){
151  pack(size);
152  packer_.pack_buffer(buffer, size*sizeof(T));
153  } else {
154  Int sz(0);
155  pack(sz);
156  }
157  break;
158  }
159  case UNPACK: {
160  unpacker_.unpack(size);
161  unpacker_.unpack_buffer(&buffer, size*sizeof(T));
162  break;
163  }
164  }
165  }
166 
167  template <typename Int>
168  void
169  binary(void*& buffer, Int& size){
170  switch (mode_)
171  {
172  case SIZER: {
173  sizer_.add(sizeof(Int));
174  sizer_.add(size);
175  break;
176  }
177  case PACK: {
178  if (buffer && size){
179  pack(size);
180  packer_.pack_buffer(buffer, size);
181  } else {
182  Int sz(0);
183  pack(sz);
184  }
185  break;
186  }
187  case UNPACK: {
188  unpacker_.unpack(size);
189  unpacker_.unpack_buffer(&buffer, size);
190  break;
191  }
192  }
193  }
194 
195 
196  void
197  string(std::string& str);
198 
199  void
200  start_packing(char* buffer, size_t size){
201  packer_.init(buffer, size);
202  mode_ = PACK;
203  }
204 
205  void
207  sizer_.reset();
208  mode_ = SIZER;
209  }
210 
211  void
212  start_unpacking(char* buffer, size_t size){
213  unpacker_.init(buffer, size);
214  mode_ = UNPACK;
215  }
216 
217  size_t
218  size() const {
219  switch (mode_){
220  case SIZER: return sizer_.size();
221  case PACK: return packer_.size();
222  case UNPACK: return unpacker_.size();
223  }
224  }
225 
226  protected:
227  //only one of these is going to be valid for this spkt_serializer
228  //not very good class design, but a little more convenient
232  SERIALIZE_MODE mode_;
233 
234 };
235 
236 } // end of namespace sprockit
237 #endif
238 
void start_packing(char *buffer, size_t size)
Definition: serializer.h:200
void pack_buffer(void *buf, int size)
pvt::ser_sizer sizer_
Definition: serializer.h:231
void array(T arr[N])
Definition: serializer.h:119
pvt::ser_unpacker unpacker_
Definition: serializer.h:230
void * memcpy(const VariablePtr< T > &dst, const VariablePtr< T > &src, size_t size)
Definition: variable.h:381
void unpack(T &t)
Definition: serializer.h:75
pvt::ser_packer packer_
Definition: serializer.h:229
size_t size() const
Definition: serializer.h:218
void start_unpacking(char *buffer, size_t size)
Definition: serializer.h:212
pvt::ser_unpacker & unpacker()
Definition: serializer.h:52
pvt::ser_packer & packer()
Definition: serializer.h:47
void init(void *buffer, size_t size)
void string(std::string &str)
void unpack_buffer(void *buf, int size)
SERIALIZE_MODE mode() const
Definition: serializer.h:84
void set_mode(SERIALIZE_MODE mode)
Definition: serializer.h:89
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:33
void binary(T *&buffer, Int &size)
Definition: serializer.h:141
pvt::ser_sizer & sizer()
Definition: serializer.h:57
SERIALIZE_MODE mode_
Definition: serializer.h:232
void binary(void *&buffer, Int &size)
Definition: serializer.h:169
virtual ~serializer()
Definition: serializer.h:80
void primitive(T &t)
Definition: serializer.h:102