12#ifndef SST_CORE_SERIALIZATION_SERIALIZE_H
13#define SST_CORE_SERIALIZATION_SERIALIZE_H
15#include "sst/core/from_string.h"
16#include "sst/core/serialization/objectMap.h"
17#include "sst/core/serialization/serializer.h"
18#include "sst/core/warnmacros.h"
20#define SST_INCLUDING_SERIALIZE_H
21#include "sst/core/serialization/impl/serialize_utility.h"
22#undef SST_INCLUDING_SERIALIZE_H
37#define SST_FRIEND_SERIALIZE() \
38 template <class SER> \
39 friend class pvt::serialize
51using ser_opt_t = uint32_t;
67 as_ptr_elem = 1u << 2,
69 map_read_only = 1u << 3,
82 return flags &
static_cast<ser_opt_t
>(option);
86namespace Core::Serialization {
91void sst_ser_object(serializer& ser, T&& obj, ser_opt_t options,
const char* name);
97struct bit_reference_wrapper
99 typename T::reference ref;
100 explicit bit_reference_wrapper(
typename T::reference ref) :
103 bit_reference_wrapper(
const bit_reference_wrapper&) =
default;
104 bit_reference_wrapper& operator=(
const bit_reference_wrapper&) =
delete;
105 ~bit_reference_wrapper() =
default;
112constexpr decltype(
auto)
115 if constexpr ( std::is_pointer_v<T> )
130template <
typename T,
typename =
void>
133 static_assert(std::is_class_v<std::remove_pointer_t<T>>,
134 "Trying to serialize a non-class type (or a pointer to one) with the default serialize_impl.");
136 static_assert(std::is_final_v<std::remove_pointer_t<T>> || !std::is_polymorphic_v<std::remove_pointer_t<T>>,
137 "The type or the pointed-to-type is polymorphic and non-final, so serialize_impl cannot know the runtime type "
138 "of the object being serialized. Polymorphic classes need to inherit from the serializable class and call the "
139 "ImplementSerializable() macro. For classes marked final, this is not necessary.");
141 static_assert(has_serialize_order_v<std::remove_pointer_t<T>>,
142 "The type or the pointed-to-type does not have a serialize_impl specialization, nor a public "
143 "serialize_order() method. This means that there is not a serialize_impl class specialization for "
144 "the type or a pointer to the type, and so the default serialize_impl is used which requires a "
145 "public serialize_order() method to be defined inside of the class type.");
148 void operator()(T& t,
serializer& ser, ser_opt_t UNUSED(options))
150 const auto& tPtr = get_ptr(t);
151 const auto mode = ser.mode();
152 if ( mode == serializer::MAP ) {
155 ser.mapper().report_object_map(map);
156 ser.mapper().map_hierarchy_start(ser.getMapName(), map);
158 else if constexpr ( std::is_pointer_v<T> ) {
159 if ( mode == serializer::UNPACK ) {
160 t =
new std::remove_pointer_t<T>();
161 ser.unpacker().report_new_pointer(
reinterpret_cast<uintptr_t
>(t));
166 tPtr->serialize_order(ser);
168 if ( mode == serializer::MAP ) ser.mapper().map_hierarchy_end();
184 friend void sst_ser_object(
serializer& ser, U&& obj, ser_opt_t options,
const char* name);
196 void serialize_and_track_pointer(T& t,
serializer& ser, ser_opt_t options)
198 if ( !ser.is_pointer_tracking_enabled() )
return serialize_impl<T>()(t, ser, options);
201 uintptr_t ptr =
reinterpret_cast<uintptr_t
>(&t);
203 switch ( ser.mode() ) {
204 case serializer::SIZER:
209 if ( ser.sizer().check_pointer_sizer(ptr) ) {
218 case serializer::PACK:
221 ser.packer().check_pointer_pack(ptr);
227 case serializer::UNPACK:
233 uintptr_t ptr_stored;
234 ser.unpack(ptr_stored);
237 ser.unpacker().report_real_pointer(ptr_stored, ptr);
241 case serializer::MAP:
252 friend void sst_ser_object(
serializer& ser, U&& obj, ser_opt_t options,
const char* name);
253 void operator()(T*& t,
serializer& ser, ser_opt_t options)
256 if ( !ser.is_pointer_tracking_enabled() ) {
257 switch ( ser.mode() ) {
258 case serializer::UNPACK:
264 bool nonnull = t !=
nullptr;
265 ser.primitive(nonnull);
271 case serializer::MAP:
277 uintptr_t ptr =
reinterpret_cast<uintptr_t
>(t);
278 if (
nullptr == t ) ptr = 0;
280 switch ( ser.mode() ) {
281 case serializer::SIZER:
286 if ( 0 == ptr )
return;
290 if ( !ser.sizer().check_pointer_sizer(ptr) ) {
294 case serializer::PACK:
299 if ( 0 == ptr )
return;
301 if ( !ser.packer().check_pointer_pack(ptr) ) {
305 case serializer::UNPACK:
308 uintptr_t ptr_stored;
309 ser.unpack(ptr_stored);
312 if ( 0 == ptr_stored ) {
317 uintptr_t real_ptr = ser.unpacker().check_pointer_unpack(ptr_stored);
320 t =
reinterpret_cast<T*
>(real_ptr);
324 ser.unpacker().report_real_pointer(ptr_stored,
reinterpret_cast<uintptr_t
>(t));
328 case serializer::MAP:
330 ObjectMap* map = ser.mapper().check_pointer_map(
reinterpret_cast<uintptr_t
>(t));
331 if ( map !=
nullptr ) {
334 ser.mapper().map_existing_object(ser.getMapName(), map);
353sst_ser_object(
serializer& ser, TREF&& obj, ser_opt_t options,
const char* name)
356 using T = std::remove_reference_t<TREF>;
360 if ( !ser.is_pointer_tracking_enabled() ) {
364 else if ( ser.mode() == serializer::MAP ) {
367 if ( !SerOption::is_set(options, SerOption::no_map) ) {
372 else if constexpr ( !std::is_pointer_v<T> ) {
374 if ( SerOption::is_set(options, SerOption::as_ptr) ) {
375 pvt::serialize<T>().serialize_and_track_pointer(obj, ser, options);
378 pvt::serialize<T>()(obj, ser, options);
383 pvt::serialize<T>()(obj, ser, options);
391#define SST_SER(obj, ...) \
392 SST::Core::Serialization::pvt::sst_ser_object( \
393 ser, (obj), SST::Core::Serialization::pvt::sst_ser_or_helper(__VA_ARGS__), #obj)
395#define SST_SER_NAME(obj, name, ...) \
396 SST::Core::Serialization::pvt::sst_ser_object( \
397 ser, (obj), SST::Core::Serialization::pvt::sst_ser_or_helper(__VA_ARGS__), name)
400template <
typename... Args>
402sst_ser_or_helper(Args... args)
404 return (SerOption::none | ... | args);
412ObjectMapSerialization(T&& obj)
416 ser.enable_pointer_tracking();
417 ser.start_mapping(&root);
418 SST_SER_NAME(obj,
"_proxy_object_");
429#define SST_INCLUDING_SERIALIZE_H
430#include "sst/core/serialization/impl/serialize_adapter.h"
431#include "sst/core/serialization/impl/serialize_aggregate.h"
432#include "sst/core/serialization/impl/serialize_array.h"
433#include "sst/core/serialization/impl/serialize_atomic.h"
434#include "sst/core/serialization/impl/serialize_bitset.h"
435#include "sst/core/serialization/impl/serialize_insertable.h"
436#include "sst/core/serialization/impl/serialize_optional.h"
437#include "sst/core/serialization/impl/serialize_shared_ptr.h"
438#include "sst/core/serialization/impl/serialize_string.h"
439#include "sst/core/serialization/impl/serialize_trivial.h"
440#include "sst/core/serialization/impl/serialize_tuple.h"
441#include "sst/core/serialization/impl/serialize_unique_ptr.h"
442#include "sst/core/serialization/impl/serialize_valarray.h"
443#include "sst/core/serialization/impl/serialize_variant.h"
446#undef SST_INCLUDING_SERIALIZE_H
ObjectMap object for non-fundamental, non-container types.
Definition objectMap.h:729
ObjectMap context which is saved in a virtual stack when name or other information changes.
Definition serializer.h:158
Base class for objects created by the serializer mapping mode used to map the variables for objects.
Definition objectMap.h:188
void incRefCount()
Increment the reference counter for this ObjectMap.
Definition objectMap.h:332
ObjectMap * findVariable(const std::string &name, bool confirm=false) const
Find a variable in this object map.
Definition objectMap.cc:228
Serialization "gateway" object called by sst_ser_object().
Definition serialize.h:182
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
Definition serialize.h:54
Options
Options use to control how serialization acts for specific elements that are serialized.
Definition serialize.h:59