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