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/output.h"
17#include "sst/core/serialization/objectMap.h"
18#include "sst/core/serialization/serializer.h"
19#include "sst/core/warnmacros.h"
21#define SST_INCLUDING_SERIALIZE_H
22#include "sst/core/serialization/impl/serialize_utility.h"
23#undef SST_INCLUDING_SERIALIZE_H
38#define SST_FRIEND_SERIALIZE() \
39 template <class SER> \
40 friend class pvt::serialize
52using ser_opt_t = uint32_t;
68 as_ptr_elem = 1u << 2,
70 map_read_only = 1u << 3,
83 return flags &
static_cast<ser_opt_t
>(option);
87namespace Core::Serialization {
92void sst_ser_object(serializer& ser, T&& obj, ser_opt_t options,
const char* name);
98struct bit_reference_wrapper
100 typename T::reference ref;
101 explicit bit_reference_wrapper(
typename T::reference ref) :
104 bit_reference_wrapper(
const bit_reference_wrapper&) =
default;
105 bit_reference_wrapper& operator=(
const bit_reference_wrapper&) =
delete;
106 ~bit_reference_wrapper() =
default;
113constexpr decltype(
auto)
116 if constexpr ( std::is_pointer_v<T> )
131template <
typename T,
typename =
void>
134 static_assert(std::is_class_v<std::remove_pointer_t<T>>,
135 "Trying to serialize a non-class type (or a pointer to one) with the default serialize_impl.");
137 static_assert(std::is_final_v<std::remove_pointer_t<T>> || !std::is_polymorphic_v<std::remove_pointer_t<T>>,
138 "The type or the pointed-to-type is polymorphic and non-final, so serialize_impl cannot know the runtime type "
139 "of the object being serialized. Polymorphic classes need to inherit from the serializable class and call the "
140 "ImplementSerializable() macro. For classes marked final, this is not necessary.");
142 static_assert(has_serialize_order_v<std::remove_pointer_t<T>>,
143 "The type or the pointed-to-type does not have a serialize_impl specialization, nor a public "
144 "serialize_order() method. This means that there is not a serialize_impl class specialization for "
145 "the type or a pointer to the type, and so the default serialize_impl is used which requires a "
146 "public serialize_order() method to be defined inside of the class type.");
149 void operator()(T& t,
serializer& ser, ser_opt_t UNUSED(options))
151 const auto& tPtr = get_ptr(t);
152 const auto mode = ser.mode();
153 if ( mode == serializer::MAP ) {
156 ser.mapper().report_object_map(map);
157 ser.mapper().map_hierarchy_start(ser.getMapName(), map);
159 else if constexpr ( std::is_pointer_v<T> ) {
160 if ( mode == serializer::UNPACK ) {
161 t =
new std::remove_pointer_t<T>();
162 ser.unpacker().report_new_pointer(
reinterpret_cast<uintptr_t
>(t));
167 tPtr->serialize_order(ser);
169 if ( mode == serializer::MAP ) ser.mapper().map_hierarchy_end();
185 friend void sst_ser_object(
serializer& ser, U&& obj, ser_opt_t options,
const char* name);
197 void serialize_and_track_pointer(T& t,
serializer& ser, ser_opt_t options)
199 if ( !ser.is_pointer_tracking_enabled() )
return serialize_impl<T>()(t, ser, options);
202 uintptr_t ptr =
reinterpret_cast<uintptr_t
>(&t);
204 switch ( ser.mode() ) {
205 case serializer::SIZER:
210 if ( ser.sizer().check_pointer_sizer(ptr) ) {
211 Output::getDefaultObject().output(
212 "WARNING: Serializaion of object of type %s using as_ptr option detected that a pointer to this "
214 "already serialized. Serialization of the object should happen before serialization of any "
215 "pointers to the object.\n",
224 case serializer::PACK:
227 ser.packer().check_pointer_pack(ptr);
233 case serializer::UNPACK:
239 uintptr_t ptr_stored;
240 ser.unpack(ptr_stored);
243 ser.unpacker().report_real_pointer(ptr_stored, ptr);
247 case serializer::MAP:
258 friend void sst_ser_object(
serializer& ser, U&& obj, ser_opt_t options,
const char* name);
259 void operator()(T*& t,
serializer& ser, ser_opt_t options)
262 if ( !ser.is_pointer_tracking_enabled() ) {
263 switch ( ser.mode() ) {
264 case serializer::UNPACK:
270 bool nonnull = t !=
nullptr;
271 ser.primitive(nonnull);
277 case serializer::MAP:
283 uintptr_t ptr =
reinterpret_cast<uintptr_t
>(t);
284 if (
nullptr == t ) ptr = 0;
286 switch ( ser.mode() ) {
287 case serializer::SIZER:
292 if ( 0 == ptr )
return;
296 if ( !ser.sizer().check_pointer_sizer(ptr) ) {
300 case serializer::PACK:
305 if ( 0 == ptr )
return;
307 if ( !ser.packer().check_pointer_pack(ptr) ) {
311 case serializer::UNPACK:
314 uintptr_t ptr_stored;
315 ser.unpack(ptr_stored);
318 if ( 0 == ptr_stored ) {
323 uintptr_t real_ptr = ser.unpacker().check_pointer_unpack(ptr_stored);
326 t =
reinterpret_cast<T*
>(real_ptr);
330 ser.unpacker().report_real_pointer(ptr_stored,
reinterpret_cast<uintptr_t
>(t));
334 case serializer::MAP:
336 ObjectMap* map = ser.mapper().check_pointer_map(
reinterpret_cast<uintptr_t
>(t));
337 if ( map !=
nullptr ) {
340 ser.mapper().map_existing_object(ser.getMapName(), map);
359sst_ser_object(
serializer& ser, TREF&& obj, ser_opt_t options,
const char* name)
362 using T = std::remove_reference_t<TREF>;
366 if ( !ser.is_pointer_tracking_enabled() ) {
370 else if ( ser.mode() == serializer::MAP ) {
373 if ( !SerOption::is_set(options, SerOption::no_map) ) {
378 else if constexpr ( !std::is_pointer_v<T> ) {
380 if ( SerOption::is_set(options, SerOption::as_ptr) ) {
381 pvt::serialize<T>().serialize_and_track_pointer(obj, ser, options);
384 pvt::serialize<T>()(obj, ser, options);
389 pvt::serialize<T>()(obj, ser, options);
397#define SST_SER(obj, ...) \
398 SST::Core::Serialization::pvt::sst_ser_object( \
399 ser, (obj), SST::Core::Serialization::pvt::sst_ser_or_helper(__VA_ARGS__), #obj)
401#define SST_SER_NAME(obj, name, ...) \
402 SST::Core::Serialization::pvt::sst_ser_object( \
403 ser, (obj), SST::Core::Serialization::pvt::sst_ser_or_helper(__VA_ARGS__), name)
406template <
typename... Args>
408sst_ser_or_helper(Args... args)
410 return (SerOption::none | ... | args);
418ObjectMapSerialization(T&& obj)
422 ser.enable_pointer_tracking();
423 ser.start_mapping(&root);
424 SST_SER_NAME(obj,
"_proxy_object_");
435#define SST_INCLUDING_SERIALIZE_H
436#include "sst/core/serialization/impl/serialize_adapter.h"
437#include "sst/core/serialization/impl/serialize_aggregate.h"
438#include "sst/core/serialization/impl/serialize_array.h"
439#include "sst/core/serialization/impl/serialize_atomic.h"
440#include "sst/core/serialization/impl/serialize_bitset.h"
441#include "sst/core/serialization/impl/serialize_insertable.h"
442#include "sst/core/serialization/impl/serialize_optional.h"
443#include "sst/core/serialization/impl/serialize_shared_ptr.h"
444#include "sst/core/serialization/impl/serialize_string.h"
445#include "sst/core/serialization/impl/serialize_trivial.h"
446#include "sst/core/serialization/impl/serialize_tuple.h"
447#include "sst/core/serialization/impl/serialize_unique_ptr.h"
448#include "sst/core/serialization/impl/serialize_valarray.h"
449#include "sst/core/serialization/impl/serialize_variant.h"
452#undef SST_INCLUDING_SERIALIZE_H
ObjectMap object for non-fundamental, non-container types.
Definition objectMap.h:747
ObjectMapClass()=default
Default constructor.
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:189
void incRefCount()
Increment the reference counter for this ObjectMap.
Definition objectMap.h:338
ObjectMap * findVariable(const std::string &name, bool confirm=false) const
Find a variable in this object map.
Definition objectMap.cc:228
static std::string demangle_name(const char *name)
Static function to demangle type names returned from typeid(T).name().
Definition objectMap.cc:152
Serialization "gateway" object called by sst_ser_object().
Definition serialize.h:183
Base serialize class.
Definition serialize.h:133
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:55
Options
Options use to control how serialization acts for specific elements that are serialized.
Definition serialize.h:60