//////////////////////////////////////////////////////////////////////// // // Factory allocator // // Author: Stan Seibert // // REVISION HISTORY: // //////////////////////////////////////////////////////////////////////// #ifndef __RAT_Factory__ #define __RAT_Factory__ #include #include namespace RAT { template class AllocBase { public: virtual T* New() = 0; }; template class Alloc : public AllocBase { public: virtual T* New() { return new TDerived; }; }; class FactoryUnknownID { public: FactoryUnknownID(const std::string &_id) { id = _id; }; std::string id; }; template class AllocTable : public std::map< std::string, AllocBase* > { }; template class Factory { public: T* New(const std::string &id) { if (table.count(id) == 0) throw FactoryUnknownID(id); else return table[id]->New(); }; void Register(const std::string &id, AllocBase *allocator) { table[id] = allocator; }; protected: AllocTable table; }; template class GlobalFactory { public: static T* New(const std::string &id) { return factory.New(id); }; static void Register(const std::string &id, AllocBase *allocator) { factory.Register(id, allocator); }; protected: static Factory factory; }; template Factory GlobalFactory::factory; } // namespace RAT #endif