SST  11.1.0
StructuralSimulationToolkit
serialize.h
1 // Copyright 2009-2021 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-2021, 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 SST_CORE_SERIALIZATION_SERIALIZE_H
13 #define SST_CORE_SERIALIZATION_SERIALIZE_H
14 
15 #include "sst/core/serialization/serializer.h"
16 #include "sst/core/warnmacros.h"
17 
18 #include <iostream>
19 #include <typeinfo>
20 
21 namespace SST {
22 namespace Core {
23 namespace Serialization {
24 
25 /**
26  Base serialize class. This is the default, which if hit will
27  static_assert. All other instances are partial specializations of
28  this class and do all the real serialization.
29  */
30 template <class T, class Enable = void>
31 class serialize
32 {
33 public:
34  inline void operator()(T& UNUSED(t), serializer& UNUSED(ser))
35  {
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 {
55 public:
56  inline void operator()(T& t, serializer& ser) { ser.primitive(t); }
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  Version of serialize that works for bool.
71  */
72 template <>
73 class serialize<bool>
74 {
75 public:
76  void operator()(bool& t, serializer& ser)
77  {
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 {
94 public:
95  inline void operator()(T*& t, serializer& ser)
96  {
97  switch ( ser.mode() ) {
98  case serializer::SIZER:
99  ser.size(*t);
100  break;
101  case serializer::PACK:
102  ser.primitive(*t);
103  break;
104  case serializer::UNPACK:
105  t = new T();
106  ser.primitive(*t);
107  break;
108  }
109  }
110 };
111 
112 /**
113  Version of serialize that works for std::pair.
114  */
115 template <class U, class V>
116 class serialize<std::pair<U, V>>
117 {
118 public:
119  inline void operator()(std::pair<U, V>& t, serializer& ser)
120  {
121  serialize<U>()(t.first, ser);
122  serialize<V>()(t.second, ser);
123  }
124 };
125 
126 template <class T>
127 inline void
128 operator&(serializer& ser, T& t)
129 {
130  serialize<T>()(t, ser);
131 }
132 
133 } // namespace Serialization
134 } // namespace Core
135 } // namespace SST
136 
137 #include "sst/core/serialization/serialize_array.h"
138 #include "sst/core/serialization/serialize_deque.h"
139 #include "sst/core/serialization/serialize_list.h"
140 #include "sst/core/serialization/serialize_map.h"
141 #include "sst/core/serialization/serialize_set.h"
142 #include "sst/core/serialization/serialize_string.h"
143 #include "sst/core/serialization/serialize_vector.h"
144 
145 #endif // SST_CORE_SERIALIZATION_SERIALIZE_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:34
Base serialize class.
Definition: serialize.h:31