OpenASIP  2.0
HDBRegistry.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file HDBRegistry.cc
26  *
27  * Implementation of HDBRegistry class.
28  *
29  * @author Pekka Jääskeläinen 2005 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 #include "HDBRegistry.hh"
33 #include "AssocTools.hh"
34 #include "CachedHDBManager.hh"
35 #include "Application.hh"
36 #include "Environment.hh"
37 #include "FileSystem.hh"
38 
39 namespace HDB {
40 
41 HDBRegistry* HDBRegistry::instance_ = NULL;
42 
43 /**
44  * Constructor.
45  */
47 }
48 
49 /**
50  * Destructor.
51  */
54 }
55 
56 /**
57  * Creates and returns an instance of HDB registry.
58  *
59  * @return An instance of HDB registry.
60  */
63 
64  if (instance_ == NULL) {
65  instance_ = new HDBRegistry();
66  }
67  return *instance_;
68 }
69 
70 /**
71  * Returns the HDB at given path.
72  *
73  * In case the HDB is not found in registry, tries to open it.
74  *
75  * @param fileName File name of the HDB.
76  * @return The HDB associated with the file name.
77  * @exception Exception In case there was a problem while opening the HDB.
78  */
80 HDBRegistry::hdb(const std::string fileName) {
83  return *registry_[FileSystem::absolutePathOf(fileName)];
84  }
86  FileSystem::absolutePathOf(fileName));
87 
88  registry_[FileSystem::absolutePathOf(fileName)] = manager;
89  return *manager;
90 }
91 
92 /**
93  * Adds HDBManager in to the registry.
94  *
95  * Takes responsibility of deleting the given HDBManager.
96  *
97  * @param hdb The HDBManager to be added in to the registry.
98  */
99 void
101 
103  registry_, FileSystem::absolutePathOf(hdbManager->fileName()))) {
104 
105  delete hdbManager;
106  hdbManager = NULL;
107  return;
108  }
109  registry_[FileSystem::absolutePathOf(hdbManager->fileName())] = hdbManager;
110 }
111 
112 /**
113  * Returns true if the registry contains a HDBManager of the given HDB file.
114  *
115  * @param hdbFile The HDB file managed by the HDBManager.
116  * @return True if the registry contains a HDBManager of the given HDB file.
117  */
118 bool
119 HDBRegistry::hasHDB(const std::string& hdbFile) {
120 
123  return true;
124  } else {
125  return false;
126  }
127 }
128 
129 /**
130  * Returns the total number of stored HDBs.
131  *
132  * @return Count of stored HDBs.
133  */
134 int
136 
137  return registry_.size();
138 }
139 
140 /*
141  * Searches and loads all HDB:s found in Environment paths.
142  *
143  * Stores erros caught in loading HDBs.
144  */
145 void
147 
148  // check every HDB path exists, and if doesn't, remove from registry
150 
151  const std::vector<std::string> hdbPaths = Environment::hdbPaths();
152  std::vector<std::string>::const_iterator hdbIter = hdbPaths.begin();
153  for(; hdbIter != hdbPaths.end(); hdbIter++) {
154  if (!FileSystem::fileExists(*hdbIter) ||
155  !FileSystem::fileIsDirectory(*hdbIter) ||
156  !FileSystem::fileIsReadable(*hdbIter)) {
157  // Directory doesn't exist or can't be read.
158  continue;
159  }
160  std::vector<std::string> dir =
162  std::vector<std::string>::const_iterator dirIter = dir.begin();
163  for (; dirIter != dir.end(); dirIter++) {
164  std::string file = (*dirIter);
165  if (file.rfind(".hdb", file.size()) != std::string::npos &&
166  file.rfind(".hdb", file.size()) == (file.size() - 4)) {
167  if (!AssocTools::containsKey(registry_, file)) {
168  try {
169  CachedHDBManager* manager =
172 
173  registry_[FileSystem::absolutePathOf(file)] = manager;
174  } catch (IOException& e) {
175  std::string errorMessage = "Error in '" + file +
176  "': " + e.errorMessage() + ".";
177  errorMessages_.push_back(errorMessage);
178  }
179  }
180  }
181  }
182  }
183 }
184 
185 /**
186  * Returns the HDB from given index.
187  *
188  * @return The HDB from the given index.
189  * @exception OutOfRange Is thrown if index is bigger than the HDB count.
190  */
192 HDBRegistry::hdb(unsigned int index) {
193  if (index > (registry_.size() - 1)) {
194  throw OutOfRange(__FILE__, __LINE__,
195  "HDBRegistry::hdb(unsigned int)");
196  }
197  std::map<const std::string, CachedHDBManager*>::const_iterator
198  mapIterator = registry_.begin();
199  for (unsigned int counter = 0; mapIterator != registry_.end();
200  mapIterator++) {
201  if (counter == index) {
202  break;
203  } else {
204  counter++;
205  }
206  }
207  return *(*mapIterator).second;
208 }
209 
210 /**
211  * Returns the full path of the HDB with given index.
212  *
213  * @return The HDB file path.
214  * @exception OutOfRange Is thrown if index is out of range.
215  */
216 std::string
217 HDBRegistry::hdbPath(unsigned index) {
218  if (index > (registry_.size() - 1)) {
219  throw OutOfRange(__FILE__, __LINE__,
220  "HDBRegistry::hdb(unsigned int)");
221  }
222  std::map<const std::string, CachedHDBManager*>::const_iterator iter =
223  registry_.begin();
224 
225  for (unsigned c = 0; iter != registry_.end(); iter++) {
226  if (c == index) return (*iter).first;
227  c++;
228  }
229  assert(false);
230 }
231 
232 /**
233  * Returns the number of error messages created during HDB loading.
234  *
235  * @return The number of error messages created during HDB loading.
236  */
237 int
239 
240  return errorMessages_.size();
241 }
242 
243 /**
244  * Returns the error message from given index.
245  *
246  * @return The error message in given index.
247  * @exception OutOfRange Is thrown if index is bigger than the HDB
248  * error count.
249  */
250 std::string
251 HDBRegistry::hdbErrorMessage(unsigned int index) {
252  if (index > (errorMessages_.size() - 1)) {
253  throw OutOfRange(__FILE__, __LINE__,
254  "HDBRegistry::hdbErrorMessage(unsigned int)");
255  }
256  return errorMessages_[index];
257 }
258 
259 /**
260  * Removes nonexistent HDB files from registry.
261  */
262 void
264 
265  std::map<const std::string, CachedHDBManager*>::iterator it;
266  for (it = registry_.begin(); it != registry_.end(); ) {
267  if (!FileSystem::fileExists(it->first)) {
268  registry_.erase(it->first);
269  ++it;
270  } else {
271  ++it;
272  }
273  }
274 }
275 
276 }
HDB::HDBRegistry::errorMessages_
std::vector< std::string > errorMessages_
errors found during loading HDBs are strored in this vector
Definition: HDBRegistry.hh:69
FileSystem.hh
HDB
Definition: CostDatabase.hh:49
AssocTools::containsKey
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
OutOfRange
Definition: Exception.hh:320
HDB::HDBRegistry::HDBRegistry
HDBRegistry()
HDB registry must be created with instance() method.
Definition: HDBRegistry.cc:46
HDB::HDBRegistry::hdb
CachedHDBManager & hdb(const std::string fileName)
Definition: HDBRegistry.cc:80
HDB::HDBRegistry::hdbErrorMessage
std::string hdbErrorMessage(unsigned int index)
Definition: HDBRegistry.cc:251
FileSystem::absolutePathOf
static std::string absolutePathOf(const std::string &pathName)
Definition: FileSystem.cc:303
HDB::HDBRegistry::hasHDB
bool hasHDB(const std::string &hdbFile)
Definition: HDBRegistry.cc:119
HDB::CachedHDBManager
Definition: CachedHDBManager.hh:63
assert
#define assert(condition)
Definition: Application.hh:86
FileSystem::fileIsDirectory
static bool fileIsDirectory(const std::string fileName)
HDB::HDBRegistry
Definition: HDBRegistry.hh:46
Environment::hdbPaths
static std::vector< std::string > hdbPaths(bool libraryPathsOnly=false)
Definition: Environment.cc:683
Application.hh
Environment.hh
HDB::HDBRegistry::instance_
static HDBRegistry * instance_
Unique instance of the class.
Definition: HDBRegistry.hh:71
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
HDB::HDBRegistry::hdbCount
int hdbCount()
Definition: HDBRegistry.cc:135
HDB::HDBManager::fileName
std::string fileName() const
Definition: HDBManager.cc:612
CachedHDBManager.hh
HDB::HDBRegistry::hdbErrorCount
int hdbErrorCount()
Definition: HDBRegistry.cc:238
HDB::HDBRegistry::registry_
std::map< const std::string, CachedHDBManager * > registry_
all opened HDBs are stored in this map
Definition: HDBRegistry.hh:67
FileSystem::fileExists
static bool fileExists(const std::string fileName)
AssocTools.hh
HDB::HDBRegistry::addHDB
void addHDB(CachedHDBManager *hdbManager)
Definition: HDBRegistry.cc:100
HDB::HDBRegistry::~HDBRegistry
virtual ~HDBRegistry()
Definition: HDBRegistry.cc:52
HDB::HDBRegistry::loadFromSearchPaths
void loadFromSearchPaths()
Definition: HDBRegistry.cc:146
IOException
Definition: Exception.hh:130
FileSystem::fileIsReadable
static bool fileIsReadable(const std::string fileName)
HDBRegistry.hh
HDB::HDBRegistry::hdbPath
std::string hdbPath(unsigned int index)
Definition: HDBRegistry.cc:217
AssocTools::deleteAllValues
static void deleteAllValues(ContainerType &aMap)
FileSystem::directoryContents
static std::vector< std::string > directoryContents(const std::string &directory, const bool absolutePaths=true)
Definition: FileSystem.cc:600
HDB::HDBRegistry::instance
static HDBRegistry & instance()
Definition: HDBRegistry.cc:62
HDB::CachedHDBManager::instance
static CachedHDBManager & instance(const std::string &hdbFile)
Definition: CachedHDBManager.cc:89
HDB::HDBRegistry::removeDeadHDBPaths
void removeDeadHDBPaths()
Deletes nonexistent HDB paths from registry.
Definition: HDBRegistry.cc:263