/* ScummVM Tools * * ScummVM Tools is the legal property of its developers, whose * names are too numerous to list here. Please refer to the * COPYRIGHT file distributed with this source distribution. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef DEC_OBJECTFACTORY_H #define DEC_OBJECTFACTORY_H #include #include #include "common/scummsys.h" /** * Template function for creating an instance of Type. */ template BaseType *createObject() { return new Type(); } /** * Generic factory for a class and its subclasses. */ template class ObjectFactory { private: /** * Function pointer to the object creation function. */ typedef BaseType *(*CreateFunc)(); /** * Type used to store registered entries. */ typedef std::map RegistryMap; RegistryMap _registry; ///< Map from an identifier to a creation function. public: /** * Register a new entry. * * @param key The key to register the class under. */ template void addEntry(const KeyType &key) { _registry[key] = &createObject; } /** * Creates an instance of some registered class. * * @param key The key associated with the desired class. * @return NULL if the name is not registered, else an instance of the associated class. */ BaseType *create(const KeyType &key) const { typename RegistryMap::const_iterator entry = _registry.find(key); if (entry == _registry.end()) return NULL; return (entry->second)(); } }; #endif