SST 16.1.0
Structural Simulation Toolkit
objectMapDeferred.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_OBJECTMAPDEFERRED_H
13#define SST_CORE_SERIALIZATION_OBJECTMAPDEFERRED_H
14
15#include "sst/core/baseComponent.h"
16#include "sst/core/serialization/serializer.h"
17
18#include <map>
19#include <string>
20#include <vector>
21
22namespace SST::Core::Serialization {
23
24/**
25 ObjectMap version that will delay building the internal data
26 structures until the object is "selected". Also, once the object's
27 parent is selected, the internal data will be deleted.
28 */
29template <typename T>
30class ObjectMapDeferred : public ObjectMap
31{
32public:
33 /**
34 Returns type of the deferred object
35
36 @return empty type of object
37 */
38 std::string getType() const final override { return type_; }
39
40 /**
41 Returns nullptr since there is no underlying object being
42 represented
43
44 @return nullptr
45 */
46 void* getAddr() const final override { return addr_; }
47
48
49 /**
50 Get the list of child variables contained in this ObjectMap
51
52 @return Reference to map containing ObjectMaps for this
53 ObjectMap's child variables. pair.first is the name of the
54 variable in the context of this object.
55 */
56 const ObjectMultimap& getVariables() const final override { return obj_->getVariables(); }
57
58 /**
59 For the Deferred Build, the only variable that gets added will
60 be the "real" ObjectMap.
61
62 @param name Name of the object
63 @param obj ObjectMap to add as a variable
64 */
65 void addVariable(const std::string& name, ObjectMap* obj) final override
66 {
67 // This should be the real ObjectMap for the class we are
68 // representing. We will check it by making sure the name
69 // matches what is passed in in the activate_callback()
70 // function.
71 if ( name == "!proxy!" ) {
72 obj_ = obj;
73 }
74 else {
75 printf("WARNING:: ObjectMapDeferred not built properly. No mapping will be available\n");
76 }
77 }
78
79 ObjectMapDeferred(T* addr, const std::string& type) :
80 ObjectMap(),
81 addr_(addr),
82 type_(demangle_name(type.c_str()))
83 {
84 // Set category based on type
85 if constexpr ( std::is_base_of_v<BaseComponent, T> || std::is_base_of_v<SubComponent, T> ) {
86 setCategory(ObjectCategory::Component);
87 }
88 }
89
90 ~ObjectMapDeferred() override { delete obj_; }
91
92protected:
93 void activate_callback() final override
94 {
95 // On activate, we need to create the ObjectMap data structure
96 if ( obj_ ) return;
97
99 ser.enable_pointer_tracking();
100 ser.start_mapping(this);
101
102 SST_SER_NAME(addr_, "!proxy!");
103 }
104
105 void deactivate_callback() final override
106 {
107 // On deactivate, need to delete obj_;
108 obj_->decRefCount();
109 obj_ = nullptr;
110 }
111
112
113private:
114 /**
115 ObjectMap for the real data
116 */
117 ObjectMap* obj_ = nullptr;
118
119 /**
120 Object this ObjectMap represents
121 */
122 T* addr_ = nullptr;
123
124 /**
125 Type of the variable as given by the demangled version of
126 typeid(T).name() for the type.
127 */
128 std::string type_ = "";
129};
130
131class ComponentSerializer : public ObjectMapDeferred<BaseComponent>
132{
133public:
134 ComponentSerializer(BaseComponent* comp) :
135 ObjectMapDeferred<BaseComponent>(comp, typeid(*comp).name())
136 {}
137
138 // Expose the protected activate to trigger serialization
139 void serialize() { activate_callback(); }
140
141 // Check if serialization produced results
142 bool hasSerialized() const
143 {
144 // After activate_callback, obj_ is set via addVariable("!proxy!", ...)
145 // getVariables() delegates to obj_->getVariables()
146 return !getVariables().empty();
147 }
148};
149
150} // namespace SST::Core::Serialization
151
152#endif // SST_CORE_SERIALIZATION_OBJECTMAPDEFERRED_H
Main component object for the simulation.
Definition baseComponent.h:98
ObjectMap version that will delay building the internal data structures until the object is "selected...
Definition objectMapDeferred.h:31
void deactivate_callback() final override
Function that will get called when this object is deactivated (i.e selectParent() is called).
Definition objectMapDeferred.h:105
const ObjectMultimap & getVariables() const final override
Get the list of child variables contained in this ObjectMap.
Definition objectMapDeferred.h:56
std::string getType() const final override
Returns type of the deferred object.
Definition objectMapDeferred.h:38
void addVariable(const std::string &name, ObjectMap *obj) final override
For the Deferred Build, the only variable that gets added will be the "real" ObjectMap.
Definition objectMapDeferred.h:65
void * getAddr() const final override
Returns nullptr since there is no underlying object being represented.
Definition objectMapDeferred.h:46
void activate_callback() final override
Function that will get called when this object is selected.
Definition objectMapDeferred.h:93
Base class for objects created by the serializer mapping mode used to map the variables for objects.
Definition objectMap.h:189
ObjectMap()=default
Default constructor primarily used for the "top" object in the hierarchy.
static std::string demangle_name(const char *name)
Static function to demangle type names returned from typeid(T).name().
Definition objectMap.cc:152
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43