import-trace: alias-based (importer) registry
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
This commit is contained in:
16
src/core/util/AliasedRegisterable.hpp
Normal file
16
src/core/util/AliasedRegisterable.hpp
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef __UTIL_ALIASEDREGISTERABLE_H__
|
||||
#define __UTIL_ALIASEDREGISTERABLE_H__
|
||||
|
||||
#include <deque>
|
||||
#include <string>
|
||||
|
||||
namespace fail {
|
||||
|
||||
class AliasedRegisterable {
|
||||
public:
|
||||
virtual void getAliases(std::deque<std::string> *aliases) = 0;
|
||||
};
|
||||
|
||||
}; // end of namespace fail
|
||||
|
||||
#endif // __UTIL_ALIASEDREGISTERABLE_H__
|
||||
68
src/core/util/AliasedRegistry.cc
Normal file
68
src/core/util/AliasedRegistry.cc
Normal file
@ -0,0 +1,68 @@
|
||||
#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
|
||||
43
src/core/util/AliasedRegistry.hpp
Normal file
43
src/core/util/AliasedRegistry.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef __UTIL_ALIASEDREGISTRY_H__
|
||||
#define __UTIL_ALIASEDREGISTRY_H__
|
||||
|
||||
#include "util/AliasedRegisterable.hpp"
|
||||
#include <map>
|
||||
|
||||
namespace fail {
|
||||
|
||||
class AliasedRegistry {
|
||||
private:
|
||||
std::map<std::string, AliasedRegisterable*> m_registry;
|
||||
std::map<AliasedRegisterable*, std::string> m_primes;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Register given AliasedRegisterable by it's aliases
|
||||
*/
|
||||
bool add(AliasedRegisterable* obj);
|
||||
|
||||
/**
|
||||
* Get an AliasedRegisterable by an arbitrary alias
|
||||
*/
|
||||
AliasedRegisterable *get(std::string alias);
|
||||
|
||||
/**
|
||||
* Get the prime (i.e. first to be specified) alias for given object
|
||||
*/
|
||||
bool getPrimeAlias(AliasedRegisterable* target, std::string& alias);
|
||||
|
||||
/**
|
||||
* Get all registered prime aliases
|
||||
*/
|
||||
void getPrimeAliases(std::deque<std::string>& prime_aliases);
|
||||
|
||||
/**
|
||||
* Get prime aliases' names as CSV
|
||||
*/
|
||||
std::string getPrimeAliasesCSV();
|
||||
};
|
||||
|
||||
}; // end of namespace fail
|
||||
|
||||
#endif // __UTIL_ALIASEDREGISTRY_H__
|
||||
@ -29,6 +29,9 @@ set(SRCS
|
||||
SynchronizedQueue.hpp
|
||||
WallclockTimer.cc
|
||||
WallclockTimer.hpp
|
||||
AliasedRegistry.hpp
|
||||
AliasedRegistry.cc
|
||||
AliasedRegisterable.hpp
|
||||
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user