SST  6.1.0
StructuralSimulationToolkit
linkMap.h
1 // Copyright 2009-2016 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2016, Sandia Corporation
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 NULL
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 ( NULL != 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 ( NULL != 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(NULL) {}
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  * Add a port name to the list of allowed ports.
141  * Used by SelfLinks, as these are undocumented.
142  */
143  void addSelfPort(std::string& name)
144  {
145  selfPorts.push_back(name);
146  }
147 
148  bool isSelfPort(const std::string& name) const {
149  for ( std::vector<std::string>::const_iterator i = selfPorts.begin() ; i != selfPorts.end() ; ++i ) {
150  /* Compare name with stored name, which may have wildcards */
151  // if ( checkPort(i->c_str(), x) ) {
152  if ( name == *i ) {
153  return true;
154  }
155  }
156  return false;
157  }
158 
159  /** Inserts a new pair of name and link into the map */
160  void insertLink(std::string name, Link* link) {
161  linkMap.insert(std::pair<std::string,Link*>(name,link));
162  }
163 
164  /** Returns a Link pointer for a given name */
165  Link* getLink(std::string name) {
166 // if ( !checkPort(name) ) {
167 // #ifdef USE_PARAM_WARNINGS
168 // std::cerr << "Warning: Using undocumented port '" << name << "'." << std::endl;
169 // #endif
170 // }
171  std::map<std::string,Link*>::iterator it = linkMap.find(name);
172  if ( it == linkMap.end() ) return NULL;
173  else return it->second;
174  }
175 
176  /**
177  Checks to see if LinkMap is empty.
178  @return True if Link map is empty, false otherwise
179  */
180  bool empty() {
181  return linkMap.empty();
182  }
183 
184  // FIXME: Cludge for now, fix later. Need to make LinkMap look
185  // like a regular map instead.
186  /** Return a reference to the internal map */
187  std::map<std::string,Link*>& getLinkMap() {
188  return linkMap;
189  }
190 
191 };
192 
193 } // namespace SST
194 
195 #endif // SST_CORE_LINKMAP_H
Maps port names to the Links that are connected to it.
Definition: linkMap.h:28
Definition: action.cc:17
std::map< std::string, Link * > & getLinkMap()
Return a reference to the internal map.
Definition: linkMap.h:187
Link * getLink(std::string name)
Returns a Link pointer for a given name.
Definition: linkMap.h:165
void addSelfPort(std::string &name)
Add a port name to the list of allowed ports.
Definition: linkMap.h:143
bool empty()
Checks to see if LinkMap is empty.
Definition: linkMap.h:180
void insertLink(std::string name, Link *link)
Inserts a new pair of name and link into the map.
Definition: linkMap.h:160
void setAllowedPorts(const std::vector< std::string > *p)
Set the list of allowed port names from the ElementInfoPort.
Definition: linkMap.h:134