SST 16.0.0
Structural Simulation Toolkit
serialize_string.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_STRING_H
13#define SST_CORE_SERIALIZATION_IMPL_SERIALIZE_STRING_H
14
15#ifndef SST_INCLUDING_SERIALIZE_H
16#warning \
17 "The header file sst/core/serialization/impl/serialize_string.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 <string>
23#include <type_traits>
24#include <vector>
25
26namespace SST::Core::Serialization {
27
28class ObjectMapString : public ObjectMap
29{
30protected:
31 std::string* addr_;
32
33public:
34 /**
35 Get the address of the represented object
36
37 @return address of represented object
38 */
39 void* getAddr() const final override { return addr_; }
40
41 std::string get() const final override { return *addr_; }
42
43 void set_impl(const std::string& value) final override { *addr_ = value; }
44
45 bool isFundamental() const final override { return true; }
46
47 std::string getType() const final override
48 {
49 // The demangled name for std::string is ridiculously long, so
50 // just return "std::string"
51 return "std::string";
52 }
53
54 explicit ObjectMapString(std::string* addr) :
55 addr_(addr)
56 {}
57
58 /**
59 Disallow copying and assignment
60 */
61
62 ObjectMapString(const ObjectMapString&) = delete;
63 ObjectMapString& operator=(const ObjectMapString&) = delete;
64
65 ~ObjectMapString() override = default;
66};
67
68template <typename T>
69class serialize_impl<T, std::enable_if_t<std::is_same_v<std::remove_pointer_t<T>, std::string>>>
70{
71 void operator()(T& str, serializer& ser, ser_opt_t options)
72 {
73 // sPtr is a reference to either str if it's a pointer, or to &str if it's not
74 const auto& sPtr = get_ptr(str);
75 const auto mode = ser.mode();
76 if ( mode == serializer::MAP ) {
77 ObjectMap* obj_map = new ObjectMapString(sPtr);
78 if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->setReadOnly();
79 ser.mapper().map_object(ser.getMapName(), obj_map);
80 }
81 else {
82 if constexpr ( std::is_pointer_v<T> ) {
83 if ( mode == serializer::UNPACK ) str = new std::string();
84 }
85 ser.string(*sPtr);
86 }
87 }
88
89 SST_FRIEND_SERIALIZE();
90};
91
92} // namespace SST::Core::Serialization
93
94#endif // SST_CORE_SERIALIZATION_IMPL_SERIALIZE_STRING_H
Definition serialize_string.h:29
ObjectMapString(const ObjectMapString &)=delete
Disallow copying and assignment.
void * getAddr() const final override
Get the address of the represented object.
Definition serialize_string.h:39
std::string getType() const final override
Get the type of the variable represented by the ObjectMap.
Definition serialize_string.h:47
bool isFundamental() const final override
Check to see if this ObjectMap represents a fundamental or a class treated as a fundamental.
Definition serialize_string.h:45
std::string get() const final override
Get the value of the variable as a string.
Definition serialize_string.h:41
Base class for objects created by the serializer mapping mode used to map the variables for objects.
Definition objectMap.h:188
ObjectMap()=default
Default constructor primarily used for the "top" object in the hierarchy.
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