torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConfigMap.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the ConfigMap class.
18 
19 #ifndef TORC_PHYSICAL_CONFIGMAP_HPP
20 #define TORC_PHYSICAL_CONFIGMAP_HPP
21 
22 #include "torc/physical/Config.hpp"
23 #include <map>
24 
25 namespace torc {
26 namespace physical {
27 
28  /// \brief Configuration setting map.
29  /// \details A configuration map is a collection of {setting:name:value} triplets, used to
30  /// represent additional information for netlist design elements. Every physical netlist
31  /// element that can be configured inherits from this class: Design, Module, Instance, Net.
32  /// \details Special Xilinx settings beginning with underscores are permitted to exist multiple
33  /// times in the same ConfigMap. This is consistent with XDL usage, particularly in the
34  /// case of XDL design statements.
35  /// \note The name mentioned here is a user-specified name stemming from the design. It is not
36  /// the configuration setting name. For example, in configuration "DFF:blink:#FF", "DFF"
37  /// is the specified setting, "blink" is the name that the design assigns to the
38  /// corresponding resource, and "#FF" is the value to which "DFF" is set.
39  class ConfigMap : private std::multimap<std::string, Config> {
40  protected:
41  // types
42  /// \brief Imported type name.
44  /// \brief Convenience typedef to represent our superclass.
45  typedef std::multimap<std::string, Config> super;
46  // members
47  /// \brief Sequence index to use for the next configuration added to this map.
49  public:
50  // types
51  /// \brief Constant iterator to {setting,Config} pairs.
53  // constructors
54  /// \brief Null constructor.
56  // iterators
57  /// \brief Returns the begin constant iterator for configurations.
58  const_iterator configBegin(void) const { return super::begin(); }
59  /// \brief Returns the end constant iterator for configurations.
60  const_iterator configEnd(void) const { return super::end(); }
61  // functions
62  /// \brief Returns the number of configurations in the map.
63  size_t getConfigCount(void) const { return super::size(); }
64  /// \brief Returns true if the configuration map is empty.
65  bool configIsEmpty(void) const { return super::empty(); }
66  /// \brief Clears the configuration map.
67  void clearConfig(void) { ConfigMap::clear(); }
68  /// \brief Returns true if the specified setting exists in the map.
69  bool hasConfig(const string& inSetting) const {
70  /// \todo Acquire mutex.
71  const_iterator result = find(inSetting);
72  return result != end();
73  /// \todo Release mutex.
74  }
75  /// \brief Looks up the specified setting in the map.
76  /// \param inSetting The setting to query.
77  /// \param outConfig Reference to a configuration to be populated if the setting exists in
78  /// the map. Default values are used if the setting does not exist.
79  /// \note If this is a special setting with multiple configuration entries, only the first
80  /// one will be placed in outConfig.
81  /// \returns true if the settings exists in the map, or false otherwise.
82  bool getConfig(const string& inSetting, Config& outConfig) {
83  // employ friendship status to reach in and reference the config name and value members
84  return getConfig(inSetting, outConfig.mName, outConfig.mValue);
85  }
86  /// \brief Looks up the specified setting in the map.
87  /// \param inSetting The setting to query.
88  /// \param outName Reference to a string to accept the configuration name, or the default
89  /// name if the setting does not exist.
90  /// \param outValue Reference to a string to accept the configuration value, or the default
91  /// value if the setting does not exist.
92  /// \note If this is a special setting with multiple configuration entries, only the first
93  /// one will be placed in outName and outValue.
94  /// \returns true if the settings exists in the map, or false otherwise.
95  bool getConfig(const string& inSetting, string& outName, string& outValue) {
96  /// \todo Acquire mutex.
97  iterator result = find(inSetting);
98  if(result == end()) {
99  // if the key doesn't exist, return default values
100  outName = Config::sConfigDefaultName;
101  outValue = Config::sConfigDefaultValue;
102  return false;
103  } else {
104  // otherwise return the config data
105  Config& config = result->second;
106  outName = config.getName();
107  outValue = config.getValue();
108  return true;
109  }
110  /// \todo Release mutex.
111  }
112  /// \brief Sets the configuration for the given setting.
113  /// \details If this is a regular setting, then any existing configuration for the setting
114  /// will be replaced, but if this is a special setting for which multiple
115  /// configurations are allowed, it will be added to the map alongside the existing
116  /// configurations.
117  /// \param inSetting The setting of interest.
118  /// \param inConfig The configuration to set.
119  void setConfig(const string& inSetting, const Config& inConfig) {
120  setConfig(inSetting, inConfig.getName(), inConfig.getValue());
121  }
122  /// \brief Sets the configuration for the given setting.
123  /// \details If this is a regular setting, then any existing configuration for the setting
124  /// will be replaced, but if this is a special setting for which multiple
125  /// configurations are allowed, it will be added to the map alongside the existing
126  /// configurations.
127  /// \param inSetting The setting of interest.
128  /// \param inName The configuration name to set.
129  /// \param inValue The configuration value to set.
130  void setConfig(const string& inSetting, const string& inName, const string& inValue) {
131  /// \todo Acquire mutex.
132  // if duplicates are disallowed for this setting, erase any matching entries
133  if(!allowConfigDuplicates(inSetting)) {
134  std::pair<iterator, iterator> range = equal_range(inSetting);
135  if(range.first != range.second) erase(range.first, range.second);
136  }
137  // insert the config entry
138  insert(make_pair(inSetting, Config(inName, inValue, mNextSequenceIndex++)));
139  /// \todo Release mutex.
140  }
141  /// \brief Removes the named configuration.
142  /// \param inSetting The configuration to remove.
143  /// \returns true if the configuration was removed, or false if the configuration did not
144  /// exist.
145  bool removeConfig(const string& inSetting) {
146  /// \todo Acquire mutex.
147  iterator e = end();
148  iterator result = super::find(inSetting);
149  if(result == e) return false;
150  erase(result);
151  /// \todo Release mutex.
152  return true;
153  }
154  /// \brief Merges the configurations from the given ConfigMap into this one.
155  /// \details For each setting, if the setting does not already exist in the map, it is
156  /// added. If the setting does exist, then the incoming configuration either replaces
157  /// the existing setting (in the case of regular settings), or is added to the map (in
158  /// the case of special settings for which multiple configurations are allowed).
159  void addConfigs(const ConfigMap& inConfigMap) {
160  if(inConfigMap.empty()) return;
161  /// \todo Acquire mutex.
162  const_iterator p = inConfigMap.begin();
163  const_iterator e = inConfigMap.end();
164  while(p != e) {
165  // look up the config information
166  const string& setting = p->first;
167  const Config& config = p->second;
168  // try to insert the setting into the map (while respecting our special semantics)
169  setConfig(setting, config);
170  p++;
171  }
172  /// \todo Release mutex.
173  }
174  /// \brief Returns true if multiple configurations are allowed for the given setting.
175  /// \details Special Xilinx settings prefixed with an underscore may have multiple
176  /// configurations in the map.
177  static bool allowConfigDuplicates(const string& inSetting) {
178  return inSetting.size() >= 1 && inSetting[0] == '_';
179  }
180  /// \brief Returns a range that encompasses all of the configurations for the given setting.
181  /// \returns An iterator pair that encompasses all configurations for the setting. Refer
182  /// to std::pair to determine how to extract the iterators.
183  std::pair<iterator, iterator> getMultiConfigValues(const string& inSetting)
184  { return equal_range(inSetting); }
185  /// \brief Returns the number of configurations for the given setting.
186  size_type getMultiConfigCount(const string& inSetting) { return count(inSetting); }
187  };
188 
189 } // namespace physical
190 } // namespace torc
191 
192 #endif // TORC_PHYSICAL_CONFIGMAP_HPP
boost::uint64_t SequenceIndex
Typedef for generic sequences.
void clearConfig(void)
Clears the configuration map.
Definition: ConfigMap.hpp:67
void setConfig(const string &inSetting, const Config &inConfig)
Sets the configuration for the given setting.
Definition: ConfigMap.hpp:119
void setConfig(const string &inSetting, const string &inName, const string &inValue)
Sets the configuration for the given setting.
Definition: ConfigMap.hpp:130
bool getConfig(const string &inSetting, Config &outConfig)
Looks up the specified setting in the map.
Definition: ConfigMap.hpp:82
Header for the Config class.
size_t getConfigCount(void) const
Returns the number of configurations in the map.
Definition: ConfigMap.hpp:63
string mName
The name of the object.
Definition: Named.hpp:43
static bool allowConfigDuplicates(const string &inSetting)
Returns true if multiple configurations are allowed for the given setting.
Definition: ConfigMap.hpp:177
void addConfigs(const ConfigMap &inConfigMap)
Merges the configurations from the given ConfigMap into this one.
Definition: ConfigMap.hpp:159
std::string string
std::string string
Imported type name.
Definition: ConfigMap.hpp:43
const_iterator const_iterator
Constant iterator to {setting,Config} pairs.
Definition: ConfigMap.hpp:52
bool getConfig(const string &inSetting, string &outName, string &outValue)
Looks up the specified setting in the map.
Definition: ConfigMap.hpp:95
bool removeConfig(const string &inSetting)
Removes the named configuration.
Definition: ConfigMap.hpp:145
const_iterator configBegin(void) const
Returns the begin constant iterator for configurations.
Definition: ConfigMap.hpp:58
size_type getMultiConfigCount(const string &inSetting)
Returns the number of configurations for the given setting.
Definition: ConfigMap.hpp:186
string mValue
The configuration setting value.
Definition: Config.hpp:57
Configuration. A {name:value} pair.
Definition: Config.hpp:39
bool hasConfig(const string &inSetting) const
Returns true if the specified setting exists in the map.
Definition: ConfigMap.hpp:69
const string & getName(void) const
Returns the object name.
Definition: Named.hpp:51
const string & getValue(void) const
Return the configuration value.
Definition: Config.hpp:86
static const char * sConfigDefaultValue
Default configuration value.
Definition: Config.hpp:53
std::multimap< std::string, Config > super
Convenience typedef to represent our superclass.
Definition: ConfigMap.hpp:45
ConfigMap(void)
Null constructor.
Definition: ConfigMap.hpp:55
SequenceIndex mNextSequenceIndex
Sequence index to use for the next configuration added to this map.
Definition: ConfigMap.hpp:48
Configuration setting map.
Definition: ConfigMap.hpp:39
bool configIsEmpty(void) const
Returns true if the configuration map is empty.
Definition: ConfigMap.hpp:65
std::pair< iterator, iterator > getMultiConfigValues(const string &inSetting)
Returns a range that encompasses all of the configurations for the given setting. ...
Definition: ConfigMap.hpp:183
const_iterator configEnd(void) const
Returns the end constant iterator for configurations.
Definition: ConfigMap.hpp:60
static const char * sConfigDefaultName
Default configuration name.
Definition: Config.hpp:51