SST  15.1.0
StructuralSimulationToolkit
serialize_trivial.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_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/serialization/serializer.h"
21 
22 #include <array>
23 #include <bitset>
24 #include <type_traits>
25 #include <utility>
26 
27 namespace SST::Core::Serialization {
28 
29 // Types excluded because they would cause ambiguity with more specialized methods
30 template <typename T>
31 struct is_trivially_serializable_excluded : std::is_array<T>
32 {};
33 
34 template <typename T, size_t S>
35 struct is_trivially_serializable_excluded<std::array<T, S>> : std::true_type
36 {};
37 
38 template <size_t N>
39 struct is_trivially_serializable_excluded<std::bitset<N>> : std::true_type
40 {};
41 
42 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43 // Version of serialize that works for trivially serializable types which aren't excluded, and pointers thereof //
44 // //
45 // Note that the pointer tracking happens at a higher level, and only if it is turned on. If it is not turned //
46 // on, then this only copies the value pointed to into the buffer. If multiple objects point to the same //
47 // location, they will each have an independent copy after deserialization. //
48 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49 
50 template <class T>
52  std::enable_if_t<std::conjunction_v<std::negation<is_trivially_serializable_excluded<std::remove_pointer_t<T>>>,
53  is_trivially_serializable<std::remove_pointer_t<T>>>>>
54 {
55  void operator()(T& t, serializer& ser, ser_opt_t UNUSED(options))
56  {
57  switch ( const auto mode = ser.mode() ) {
58  case serializer::MAP:
59  // Right now only arithmetic and enum types are handled in mapping mode without custom serializer
60  if constexpr ( std::is_arithmetic_v<std::remove_pointer_t<T>> ||
61  std::is_enum_v<std::remove_pointer_t<T>> ) {
62  ObjectMap* obj_map;
63  if constexpr ( std::is_pointer_v<T> )
65  else
66  obj_map = new ObjectMapFundamental<T>(&t);
67  if ( SerOption::is_set(options, SerOption::map_read_only) ) ser.mapper().setNextObjectReadOnly();
68  ser.mapper().map_primitive(ser.getMapName(), obj_map);
69  }
70  else {
71  // TODO: Handle mapping mode for trivially serializable types without from_string() methods which do not
72  // define their own serialization methods.
73  }
74  break;
75 
76  default:
77  if constexpr ( std::is_pointer_v<T> ) {
78  if ( mode == serializer::UNPACK ) t = new std::remove_pointer_t<T> {};
79  ser.primitive(*t);
80  }
81  else {
82  ser.primitive(t);
83  }
84  break;
85  }
86  }
87 
88  SST_FRIEND_SERIALIZE();
89 };
90 
91 } // namespace SST::Core::Serialization
92 
93 #endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_TRIVIAL_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
ObjectMap representing fundamental types, and classes treated as fundamental types.
Definition: objectMap.h:1264
Base serialize class.
Definition: serialize.h:113
Base class for objects created by the serializer mapping mode used to map the variables for objects...
Definition: objectMap.h:158