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