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