Factory.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "PreCompiled.h"
00027
00028 #ifndef _PreComp_
00029 # include <list>
00030 #endif
00031
00032
00033 #include "Factory.h"
00034 #include "Console.h"
00035
00036 using namespace Base;
00037
00038
00039 Factory::~Factory ()
00040 {
00041 for (std::map<const std::string, AbstractProducer*>::iterator pI = _mpcProducers.begin(); pI != _mpcProducers.end(); pI++)
00042 delete pI->second;
00043 }
00044
00045 void* Factory::Produce (const char *sClassName) const
00046 {
00047 std::map<const std::string, AbstractProducer*>::const_iterator pProd;
00048
00049 pProd = _mpcProducers.find(sClassName);
00050 if (pProd != _mpcProducers.end())
00051 return pProd->second->Produce();
00052 else
00053 return NULL;
00054 }
00055
00056 void Factory::AddProducer (const char *sClassName, AbstractProducer *pcProducer)
00057 {
00058 _mpcProducers[sClassName] = pcProducer;
00059 }
00060
00061 bool Factory::CanProduce(const char* sClassName) const
00062 {
00063 return (_mpcProducers.find(sClassName) != _mpcProducers.end());
00064 }
00065
00066 std::list<std::string> Factory::CanProduce() const
00067 {
00068 std::list<std::string> lObjects;
00069
00070 for (std::map<const std::string, AbstractProducer*>::const_iterator pI = _mpcProducers.begin(); pI != _mpcProducers.end(); pI++)
00071 {
00072 lObjects.push_back(pI->first);
00073 }
00074
00075 return lObjects;
00076 }
00077
00078
00079
00080 ScriptFactorySingleton* ScriptFactorySingleton::_pcSingleton = NULL;
00081
00082
00083
00084 ScriptFactorySingleton& ScriptFactorySingleton::Instance(void)
00085 {
00086 if (_pcSingleton == NULL)
00087 _pcSingleton = new ScriptFactorySingleton;
00088 return *_pcSingleton;
00089 }
00090
00091 void ScriptFactorySingleton::Destruct (void)
00092 {
00093 if (_pcSingleton != 0)
00094 delete _pcSingleton;
00095 _pcSingleton = 0;
00096 }
00097
00098 const char* ScriptFactorySingleton::ProduceScript (const char* sScriptName) const
00099 {
00100 const char* script = (const char*)Produce(sScriptName);
00101
00102 if ( !script )
00103 {
00104 #ifdef FC_DEBUG
00105 Console().Warning("\"%s\" is not registered\n", sScriptName);
00106 #endif
00107 return "";
00108 }
00109
00110 return script;
00111 }