SST 16.0.0
Structural Simulation Toolkit
serialize_trivial.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_IMPL_SERIALIZE_TRIVIAL_H
13#define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_TRIVIAL_H
14
15#ifndef SST_INCLUDING_SERIALIZE_H
16#warning \
17 "The header file sst/core/serialization/impl/serialize_trivial.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serialize.h"
18#endif
19
20#include "sst/core/output.h"
21#include "sst/core/serialization/serializer.h"
22
23#include <array>
24#include <bitset>
25#include <cstddef>
26#include <string>
27#include <type_traits>
28#include <typeinfo>
29#include <utility>
30
31namespace SST::Core::Serialization {
32
33// Types excluded because they would cause ambiguity with more specialized methods
34template <typename T>
35struct is_trivially_serializable_excluded : std::is_array<T>
36{};
37
38template <typename T, size_t S>
39struct is_trivially_serializable_excluded<std::array<T, S>> : std::true_type
40{};
41
42template <size_t N>
43struct is_trivially_serializable_excluded<std::bitset<N>> : std::true_type
44{};
45
46//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47// Version of serialize that works for trivially serializable types which aren't excluded, and pointers thereof //
48// //
49// Note that the pointer tracking happens at a higher level, and only if it is turned on. If it is not turned //
50// on, then this only copies the value pointed to into the buffer. If multiple objects point to the same //
51// location, they will each have an independent copy after deserialization. //
52//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53
54template <class T>
56 std::enable_if_t<std::conjunction_v<std::negation<is_trivially_serializable_excluded<std::remove_pointer_t<T>>>,
57 is_trivially_serializable<std::remove_pointer_t<T>>>>>
58{
59 void operator()(T& t, serializer& ser, ser_opt_t options)
60 {
61 using U = std::remove_pointer_t<T>;
62 const auto& tPtr = get_ptr(t);
63 switch ( ser.mode() ) {
64 case serializer::MAP:
65 // Arithmetic, enum, complex types, and types constructible from std::string and convertible to std::string,
66 // are handled in mapping mode without a custom serializer
67 if constexpr ( std::is_arithmetic_v<U> || std::is_enum_v<U> || complex_properties<U>::is_complex ||
68 (std::is_constructible_v<U, std::string> && std::is_convertible_v<U, std::string>)) {
69 ObjectMap* obj_map = new ObjectMapFundamental<U>(tPtr);
70 if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->setReadOnly();
71 ser.mapper().map_object(ser.getMapName(), obj_map);
72 }
73 else {
74 // Print warning at verbose levels >= 2, but only print it once for each type
75 // This reduces surprise when a variable is not added to the ObjectMap
76 static int UNUSED(once) = [] {
77 Output& output = Output::getDefaultObject();
78 if ( output.getVerboseLevel() >= 2 ) {
79 std::string typestr = ObjectMap::demangle_name(typeid(U).name());
80 const char* type = typestr.c_str();
81 if constexpr ( std::is_class_v<U> )
82 output.verbose(CALL_INFO, 0, 0,
83 "Warning: Trivially serializable class type %s does not automatically have an "
84 "ObjectMap created for it.\nTo create an ObjectMap for %s, use one of these "
85 "methods:\n1. Add a serialize_order() method to %s.\n2. Define a serialize_impl<%s> "
86 "specialization.\n3. Add a constructor taking a std::string and an operator "
87 "std::string() const to %s, to allow conversion from/to std::string.\n\n",
88 type, type, type, type, type);
89 else if constexpr ( std::is_union_v<U> )
90 output.verbose(CALL_INFO, 0, 0,
91 "Warning: Trivially serializable union type %s does not automatically have an "
92 "ObjectMap created for it.\nTo create an ObjectMap for %s, use one of these "
93 "methods:\n1. Define a serialize_impl<%s> specialization.\n2. Add a constructor "
94 "taking a std::string and an operator std::string() const to %s, to allow "
95 "conversion from/to std::string.\n\n",
96 type, type, type, type);
97 else
98 output.verbose(CALL_INFO, 0, 0,
99 "Warning: Trivially serializable type %s does not automatically have an ObjectMap "
100 "created for it.\nTo create an ObjectMap for %s, define a serialize_impl<%s> "
101 "specialization.\n\n",
102 type, type, type);
103 }
104 return 0;
105 }();
106 }
107 break;
108
109 case serializer::UNPACK:
110 if constexpr ( std::is_pointer_v<T> ) t = new U();
111 [[fallthrough]];
112
113 default:
114 ser.primitive(*tPtr);
115 break;
116 }
117 }
118
119 SST_FRIEND_SERIALIZE();
120};
121
122} // namespace SST::Core::Serialization
123
124#endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_TRIVIAL_H
ObjectMap representing fundamental types, and classes treated as fundamental types.
Definition objectMap.h:1230
Base class for objects created by the serializer mapping mode used to map the variables for objects.
Definition objectMap.h:188
static std::string demangle_name(const char *name)
Static function to demangle type names returned from typeid(T).name().
Definition objectMap.cc:152
void setReadOnly(bool state=true)
Set the read-only state of the object.
Definition objectMap.h:263
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
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:58
uint32_t getVerboseLevel() const
Returns object verbose level.
Definition output.cc:109
void verbose(uint32_t line, const char *file, const char *func, uint32_t output_level, uint32_t output_bits, const char *format,...) const
Output the verbose message with formatting as specified by the format parameter.
Definition output.h:232