Sierra Toolkit  Version of the Day
Marshal.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef STK_UTIL_UTIL_MARSHAL_HPP
10 #define STK_UTIL_UTIL_MARSHAL_HPP
11 
12 #include <stdint.h>
13 
14 #include <string>
15 #include <sstream>
16 #include <vector>
17 #include <list>
18 #include <typeinfo>
19 
20 namespace stk_classic {
21 
49 struct Marshal
50 {
55  enum {
56  TYPE_CHECK_NONE = 0x00000000,
57  TYPE_CHECK_POD = 0x00000001,
58  TYPE_CHECK_LIST = 0x00000002,
59  TYPE_CHECK_VECTOR = 0x00000004,
60  TYPE_CHECK_ALL = 0xFFFFFFFF
61  };
62 
67  Marshal(unsigned type_check = TYPE_CHECK_NONE);
68 
75  explicit Marshal(const std::string &s);
76 
83  std::string str() const;
84 
91  size_t size() const;
92 
101  void write(const char *address, size_t byte_count);
102 
111  void read(char *address, size_t byte_count);
112 
119  operator void * () const;
120 
121 private:
122  Marshal(const Marshal &marshal);
123  Marshal &operator=(const Marshal &);
124 
125 public:
126  std::stringstream stream;
127  unsigned m_typeCheck;
128 };
129 
130 
142 template <typename T>
143 Marshal &operator<<(Marshal &mout, const T &t);
144 
155 template <typename T>
156 Marshal &operator>>(Marshal &min, T &t);
157 
170 template<>
171 Marshal &operator<<(Marshal &mout, const std::type_info &t);
172 
186 template<>
187 Marshal &operator>>(Marshal &min, const std::type_info &t);
188 
189 template<>
190 Marshal &operator<<(Marshal &mout, const signed char &t);
191 template<>
192 Marshal &operator<<(Marshal &mout, const unsigned char &t);
193 template<>
194 Marshal &operator<<(Marshal &mout, const char &t);
195 template<>
196 Marshal &operator<<(Marshal &mout, const short &t);
197 template<>
198 Marshal &operator<<(Marshal &mout, const unsigned short &t);
199 template<>
200 Marshal &operator<<(Marshal &mout, const int &t);
201 template<>
202 Marshal &operator<<(Marshal &mout, const unsigned int &t);
203 template<>
204 Marshal &operator<<(Marshal &mout, const long &t);
205 template<>
206 Marshal &operator<<(Marshal &mout, const unsigned long &t);
207 template<>
208 Marshal &operator<<(Marshal &mout, const long long &t);
209 template<>
210 Marshal &operator<<(Marshal &mout, const unsigned long long &t);
211 template<>
212 Marshal &operator<<(Marshal &mout, const float &t);
213 template<>
214 Marshal &operator<<(Marshal &mout, const double &t);
215 template<>
216 Marshal &operator<<(Marshal &mout, const std::string &s);
217 
218 template<>
219 Marshal &operator>>(Marshal &min, signed char &t);
220 template<>
221 Marshal &operator>>(Marshal &min, unsigned char &t);
222 template<>
223 Marshal &operator>>(Marshal &min, char &t);
224 template<>
225 Marshal &operator>>(Marshal &min, short &t);
226 template<>
227 Marshal &operator>>(Marshal &min, unsigned short &t);
228 template<>
229 Marshal &operator>>(Marshal &min, int &t);
230 template<>
231 Marshal &operator>>(Marshal &min, unsigned int &t);
232 template<>
233 Marshal &operator>>(Marshal &min, long &t);
234 template<>
235 Marshal &operator>>(Marshal &min, unsigned long &t);
236 template<>
237 Marshal &operator>>(Marshal &min, long long &t);
238 template<>
239 Marshal &operator>>(Marshal &min, unsigned long long &t);
240 template<>
241 Marshal &operator>>(Marshal &min, float &t);
242 template<>
243 Marshal &operator>>(Marshal &min, double &t);
244 template<>
245 Marshal &operator>>(Marshal &min, std::string &s);
246 
247 
248 template <class T>
249 Marshal &operator<<(Marshal &mout, const std::vector<T> &v) {
250  if (mout.m_typeCheck & Marshal::TYPE_CHECK_VECTOR)
251  mout << typeid(v);
252 
253  size_t size = v.size();
254  mout << size;
255  for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it)
256  mout << (*it);
257 
258  return mout;
259 }
260 
261 template <class T>
262 Marshal &operator>>(Marshal &min, std::vector<T> &v) {
263  if (min.m_typeCheck & Marshal::TYPE_CHECK_VECTOR)
264  min >> typeid(v);
265 
266  size_t size = 0;
267  min >> size;
268  v.reserve(size);
269  for (size_t i = 0; i < size; ++i) {
270  T t;
271  min >> t;
272  v.push_back(t);
273  }
274 
275  return min;
276 }
277 
278 template <class T>
279 Marshal &operator<<(Marshal &mout, const std::list<T> &l) {
280  if (mout.m_typeCheck & Marshal::TYPE_CHECK_LIST)
281  mout << typeid(l);
282 
283  size_t size = l.size();
284  mout << size;
285  for (typename std::list<T>::const_iterator it = l.begin(); it != l.end(); ++it)
286  mout << (*it);
287 
288  return mout;
289 }
290 
291 template <class T>
292 Marshal &operator>>(Marshal &min, std::list<T> &l) {
293  if (min.m_typeCheck & Marshal::TYPE_CHECK_LIST)
294  min >> typeid(l);
295 
296  size_t size;
297  min >> size;
298  for (size_t i = 0; i < size; ++i) {
299  T t;
300  min >> t;
301  l.push_back(t);
302  }
303 
304  return min;
305 }
306 
307 template <class T>
308 Marshal &write(Marshal &mout, const T &t) {
309  mout.write((const char *) &t, sizeof(T));
310 
311  return mout;
312 }
313 
314 template <typename T>
315 Marshal &read(Marshal &min, T &t) {
316  t = T();
317 
318  min.read((char *) &t, sizeof(T));
319  return min;
320 }
321 
322 } // namespace stk_classic
323 
324 #endif // STK_UTIL_UTIL_MARSHAL_HPP
size_t size() const
Member function size returns the byte count of the string of packed bytes creates by put-to operation...
Definition: Marshal.cpp:180
Marshal(unsigned type_check=TYPE_CHECK_NONE)
Definition: Marshal.cpp:153
void write(const char *address, size_t byte_count)
Member function write writer bytes to the packed byte stream.
Definition: Marshal.cpp:193
Struct Marshal is a data packer for sending and receiving parallel messages. The data put-to (<<) is ...
Definition: Marshal.hpp:49
std::string str() const
Member function str returns the string of packed bytes created by put-to operations to the stream...
Definition: Marshal.cpp:173
std::stringstream stream
Packed byte stream to put-to or get-from.
Definition: Marshal.hpp:126
unsigned m_typeCheck
Type checking to activate.
Definition: Marshal.hpp:127
void read(char *address, size_t byte_count)
Member function read reads bytes from the packed byte stream.
Definition: Marshal.cpp:202
Sierra Toolkit.