20 #ifndef COIL_FACTORY_H 21 #define COIL_FACTORY_H 27 #include <coil/Singleton.h> 30 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) 31 # ifdef LIBRARY_EXPORTS 33 # define DLL_PLUGIN __declspec(dllexport) 35 # define EXTERN extern 36 # define DLL_PLUGIN __declspec(dllimport) 57 template <
class AbstractClass,
class ConcreteClass>
60 return new ConcreteClass();
74 template <
class AbstractClass,
class ConcreteClass>
77 if (obj == 0) {
return; }
78 ConcreteClass* tmp =
dynamic_cast<ConcreteClass*
>(obj);
79 if (tmp == 0) {
return; }
99 typename Identifier = std::string,
100 typename Compare = std::less<Identifier>,
101 typename Creator = AbstractClass* (*)(),
102 typename Destructor =
void (*)(AbstractClass*&)
109 typedef std::map<Identifier, FactoryEntry>
FactoryMap;
147 if (m_creators.count(
id) == 0) {
return false; }
172 std::vector<Identifier> idlist;
173 idlist.reserve(m_creators.size());
175 FactoryMapIt it(m_creators.begin());
176 FactoryMapIt it_end(m_creators.end());
180 idlist.push_back(it->first);
221 if (creator == 0 || destructor == 0) {
return INVALID_ARG; }
223 FactoryEntry f(creator, destructor);
255 if (m_creators.count(
id) == 0) {
return NOT_FOUND; }
257 m_creators.erase(
id);
286 if (m_creators.count(
id) == 0) {
return 0; }
287 return m_creators[
id].creator_();
313 if (m_creators.count(
id) == 0) {
return; }
314 m_creators[
id].destructor_(obj);
338 FactoryMapIt it(m_creators.begin());
339 FactoryMapIt it_end(m_creators.end());
343 it->second.destructor_(obj);
392 : creator_(creator), destructor_(destructor)
418 typename Identifier = std::string,
419 typename Compare = std::less<Identifier>,
420 typename Creator = AbstractClass* (*)(),
421 typename Destructor =
void (*)(AbstractClass*&)
472 #endif // COIL_FACTORY_H
bool hasFactory(const Identifier &id)
Factory presence check.
FactoryMap::iterator FactoryMapIt
AbstractClass * Creator()
Creator template.
ReturnCode removeFactory(const Identifier &id)
Remove factory.
ReturnCode addFactory(const Identifier &id, Creator creator, Destructor destructor)
Add factory.
GlobalFactory template class.
std::vector< Identifier > getIdentifiers()
Get factory ID list.
Singleton template class.
void deleteObject(const Identifier &id, AbstractClass *&obj)
Delete factory object.
AbstractClass * createObject(const Identifier &id)
Create factory object.
std::map< Identifier, FactoryEntry > FactoryMap
void Destructor(AbstractClass *&obj)
Destructor template.
~GlobalFactory()
Destructor.
FactoryEntry(Creator creator, Destructor destructor)
Constructor.
void deleteObject(AbstractClass *&obj)
Delete factory object.
GlobalFactory()
Constructor.
Common Object Interface Layer.