I am a physicist working on the code that needs to be built, and then caches several different types of matrices. Then the matrices must be extracted, given the set of information that uniquely identifies them (the "key"). I am trying to find a flexible way to do this.
My current approach involves combining all the "key" information into one class:
class FilterKey{
public:
FilterKey(const char type, const X key1, const Y* key2...);
~FilterKey();
bool operator==(const FilterKey& rhs) const;
private:
char type; X mKey1; Y* mpKey2;
}
User code passes them to an interface class called "MatrixDirectory", created as a global variable (but not a singleton) that stores a map between types and caches:
class MatrixDirectory : private NonCopyable{
public:
void Clear();
void Filter(std::vector<double>& U, const FilterKey& key);
private:
std::map<char types, FilterCache* caches> mpFilterCaches;
};
When MatrixDirectory :: Filter encounters a new char type, it calls FilterCacheFactory, which builds the corresponding derived class from FilterCache based on char:
namespace FilterCacheFactory{
FilterCache* MakeFilterCache(char type) {
if '1'==type return new OneDFilterCache();
else if 'S'==type return new S2FilterCache();
else if 'B'==type return new B2FilterCache();
else REQUIRE(False, "Invalid filter type '" << type << "'!");
}
}
. FilterCaches , FilterKey.
, , , . , FilterKey. , MatrixDirectory FilterCaches, . , , , #include . ?