SST  15.1.0
StructuralSimulationToolkit
objectMapDeferred.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_OBJECTMAPDEFERRED_H
13 #define SST_CORE_SERIALIZATION_OBJECTMAPDEFERRED_H
14 
15 #include "sst/core/serialization/serializer.h"
16 
17 #include <map>
18 #include <string>
19 #include <vector>
20 
21 namespace SST::Core::Serialization {
22 
23 /**
24  ObjectMap version that will delay building the internal data
25  structures until the object is "selected". Also, once the object's
26  parent is selected, the internal data will be deleted.
27  */
28 template <typename T>
30 {
31 public:
32  /**
33  Returns type of the deferred object
34 
35  @return empty type of object
36  */
37  std::string getType() override { return type_; }
38 
39  /**
40  Returns nullptr since there is no underlying object being
41  represented
42 
43  @return nullptr
44  */
45  void* getAddr() override { return static_cast<void*>(addr_); }
46 
47 
48  /**
49  Get the list of child variables contained in this ObjectMap
50 
51  @return Reference to map containing ObjectMaps for this
52  ObjectMap's child variables. pair.first is the name of the
53  variable in the context of this object.
54  */
55  const std::multimap<std::string, ObjectMap*>& getVariables() override { return obj_->getVariables(); }
56 
57  /**
58  For the Deferred Build, the only variable that gets added will
59  be the "real" ObjectMap.
60 
61  @param name Name of the object
62  @param obj ObjectMap to add as a variable
63  */
64  void addVariable(const std::string& name, ObjectMap* obj) override
65  {
66  // This should be the real ObjectMap for the class we are
67  // representing. We will check it by making sure the name
68  // matches what is passed in in the activate_callback()
69  // function.
70  if ( name == "!proxy!" ) {
71  obj_ = obj;
72  }
73  else {
74  printf("WARNING:: ObjectMapDeferred not built properly. No mapping will be available\n");
75  }
76  }
77 
78  ObjectMapDeferred(T* addr, const std::string& type) :
79  ObjectMap(),
80  addr_(addr),
81  type_(demangle_name(type.c_str()))
82  {}
83 
84  ~ObjectMapDeferred() override { delete obj_; }
85 
86 protected:
87  void activate_callback() override
88  {
89  // On activate, we need to create the ObjectMap data structure
90  if ( obj_ != nullptr ) return;
91 
93  ser.enable_pointer_tracking();
94  ser.start_mapping(this);
95 
96  SST_SER_NAME(addr_, "!proxy!");
97  }
98 
99  void deactivate_callback() override
100  {
101  // On deactivate, need to delete obj_;
102  obj_->decRefCount();
103  obj_ = nullptr;
104  }
105 
106 
107 private:
108  /**
109  ObjectMap for the real data
110  */
111  ObjectMap* obj_ = nullptr;
112 
113  /**
114  Object this ObjectMap represents
115  */
116  T* addr_ = nullptr;
117 
118  /**
119  Type of the variable as given by the demangled version of
120  typeif<T>.name() for the type.
121  */
122  std::string type_ = "";
123 };
124 
125 } // namespace SST::Core::Serialization
126 
127 #endif // SST_CORE_SERIALIZATION_OBJECTMAPDEFERRED_H
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
void activate_callback() override
Function that will get called when this object is selected.
Definition: objectMapDeferred.h:87
const std::multimap< std::string, ObjectMap * > & getVariables() override
Get the list of child variables contained in this ObjectMap.
Definition: objectMapDeferred.h:55
void deactivate_callback() override
Function that will get called when this object is deactivated (i.e selectParent() is called) ...
Definition: objectMapDeferred.h:99
ObjectMap version that will delay building the internal data structures until the object is "selected...
Definition: objectMapDeferred.h:29
Base class for objects created by the serializer mapping mode used to map the variables for objects...
Definition: objectMap.h:158
std::string getType() override
Returns type of the deferred object.
Definition: objectMapDeferred.h:37
void addVariable(const std::string &name, ObjectMap *obj) override
For the Deferred Build, the only variable that gets added will be the "real" ObjectMap.
Definition: objectMapDeferred.h:64
virtual const std::multimap< std::string, ObjectMap * > & getVariables()
Get the list of child variables contained in this ObjectMap.
Definition: objectMap.h:300
void * getAddr() override
Returns nullptr since there is no underlying object being represented.
Definition: objectMapDeferred.h:45
static std::string demangle_name(const char *name)
Static function to demangle type names returned from typeid(T).name()
Definition: objectMap.cc:161
void decRefCount()
Decrement the reference counter for this ObjectMap.
Definition: objectMap.h:316