SST  10.0.0
StructuralSimulationToolkit
linkMap.h
1 // Copyright 2009-2020 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-2020, 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/sst_types.h"
16 
17 #include <string>
18 #include <map>
19 
20 #include "sst/core/component.h"
21 #include "sst/core/link.h"
22 
23 namespace SST {
24 
25 /**
26  * Maps port names to the Links that are connected to it
27  */
28 class LinkMap {
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  // /* Compare name with stored name, which may have wildcards */
84  // if ( checkPort(i->c_str(), x) ) {
85  // found = true;
86  // break;
87  // }
88  // }
89  // }
90  // return found;
91  // }
92 
93  // bool checkPort(const std::string& name) const
94  // {
95  // const char *x = name.c_str();
96  // bool found = false;
97  // if ( nullptr != allowedPorts ) {
98  // for ( std::vector<std::string>::const_iterator i = allowedPorts->begin() ; i != allowedPorts->end() ; ++i ) {
99  // /* Compare name with stored name, which may have wildcards */
100  // if ( checkPort(i->c_str(), x) ) {
101  // found = true;
102  // break;
103  // }
104  // }
105  // }
106 
107  // if ( !found ) { // Check self ports
108  // for ( std::vector<std::string>::const_iterator i = selfPorts.begin() ; i != selfPorts.end() ; ++i ) {
109  // /* Compare name with stored name, which may have wildcards */
110  // // if ( checkPort(i->c_str(), x) ) {
111  // if ( name == *i ) {
112  // found = true;
113  // break;
114  // }
115  // }
116  // }
117 
118  // return found;
119  // }
120 
121 public:
122  LinkMap() /*: allowedPorts(nullptr)*/ {}
123  ~LinkMap() {
124  // Delete all the links in the map
125  for ( std::map<std::string,Link*>::iterator it = linkMap.begin(); it != linkMap.end(); ++it ) {
126  delete it->second;
127  }
128  linkMap.clear();
129  }
130 
131  // /**
132  // * Set the list of allowed port names from the ElementInfoPort
133  // */
134  // void setAllowedPorts(const std::vector<std::string> *p)
135  // {
136  // allowedPorts = p;
137  // }
138 
139 
140 
141  /**
142  * Add a port name to the list of allowed ports.
143  * Used by SelfLinks, as these are undocumented.
144  */
145  void addSelfPort(const std::string& name)
146  {
147  selfPorts.push_back(name);
148  }
149 
150  bool isSelfPort(const std::string& name) const {
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 ) {
155  return true;
156  }
157  }
158  return false;
159  }
160 
161  /** Inserts a new pair of name and link into the map */
162  void insertLink(const std::string& name, Link* link) {
163  linkMap.insert(std::pair<std::string,Link*>(name,link));
164  }
165 
166  void removeLink(const std::string& name) {
167  linkMap.erase(name);
168  }
169 
170  /** Returns a Link pointer for a given name */
171  Link* getLink(const std::string& name) {
172 // if ( !checkPort(name) ) {
173 // #ifdef USE_PARAM_WARNINGS
174 // std::cerr << "Warning: Using undocumented port '" << name << "'." << std::endl;
175 // #endif
176 // }
177  std::map<std::string,Link*>::iterator it = linkMap.find(name);
178  if ( it == linkMap.end() ) return nullptr;
179  else return it->second;
180  }
181 
182  /**
183  Checks to see if LinkMap is empty.
184  @return True if Link map is empty, false otherwise
185  */
186  bool empty() {
187  return linkMap.empty();
188  }
189 
190  // FIXME: Kludge for now, fix later. Need to make LinkMap look
191  // like a regular map instead.
192  /** Return a reference to the internal map */
193  std::map<std::string,Link*>& getLinkMap() {
194  return linkMap;
195  }
196 
197 };
198 
199 } // namespace SST
200 
201 #endif // SST_CORE_LINKMAP_H
Maps port names to the Links that are connected to it.
Definition: linkMap.h:28
void insertLink(const std::string &name, Link *link)
Inserts a new pair of name and link into the map.
Definition: linkMap.h:162
std::map< std::string, Link * > & getLinkMap()
Return a reference to the internal map.
Definition: linkMap.h:193
void addSelfPort(const std::string &name)
Set the list of allowed port names from the ElementInfoPort.
Definition: linkMap.h:145
bool empty()
Checks to see if LinkMap is empty.
Definition: linkMap.h:186
Link * getLink(const std::string &name)
Returns a Link pointer for a given name.
Definition: linkMap.h:171