52#ifndef UTIL_GENERICFACTORY_HPP
53#define UTIL_GENERICFACTORY_HPP
155 template<
class Object,
typename IdentType = std::string,
156 typename Creator = Object* (*)()>
160 using CreatorMapType = std::map<IdentType, Creator>;
167 if (instance_ == NULL) instance_ =
new FactoryType;
178 return creatorMap_.insert(CreatorMapType::value_type(
id, creator)).second;
189 return creatorMap_.erase(
id) == 1;
201 typename CreatorMapType::iterator i = creatorMap_.find(
id);
202 if (i != creatorMap_.end()) {
204 return (i->second)();
215 std::vector<IdentType> idents;
216 typename CreatorMapType::iterator i;
218 for (i = creatorMap_.begin(); i != creatorMap_.end(); ++i) {
219 idents.push_back(i->first);
226 static FactoryType* instance_;
227 CreatorMapType creatorMap_;
231 template<
typename O,
typename I,
typename C>
233 std::vector<I> idents;
234 std::vector<I>::iterator i;
238 o <<
"Avaliable type identifiers in this factory: " << std::endl;
239 for (i = idents.begin(); i != idents.end(); ++i) {
240 o << *i << std::endl;
247 template<
class Object,
typename IdentType,
typename Creator>
248 GenericFactory<Object, IdentType, Creator>*
249 GenericFactory<Object, IdentType, Creator>::instance_;
251#define DECLARE_CREATOR(abstractObject, concreteObject) \
252 inline abstractObject* create##concreteObject() { return new concreteObject; }
254#define REGISTER_CREATOR(factory, ident, concreteObject) \
255 const bool registered##concreteObject = \
256 factory::getInstance()->registerCreator(ident, create##concreteObject);
GenericFactory is a template based Object Factory Factory pattern is used to define an interface for ...
static FactoryType * getInstance()
Returns an instance of object factory.
bool unregisterCreator(const IdentType &id)
Unregisters the creator for the given type identifier.
Object * createObject(const IdentType &id)
Looks up the type identifier in the internal map.
bool registerCreator(const IdentType &id, Creator creator)
Registers a creator with a type identifier.
std::vector< IdentType > getIdents()
Returns all of the registed type identifiers.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.