#ifndef COMMON_FACTORY_H #define COMMON_FACTORY_H #include #include template class Factory { public: Factory(void) { } virtual ~Factory() { } virtual C *create(void) const = 0; }; template class ConstructorFactory : public Factory { public: ConstructorFactory(void) { } ~ConstructorFactory() { } B *create(void) const { return (new C()); } }; template class ConstructorArgFactory : public Factory { A a_; public: ConstructorArgFactory(A a) : a_(a) { } ~ConstructorArgFactory() { } B *create(void) const { return (new C(a_)); } }; template class SubclassFactory : public Factory { Factory *factory_; public: SubclassFactory(Factory *factory) : factory_(factory) { } ~SubclassFactory() { delete factory_; factory_ = NULL; } B *create(void) const { return (factory_->create()); } }; template struct factory { Factory *operator() (void) const { return (new ConstructorFactory); } template Factory *operator() (T arg) const { return (new ConstructorArgFactory(arg)); } }; template class FactoryMap { typedef std::map *> map_type; map_type map_; public: FactoryMap(void) : map_() { } ~FactoryMap() { typename map_type::iterator it; while ((it = map_.begin()) != map_.end()) { delete it->second; map_.erase(it); } } C *create(const K& key) const { typename map_type::const_iterator it; it = map_.find(key); if (it == map_.end()) return (NULL); return (it->second->create()); } void enter(const K& key, Factory *factory) { typename map_type::iterator it; it = map_.find(key); if (it != map_.end()) { delete it->second; map_.erase(it); } map_[key] = factory; } template void enter(const K& key, Factory *factory) { enter(key, new SubclassFactory(factory)); } std::set keys(void) const { typename map_type::const_iterator it; std::set key_set; for (it = map_.begin(); it != map_.end(); ++it) key_set.insert(it->first); return (key_set); } }; #endif /* !COMMON_FACTORY_H */