MyGUI  3.2.1
MyGUI_FactoryManager.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #ifndef __MYGUI_FACTORY_MANAGER_H__
00008 #define __MYGUI_FACTORY_MANAGER_H__
00009 
00010 #include "MyGUI_Prerequest.h"
00011 #include "MyGUI_Singleton.h"
00012 #include "MyGUI_IObject.h"
00013 #include "MyGUI_GenericFactory.h"
00014 
00015 namespace MyGUI
00016 {
00017 
00018     class MYGUI_EXPORT FactoryManager :
00019         public Singleton<FactoryManager>
00020     {
00021     public:
00022         FactoryManager();
00023 
00024         void initialise();
00025         void shutdown();
00026 
00027         typedef delegates::CDelegate1<IObject*&> Delegate;
00029         void registerFactory(const std::string& _category, const std::string& _type, Delegate::IDelegate* _delegate);
00031         void unregisterFactory(const std::string& _category, const std::string& _type);
00033         void unregisterFactory(const std::string& _category);
00034 
00036         bool isFactoryExist(const std::string& _category, const std::string& _type);
00037 
00039         template<typename Type>
00040         void registerFactory(const std::string& _category)
00041         {
00042             registerFactory(_category, Type::getClassTypeName(), GenericFactory<Type>::getFactory());
00043         }
00044 
00046         template<typename Type>
00047         void registerFactory(const std::string& _category, const std::string& _type)
00048         {
00049             registerFactory(_category, _type, GenericFactory<Type>::getFactory());
00050         }
00051 
00053         template<typename Type>
00054         void unregisterFactory(const std::string& _category)
00055         {
00056             unregisterFactory(_category, Type::getClassTypeName());
00057         }
00058 
00060         IObject* createObject(const std::string& _category, const std::string& _type);
00062         template<typename Type>
00063         Type* createObject(const std::string& _category)
00064         {
00065             IObject* item = createObject(_category, Type::getClassTypeName());
00066             if (item != nullptr)
00067                 return item->castType<Type>(false);
00068             return nullptr;
00069         }
00070 
00072         void destroyObject(IObject* _object);
00073 
00074     private:
00075         typedef std::map<std::string, Delegate> MapFactoryItem;
00076         typedef std::map<std::string, MapFactoryItem> MapRegisterFactoryItem;
00077         MapRegisterFactoryItem mRegisterFactoryItems;
00078 
00079         bool mIsInitialise;
00080     };
00081 
00082 } // namespace MyGUI
00083 
00084 #endif // __MYGUI_FACTORY_MANAGER_H__