17#ifndef NAME_FACTORY_GUARD
18#define NAME_FACTORY_GUARD
32template<
class AbstractProduct>
45 unique_ptr<AbstractProduct>
createNoThrow(
const string& name)
const;
49 unique_ptr<AbstractProduct>
create(
const string& name)
const;
61 typedef pair<string, FactoryFunction>
Pair;
69template<
class ConcreteProduct,
class AbstractProduct>
75template<
class AbstractProduct>
88template<
class AbstractProduct>
97template<
class AbstractProduct>
101 if (it->first == name)
103 return unique_ptr<AbstractProduct>();
106template<
class AbstractProduct>
108create(
const string& name)
const {
116template<
class AbstractProduct>
122template<
class AbstractProduct>
126 if (it->first.compare(0, prefix.size(), prefix) == 0)
127 names.push_back(it->first);
128 sort(names.begin(), names.end());
131template<
class AbstractProduct>
136template<
class AbstractProduct>
141template<
class ConcreteProduct,
class AbstractProduct>
143 struct HoldsFunction {
144 static unique_ptr<AbstractProduct> createConcreteProduct() {
145 return unique_ptr<AbstractProduct>(
new ConcreteProduct());
149 HoldsFunction::createConcreteProduct);
152template<
class AbstractProduct>
158template<
class AbstractProduct>
161 vector<string> names;
164 if (find(names.begin(), names.end(), prefix) != names.end()) {
166 names.push_back(prefix);
172 " has the prefix \"" + prefix +
"\".");
175 if (names.size() >= 2) {
177 " has prefix \"" + prefix +
"\":\n ";
178 for (
size_t name = 0; name < names.size(); ++name)
179 errorMsg +=
' ' + names[name];
183 ASSERT(names.size() == 1);
void product(Matrix &prod, const Matrix &a, const Matrix &b)
Sets prod to a * b.
void nameFactoryRegister(NameFactory< AbstractProduct > &factory)
Registers the string returned by ConcreteProduct::getStaticName() to a function that default-construc...
string getUniqueNameWithPrefix(const NameFactory< AbstractProduct > &factory, const string &prefix)
Returns the unique product name that has the indicated prefix, or return prefix itself if it is the a...
unique_ptr< AbstractProduct > createWithPrefix(const NameFactory< AbstractProduct > &factory, const string &prefix)
Creates the unique product that has the indicated prefix, or create the actual product that has name ...
A NameFactory takes a name and then creates an instance of a class that has been previously registere...
pair< string, FactoryFunction > Pair
NameFactory(const char *abstractName)
vector< Pair >::const_iterator const_iterator
string getAbstractProductName() const
const string _abstractName
bool empty() const
Returns true if no names have been registered.
unique_ptr< AbstractProduct > createNoThrow(const string &name) const
Calls the function registered to the parameter name and returns the result.
unique_ptr< AbstractProduct >(* FactoryFunction)()
unique_ptr< AbstractProduct > create(const string &name) const
Calls the function registered to the parameter name and returns the result.
void getNamesWithPrefix(const string &prefix, vector< string > &names) const
Inserts into names all registered names that have the indicated prefix in lexicographic increasing or...
void registerProduct(const string &name, FactoryFunction function)
void throwError(const string &errorMsg)