/** * @file factory.hpp * @author Julian Iseringhausen * @date Tue Apr 5 20:40:41 2011 * * @brief Factory class that holds commands and arbitrary objects. * * */ #ifndef FACTORY_HPP_ #define FACTORY_HPP_ #include #include #include "base/object.hpp" namespace VMG { class MG; class Factory { public: virtual ~Factory(); void Register(Object* object); ///< Registers an object template T& RegisterObjectStorage(std::string id, const T& val); template T* RegisterObjectStorageArray(std::string id, const vmg_int& size); Object* Get(std::string id); ///< Returns an object. template T& GetObjectStorageVal(std::string id); template T* GetObjectStorageArray(std::string id); void Delete(std::string id); ///< Deletes an object void PrintAvailableObjects(); ///< Prints the name of all objects that have been registered to the factory. bool TestObject(std::string id) const; ///< Checks whether an object exists or not. private: friend class MG; Factory(); std::map object_map; }; template T& Factory::RegisterObjectStorage(std::string id, const T& val) { Object* object = new ObjectStorage(id, val); return object->Cast< ObjectStorage >()->Val(); } template T* Factory::RegisterObjectStorageArray(std::string id, const vmg_int& size) { Object* object = new ObjectStorageArray(id, size); return object->Cast< ObjectStorage >()->Val(); } template T& Factory::GetObjectStorageVal(std::string id) { return Get(id)->Cast< ObjectStorage >()->Val(); } template T* Factory::GetObjectStorageArray(std::string id) { return Get(id)->Cast< ObjectStorage >()->Val(); } } #endif /* FACTORY_HPP_ */