Last active
July 2, 2017 02:23
-
-
Save bysreg/222317ddc6c6e376d1ce170f264c2611 to your computer and use it in GitHub Desktop.
object_factory
This file contains 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
class ObjectFactory | |
{ | |
public: | |
using ObjectFactoryFunctionType = Object*(*)(); | |
using FactoryMapType = std::unordered_map<std::string, ObjectFactoryFunctionType>; | |
FactoryMapType factory_map; | |
Object* Create(const std::string& type) | |
{ | |
if (factory_map.find(type) == factory_map.end()) | |
{ | |
return new Object; | |
} | |
return factory_map[type](); | |
} | |
void Register(const std::string& type, ObjectFactoryFunctionType factory_function) | |
{ | |
factory_map[type] = factory_function; | |
} | |
static ObjectFactory& Instance() | |
{ | |
static ObjectFactory self; | |
return self; | |
} | |
}; | |
class ObjectFactoryRegistrator | |
{ | |
public: | |
ObjectFactoryRegistrator(const std::string& type, ObjectFactory::ObjectFactoryFunctionType factory_function) | |
{ | |
printf("register is called\n"); | |
ObjectFactory::Instance().Register(type, factory_function); | |
} | |
}; | |
#define CREATE_FUNCTION_NAME(ObjectType, ObjectString) Create ## _ ## ObjectType ## _ ## ObjectString | |
#define REGISTER(ObjectType, ObjectString)\ | |
static Object* CREATE_FUNCTION_NAME(ObjectType, ObjectString)()\ | |
{\ | |
/*the parameter of this function has to match Object's constructor parameter*/ \ | |
return new ObjectType;\ | |
}\ | |
static ObjectFactoryRegistrator registrator(# ObjectString, &CREATE_FUNCTION_NAME(ObjectType, ObjectString));\ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment