SST 15.0
Structural Simulation Toolkit
serializable.h
1// Copyright 2009-2025 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-2025, 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_SERIALIZABLE_H
13#define SST_CORE_SERIALIZATION_SERIALIZABLE_H
14
15#include "sst/core/serialization/serializable_base.h"
16#include "sst/core/serialization/serialize.h"
17
18#include <cstdint>
19#include <type_traits>
20
21namespace SST::Core::Serialization {
22
24{
25public:
26 static constexpr uint32_t NullClsId = std::numeric_limits<uint32_t>::max();
27
28 // virtual const char* cls_name() const = 0;
29
30 // virtual void serialize_order(serializer& ser) = 0;
31
32 // virtual uint32_t cls_id() const = 0;
33 // virtual std::string serialization_name() const = 0;
34
35 virtual ~serializable() {}
36};
37
38namespace pvt {
39
40void size_serializable(serializable_base* s, serializer& ser);
41
42void pack_serializable(serializable_base* s, serializer& ser);
43
44void unpack_serializable(serializable_base*& s, serializer& ser);
45
46void map_serializable(serializable_base*& s, serializer& ser);
47
48} // namespace pvt
49
50
51template <class T>
52class serialize_impl<T*, std::enable_if_t<std::is_base_of_v<serializable, T>>>
53{
54 void operator()(T*& s, serializer& ser, ser_opt_t UNUSED(options))
55 {
56 serializable_base* sp = static_cast<serializable_base*>(s);
57 switch ( ser.mode() ) {
58 case serializer::SIZER:
59 pvt::size_serializable(sp, ser);
60 break;
61 case serializer::PACK:
62 pvt::pack_serializable(sp, ser);
63 break;
64 case serializer::UNPACK:
65 pvt::unpack_serializable(sp, ser);
66 break;
67 case serializer::MAP:
68 pvt::map_serializable(sp, ser);
69 break;
70 }
71 s = static_cast<T*>(sp);
72 }
73
74 SST_FRIEND_SERIALIZE();
75};
76
77template <class T>
78void
79serialize_intrusive_ptr(T*& t, serializer& ser)
80{
81 serializable_base* s = t;
82 switch ( ser.mode() ) {
83 case serializer::SIZER:
84 pvt::size_serializable(s, ser);
85 break;
86 case serializer::PACK:
87 pvt::pack_serializable(s, ser);
88 break;
89 case serializer::UNPACK:
90 pvt::unpack_serializable(s, ser);
91 t = dynamic_cast<T*>(s);
92 break;
93 case serializer::MAP:
94 // Add your code here
95 break;
96 }
97}
98
99template <class T>
100class serialize_impl<T, std::enable_if_t<std::is_base_of_v<serializable, T>>>
101{
102 inline void operator()(T& t, serializer& ser, ser_opt_t UNUSED(options))
103 {
104 // T* tmp = &t;
105 // serialize_intrusive_ptr(tmp, ser);
106 t.serialize_order(ser);
107
108 // TODO: Need to figure out how to handle mapping mode for
109 // classes inheriting from serializable that are not pointers.
110 // For the core, this really only applies to SharedObjects,
111 // which may actually need their own specific serialization.
112
113 // For now mapping mode won't provide any data
114 }
115
116 SST_FRIEND_SERIALIZE();
117};
118
119} // namespace SST::Core::Serialization
120
121// #include "sst/core/serialization/serialize_serializable.h"
122
123#endif
Definition serializable_base.h:119
Definition serializable.h:24
Base serialize class.
Definition serialize.h:110
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45