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