This change implements a generic registry in order to clean up import-trace's code - it's possible (and reasonable) to use the registry for pruners as well. Importer now extends AliasedRegisterable; all importers have been adapted to suit the interface/abstract methods. Each AliasedRegisterable should have at least one alias (the class' name is a sensible choice) but can have several. The first specified alias is the class' prime alias which can be used e.g. to list all registered objects. Change-Id: If6daa34edce35a3b0194e4ba67ed3b44b74a49b0
69 lines
1.7 KiB
C++
69 lines
1.7 KiB
C++
#include <iostream>
|
|
#include <deque>
|
|
#include <string>
|
|
|
|
#include "util/AliasedRegistry.hpp"
|
|
#include "util/AliasedRegisterable.hpp"
|
|
|
|
namespace fail {
|
|
|
|
bool AliasedRegistry::add(AliasedRegisterable *obj) {
|
|
std::deque<std::string> aliases;
|
|
obj->getAliases(&aliases);
|
|
|
|
bool inserted = false;
|
|
for (std::deque<std::string>::iterator it = aliases.begin(); it != aliases.end(); ++it) {
|
|
if (m_registry.find(*it) == m_registry.end()) {
|
|
if (!inserted) {
|
|
m_primes[obj] = *it;
|
|
}
|
|
m_registry[*it] = obj;
|
|
inserted = true;
|
|
} else {
|
|
#ifndef __puma
|
|
std::cerr << "AliasedRegistry: alias '" << *it << "' already exists!" << std::endl;
|
|
#endif
|
|
}
|
|
}
|
|
return inserted;
|
|
}
|
|
|
|
AliasedRegisterable *AliasedRegistry::get(std::string key) {
|
|
std::map<std::string, AliasedRegisterable*>::iterator it = m_registry.find(key);
|
|
if (it != m_registry.end()) {
|
|
return it->second;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
bool AliasedRegistry::getPrimeAlias(AliasedRegisterable *obj, std::string& alias) {
|
|
std::map<AliasedRegisterable*, std::string>::iterator it = m_primes.find(obj);
|
|
if (it != m_primes.end()) {
|
|
alias = it->second;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void AliasedRegistry::getPrimeAliases(std::deque<std::string>& aliases) {
|
|
std::map<AliasedRegisterable*, std::string>::iterator it = m_primes.begin();
|
|
for (;it != m_primes.end(); ++it) {
|
|
aliases.push_back(it->second);
|
|
}
|
|
}
|
|
|
|
std::string AliasedRegistry::getPrimeAliasesCSV(){
|
|
std::string csv = "";
|
|
std::deque<std::string> primes;
|
|
getPrimeAliases(primes);
|
|
for (std::deque<std::string>::iterator it = primes.begin(); it != primes.end(); ++it) {
|
|
csv += *it + ", ";
|
|
}
|
|
csv.resize(csv.size()-2);
|
|
return csv;
|
|
}
|
|
|
|
}; // end of namespace fail
|