SST 15.0
Structural Simulation Toolkit
linkMap.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_LINKMAP_H
13#define SST_CORE_LINKMAP_H
14
15#include "sst/core/component.h"
16#include "sst/core/link.h"
17#include "sst/core/sst_types.h"
18
19#include <map>
20#include <string>
21#include <vector>
22
23namespace SST {
24
25/**
26 * Maps port names to the Links that are connected to it
27 */
28class LinkMap
29{
30
31private:
32 std::map<std::string, Link*> linkMap;
33 // const std::vector<std::string> * allowedPorts;
34 std::vector<std::string> selfPorts;
35
36 friend class SST::Core::Serialization::serialize_impl<LinkMap*>;
37
38 void serialize_order(SST::Core::Serialization::serializer& ser)
39 {
40 SST_SER(linkMap);
41 SST_SER(selfPorts);
42 }
43
44
45 // bool checkPort(const char *def, const char *offered) const
46 // {
47 // const char * x = def;
48 // const char * y = offered;
49
50 // /* Special case. Name of '*' matches everything */
51 // if ( *x == '*' && *(x+1) == '\0' ) return true;
52
53 // do {
54 // if ( *x == '%' && (*(x+1) == '(' || *(x+1) == 'd') ) {
55 // // We have a %d or %(var)d to eat
56 // x++;
57 // if ( *x == '(' ) {
58 // while ( *x && (*x != ')') ) x++;
59 // x++; /* *x should now == 'd' */
60 // }
61 // if ( *x != 'd') /* Malformed string. Fail all the things */
62 // return false;
63 // x++; /* Finish eating the variable */
64 // /* Now, eat the corresponding digits of y */
65 // while ( *y && isdigit(*y) ) y++;
66 // }
67 // if ( *x != *y ) return false;
68 // if ( *x == '\0' ) return true;
69 // x++;
70 // y++;
71 // } while ( *x && *y );
72 // if ( *x != *y ) return false; // aka, both nullptr
73 // return true;
74 // }
75
76 // bool checkPort(const std::string& name) const
77 // {
78 // // First check to see if this is a self port
79 // for ( std::vector<std::string>::const_iterator i = selfPorts.begin() ; i != selfPorts.end() ; ++i ) {
80 // /* Compare name with stored name, which may have wildcards */
81 // // if ( checkPort(i->c_str(), x) ) {
82 // if ( name == *i ) {
83 // return true;
84 // }
85 // }
86
87 // // If no a self port, check against info in library manifest
88 // Component::isValidPortForComponent(
89 // const char *x = name.c_str();
90 // bool found = false;
91 // if ( nullptr != allowedPorts ) {
92 // for ( std::vector<std::string>::const_iterator i = allowedPorts->begin() ; i != allowedPorts->end() ; ++i
93 // ) {
94 // /* Compare name with stored name, which may have wildcards */
95 // if ( checkPort(i->c_str(), x) ) {
96 // found = true;
97 // break;
98 // }
99 // }
100 // }
101 // return found;
102 // }
103
104 // bool checkPort(const std::string& name) const
105 // {
106 // const char *x = name.c_str();
107 // bool found = false;
108 // if ( nullptr != allowedPorts ) {
109 // for ( std::vector<std::string>::const_iterator i = allowedPorts->begin() ; i != allowedPorts->end() ; ++i
110 // ) {
111 // /* Compare name with stored name, which may have wildcards */
112 // if ( checkPort(i->c_str(), x) ) {
113 // found = true;
114 // break;
115 // }
116 // }
117 // }
118
119 // if ( !found ) { // Check self ports
120 // for ( std::vector<std::string>::const_iterator i = selfPorts.begin() ; i != selfPorts.end() ; ++i ) {
121 // /* Compare name with stored name, which may have wildcards */
122 // // if ( checkPort(i->c_str(), x) ) {
123 // if ( name == *i ) {
124 // found = true;
125 // break;
126 // }
127 // }
128 // }
129
130 // return found;
131 // }
132
133public:
134 LinkMap() /*: allowedPorts(nullptr)*/ {}
135
136 ~LinkMap()
137 {
138 // Delete all the links in the map
139 for ( std::map<std::string, Link*>::iterator it = linkMap.begin(); it != linkMap.end(); ++it ) {
140 delete it->second;
141 }
142 linkMap.clear();
143 }
144
145 // /**
146 // * Set the list of allowed port names from the ElementInfoPort
147 // */
148 // void setAllowedPorts(const std::vector<std::string> *p)
149 // {
150 // allowedPorts = p;
151 // }
152
153 /**
154 * Add a port name to the list of allowed ports.
155 * Used by SelfLinks, as these are undocumented.
156 */
157 void addSelfPort(const std::string& name) { selfPorts.push_back(name); }
158
159 bool isSelfPort(const std::string& name) const
160 {
161 for ( std::vector<std::string>::const_iterator i = selfPorts.begin(); i != selfPorts.end(); ++i ) {
162 /* Compare name with stored name, which may have wildcards */
163 // if ( checkPort(i->c_str(), x) ) {
164 if ( name == *i ) {
165 return true;
166 }
167 }
168 return false;
169 }
170
171 /** Inserts a new pair of name and link into the map */
172 void insertLink(const std::string& name, Link* link) { linkMap.insert(std::pair<std::string, Link*>(name, link)); }
173
174 void removeLink(const std::string& name) { linkMap.erase(name); }
175
176 /** Returns a Link pointer for a given name */
177 Link* getLink(const std::string& name)
178 {
179
180 // if ( !checkPort(name) ) {
181 // #ifdef USE_PARAM_WARNINGS
182 // std::cerr << "Warning: Using undocumented port '" << name << "'." << std::endl;
183 // #endif
184 // }
185 std::map<std::string, Link*>::iterator it = linkMap.find(name);
186 if ( it == linkMap.end() )
187 return nullptr;
188 else
189 return it->second;
190 }
191
192 /**
193 Checks to see if LinkMap is empty.
194 @return True if Link map is empty, false otherwise
195 */
196 bool empty() { return linkMap.empty(); }
197
198 // FIXME: Kludge for now, fix later. Need to make LinkMap look
199 // like a regular map instead.
200 /** Return a reference to the internal map */
201 std::map<std::string, Link*>& getLinkMap() { return linkMap; }
202};
203
204} // namespace SST
205
206#endif // SST_CORE_LINKMAP_H
Base serialize class.
Definition serialize.h:110
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
void addSelfPort(const std::string &name)
Set the list of allowed port names from the ElementInfoPort.
Definition linkMap.h:157
std::map< std::string, Link * > & getLinkMap()
Return a reference to the internal map.
Definition linkMap.h:201
Link * getLink(const std::string &name)
Returns a Link pointer for a given name.
Definition linkMap.h:177
void insertLink(const std::string &name, Link *link)
Inserts a new pair of name and link into the map.
Definition linkMap.h:172
bool empty()
Checks to see if LinkMap is empty.
Definition linkMap.h:196