SST 16.0.0
Structural Simulation Toolkit
serialize_optional.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_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#include <string>
24
25namespace SST::Core::Serialization {
26
27// Serialize std::optional
28template <typename T>
29class serialize_impl<std::optional<T>>
30{
31 // ObjectMap class representing has_value state of a std::optional
32 // If this ObjectMap is changed, the std::optional's has_value state is changed
33 // This is only used in mapping mode
34 class ObjectMapOptionalHasValue : public ObjectMapFundamental<bool>
35 {
36 std::optional<T>& obj;
37 bool has_value;
38 ObjectMap* const parent;
39
40 public:
41 ObjectMapOptionalHasValue(std::optional<T>& obj, ObjectMap* parent) :
43 obj(obj),
44 has_value(obj),
45 parent(parent)
46 {}
47
48 void set_impl(const std::string& value) override
49 {
50 bool old = has_value;
52 if ( has_value == old ) return;
53
54 if ( has_value ) {
55 obj.emplace();
56 parent->addVariable("value", ObjectMapSerialization(*obj));
57 }
58 else {
59 obj.reset();
60 parent->removeVariable("value");
61 }
62 }
63 };
64
65 void operator()(std::optional<T>& obj, serializer& ser, ser_opt_t options)
66 {
67 bool has_value = obj.has_value();
68
69 switch ( ser.mode() ) {
70 case serializer::MAP:
71 ser.mapper().map_hierarchy_start(ser.getMapName(), new ObjectMapContainer<std::optional<T>>(&obj));
72 ser.mapper().map_object("has_value", new ObjectMapOptionalHasValue(obj, ser.mapper().get_top()));
73 if ( obj ) ser.mapper().map_object("value", ObjectMapSerialization(*obj));
74 ser.mapper().map_hierarchy_end();
75 return;
76
77 case serializer::SIZER:
78 ser.size(has_value);
79 break;
80
81 case serializer::PACK:
82 ser.pack(has_value);
83 break;
84
85 case serializer::UNPACK:
86 ser.unpack(has_value);
87 if ( has_value )
88 obj.emplace();
89 else
90 obj.reset();
91 break;
92 }
93
94 // Serialize the optional object if it is present
95 if ( has_value ) SST_SER(*obj, options);
96 }
97
98 SST_FRIEND_SERIALIZE();
99};
100
101} // namespace SST::Core::Serialization
102
103#endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_OPTIONAL_H
Class used to map containers.
Definition objectMap.h:1420
ObjectMap representing fundamental types, and classes treated as fundamental types.
Definition objectMap.h:1230
virtual void set_impl(const std::string &value) override
Set the value of the object represented as a string.
Definition objectMap.h:1244
Base class for objects created by the serializer mapping mode used to map the variables for objects.
Definition objectMap.h:188
virtual void removeVariable(const std::string &UNUSED(name))
Removes a variable from this ObjectMap.
Definition objectMap.h:412
virtual void addVariable(const std::string &UNUSED(name), ObjectMap *UNUSED(obj))
Adds a variable to this ObjectMap.
Definition objectMap.h:403
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