SST  15.1.0
StructuralSimulationToolkit
serialize_optional.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_OPTIONAL_H
13 #define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_OPTIONAL_H
14 
15 #ifndef SST_INCLUDING_SERIALIZE_H
16 #warning \
17  "The header file sst/core/serialization/impl/serialize_optional.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 <optional>
23 
24 namespace SST::Core::Serialization {
25 
26 // Serialize std::optional
27 template <typename T>
28 class serialize_impl<std::optional<T>>
29 {
30  void operator()(std::optional<T>& obj, serializer& ser, ser_opt_t options)
31  {
32  bool has_value = false;
33 
34  switch ( ser.mode() ) {
35  case serializer::SIZER:
36  has_value = obj.has_value();
37  ser.size(has_value);
38  break;
39 
40  case serializer::PACK:
41  has_value = obj.has_value();
42  ser.pack(has_value);
43  break;
44 
45  case serializer::UNPACK:
46  ser.unpack(has_value);
47  if ( has_value )
48  obj.emplace();
49  else
50  obj.reset();
51  break;
52 
53  case serializer::MAP:
54  {
55  // TODO: how to map std::optional ?
56  return;
57  }
58  }
59 
60  // Serialize the optional object if it is present
61  if ( has_value ) SST_SER(*obj, options);
62  }
63 
64  SST_FRIEND_SERIALIZE();
65 };
66 
67 } // namespace SST::Core::Serialization
68 
69 #endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_OPTIONAL_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
Base serialize class.
Definition: serialize.h:113