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