Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////
- /// switch case method
- //////////
- enum object_class
- {
- object_class_Light,
- object_class_Mesh,
- object_class_COUNT
- };
- shared_ptr<Object> makeInstance(object_class ClassEnum)
- {
- switch (ClassEnum)
- {
- case object_class_Light: return make_shared(new Light());
- case object_class_Mesh: return make_shared(new Mesh());
- }
- return nullptr; // invalid case, might want to assert here
- }
- //////////
- /// x-macro method
- //////////
- #define ExpandEnum(ClassName) object_class_##ClassName,
- #define ExpandCase(ClassName) case object_class_##ClassName: return make_shared(new ClassName());
- // to add new classes, just add them to this list by name
- #define Classes(ExpandMacro) \
- ExpandMacro(Light) \
- ExpandMacro(Mesh)
- enum object_class
- {
- Classes(ExpandEnum)
- object_class_COUNT
- };
- shared_ptr<Object> makeInstance(object_class ClassEnum)
- {
- switch (ClassEnum)
- {
- Classes(ExpandCase)
- }
- return nullptr; // invalid case, might want to assert here
- }
- //////////
- /// function pointer table method
- //////////
- enum object_class
- {
- object_class_Light,
- object_class_Mesh,
- object_class_COUNT
- };
- template<typename type>
- shared_ptr<Object> createClassTemplate()
- {
- return make_shared<type>();
- }
- typedef shared_ptr<Object> factory_function();
- factory_function CreateClassTable[object_class_COUNT]
- {
- createClassTemplate<Light>,
- createClassTemplate<Mesh>,
- // make sure the order matches the enum order - you could use the x-macro trick above to generate this in the correct order
- };
- shared_ptr<Object> makeInstance(object_class ClassEnum)
- {
- return CreateClassTable[ClassEnum]();
- }
Advertisement
Add Comment
Please, Sign In to add comment