-
-
Save matthewjberger/2c9c234df1e42484bc624c86da27969d to your computer and use it in GitHub Desktop.
Factory pattern using C++ templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
using namespace std; | |
class BaseClass{ | |
public: | |
virtual int funk() { | |
return 0; | |
} | |
}; | |
class DerivedClassA : public BaseClass | |
{ | |
private: | |
static short ID; | |
virtual int funk() { | |
return 42; | |
} | |
public: | |
DerivedClassA(){}; | |
virtual ~DerivedClassA(){}; | |
}; | |
class DerivedClassB : public BaseClass | |
{ | |
private: | |
static short ID; | |
virtual int funk() { | |
return 63; | |
} | |
public: | |
DerivedClassB(){}; | |
virtual ~DerivedClassB(){}; | |
}; | |
template<typename Type> BaseClass* createType() { | |
cout << "creating type \n"; | |
return new Type; | |
} | |
class TemplatedFactory | |
{ | |
public: | |
typedef BaseClass* (*ComponentFactoryFuncPtr)(); | |
typedef map<const char*, ComponentFactoryFuncPtr> map_type; | |
map_type m_Map; | |
public: | |
template<typename Type> | |
short AddType(const char* componentName){ | |
ComponentFactoryFuncPtr function = &createType<Type>; | |
m_Map.insert(std::make_pair(componentName, function)); | |
return 0; | |
}; | |
}; | |
int main(int argc, char ** argv) | |
{ | |
TemplatedFactory * factory_p = new TemplatedFactory(); | |
factory_p->AddType<DerivedClassA>("DerivedClassA"); | |
factory_p->AddType<DerivedClassB>("DerivedClassB"); | |
cout << "Done registering\n"; | |
TemplatedFactory::map_type::iterator iterA = factory_p->m_Map.find("DerivedClassA"); | |
TemplatedFactory::map_type::iterator iterB = factory_p->m_Map.find("DerivedClassB"); | |
cout << "DerivedClassA: " << iterA->second()->funk() << "\n"; | |
cout << "DerivedClassB: " << iterB->second()->funk() << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment