SST  6.0.0
StructuralSimulationToolkit
serialize.h
1 // Copyright 2009-2016 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2016, Sandia Corporation
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 SERIALIZE_H
13 #define SERIALIZE_H
14 
15 #include <sst/core/serialization/serializer.h>
16 //#include <sprockit/debug.h>
17 //DeclareDebugSlot(serialize);
18 
19 #include <iostream>
20 #include <typeinfo>
21 
22 namespace SST {
23 namespace Core {
24 namespace Serialization {
25 
26 /**
27  Base serialize class. This is the default, which if hit will
28  static_assert. All other instances are partial specializations of
29  this class and do all the real serialization.
30  */
31 template <class T, class Enable = void>
32 class serialize {
33 public:
34  inline void operator()(T& t, serializer& ser){
35  // If the default gets called, then it's actually invalid
36  // because we don't know how to serialize it.
37 
38  // This is a bit strange, but if I just do a
39  // static_assert(false) it always triggers, but if I use
40  // std::is_* then it seems to only trigger if something expands
41  // to this version of the template.
42  // static_assert(false,"Trying to serialize an object that is not serializable.");
43  static_assert(std::is_fundamental<T>::value,"Trying to serialize an object that is not serializable.");
44  static_assert(!std::is_fundamental<T>::value,"Trying to serialize an object that is not serializable.");
45  }
46 };
47 
48 /**
49  Version of serialize that works for fundamental types and enums.
50  */
51 template <class T>
52 class serialize <T, typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type> {
53 public:
54  inline void operator()(T& t, serializer& ser){
55  ser.primitive(t);
56  }
57 };
58 
59 // template <class U, class V>
60 // class serialize<std::pair<U,V>,
61 // typename std::enable_if<(std::is_fundamental<U>::value || std::is_enum<U>::value) &&
62 // (std::is_fundamental<V>::value || std::is_enum<V>::value)>::type> {
63 // public:
64 // inline void operator()(std::pair<U,V>& t, serializer& ser){
65 // ser.primitive(t);
66 // }
67 // };
68 
69 
70 /**
71  Version of serialize that works for bool.
72  */
73 template <>
74 class serialize<bool> {
75 public:
76  void operator()(bool &t, serializer& ser){
77  int bval = t;
78  ser.primitive(bval);
79  t = bool(bval);
80  }
81 };
82 
83 /**
84  Version of serialize that works for pointers to fundamental types
85  and enums. Note that there is no pointer tracking. This only
86  copies the value pointed to into the buffer. If multiple objects
87  point to the same location, they will each have an independant copy
88  after deserialization.
89  */
90 template <class T>
91 class serialize<T*, typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value>::type> {
92 public:
93  inline void operator()(T*& t, serializer& ser){
94  switch (ser.mode()){
95  case serializer::SIZER:
96  ser.size(*t);
97  break;
98  case serializer::PACK:
99  ser.primitive(*t);
100  break;
101  case serializer::UNPACK:
102  t = new T();
103  ser.primitive(*t);
104  break;
105  }
106  }
107 };
108 
109 /**
110  Version of serialize that works for std::pair.
111  */
112 template <class U, class V>
113 class serialize<std::pair<U,V> > {
114 public:
115  inline void operator()(std::pair<U,V>& t, serializer& ser){
116  serialize<U>()(t.first,ser);
117  serialize<V>()(t.second,ser);
118  }
119 };
120 
121 template <class T>
122 inline void
123 operator&(serializer& ser, T& t){
124  serialize<T>()(t, ser);
125 }
126 
127 }
128 }
129 }
130 
131 #include <sst/core/serialization/serialize_array.h>
132 #include <sst/core/serialization/serialize_deque.h>
133 #include <sst/core/serialization/serialize_list.h>
134 #include <sst/core/serialization/serialize_map.h>
135 #include <sst/core/serialization/serialize_set.h>
136 #include <sst/core/serialization/serialize_vector.h>
137 #include <sst/core/serialization/serialize_string.h>
138 
139 #endif // SERIALIZE_H
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
Base serialize class.
Definition: serialize.h:32