SST  10.0.0
StructuralSimulationToolkit
serialize_serializable.h
1 // Copyright 2009-2020 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-2020, 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_SERIALIZABLE_H
13 #define SERIALIZE_SERIALIZABLE_H
14 
15 #include "sst/core/serialization/serializable.h"
16 #include "sst/core/serialization/serializer.h"
17 #include "sst/core/serialization/serialize.h"
18 #include <iostream>
19 
20 namespace SST {
21 namespace Core {
22 namespace Serialization {
23 
24 namespace pvt {
25 
26 void
27 size_serializable(serializable* s, serializer& ser);
28 
29 void
30 pack_serializable(serializable* s, serializer& ser);
31 
32 void
33 unpack_serializable(serializable*& s, serializer& ser);
34 
35 }
36 
37 template <>
39 
40 public:
41 void
42 operator()(serializable*& s, serializer& ser)
43 {
44  switch (ser.mode()){
45  case serializer::SIZER:
46  pvt::size_serializable(s,ser);
47  break;
48  case serializer::PACK:
49  pvt::pack_serializable(s,ser);
50  break;
51  case serializer::UNPACK:
52  pvt::unpack_serializable(s,ser);
53  break;
54  }
55 }
56 
57 };
58 
59 
60 template <class T>
61 class serialize<T*, typename std::enable_if<std::is_base_of<SST::Core::Serialization::serializable,T>::value>::type> {
62 
63 public:
64 void
65 operator()(T*& s, serializer& ser)
66 {
67  serializable* sp = static_cast<serializable*>(s);
68  switch (ser.mode()){
69  case serializer::SIZER:
70  pvt::size_serializable(sp,ser);
71  break;
72  case serializer::PACK:
73  pvt::pack_serializable(sp,ser);
74  break;
75  case serializer::UNPACK:
76  pvt::unpack_serializable(sp,ser);
77  break;
78  }
79  s = static_cast<T*>(sp);
80 }
81 
82 };
83 
84 template <class T>
85 void
86 serialize_intrusive_ptr(T*& t, serializer& ser)
87 {
88  serializable* s = t;
89  switch (ser.mode()){
90 case serializer::SIZER:
91  pvt::size_serializable(s,ser);
92  break;
93 case serializer::PACK:
94  pvt::pack_serializable(s,ser);
95  break;
96 case serializer::UNPACK:
97  pvt::unpack_serializable(s,ser);
98  t = dynamic_cast<T*>(s);
99  break;
100  }
101 }
102 
103 template <class T>
104 class serialize <T, typename std::enable_if<std::is_base_of<SST::Core::Serialization::serializable,T>::value>::type> {
105 public:
106  inline void operator()(T& t, serializer& ser){
107  // T* tmp = &t;
108  // serialize_intrusive_ptr(tmp, ser);
109  t.serialize_order(ser);
110  }
111 };
112 
113 // Hold off on trivially_serializable for now, as it's not really safe
114 // in the case of inheritance
115 //
116 // template <class T>
117 // class serialize <T, typename std::enable_if<std::is_base_of<SST::Core::Serialization::trivially_serializable,T>::value>::type> {
118 // public:
119 // inline void operator()(T& t, serializer& ser){
120 // ser.primitive(t);
121 // }
122 // };
123 
124 
125 }
126 }
127 }
128 
129 #endif // SERIALIZE_SERIALIZABLE_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: serializable.h:109
Base serialize class.
Definition: serialize.h:33