SST 12.1.0
Structural Simulation Toolkit
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
21namespace SST {
22namespace Core {
23namespace Serialization {
24
25namespace pvt {
26
27void size_serializable(serializable* s, serializer& ser);
28
29void pack_serializable(serializable* s, serializer& ser);
30
31void unpack_serializable(serializable*& s, serializer& ser);
32
33} // namespace pvt
34
35template <>
37{
38
39public:
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
56template <class T>
57class serialize<T*, typename std::enable_if<std::is_base_of<SST::Core::Serialization::serializable, T>::value>::type>
58{
59
60public:
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
79template <class T>
80void
81serialize_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
98template <class T>
99class serialize<T, typename std::enable_if<std::is_base_of<SST::Core::Serialization::serializable, T>::value>::type>
100{
101public:
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
Definition: serializable.h:119
Base serialize class.
Definition: serialize.h:32
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35