I am making a simple messaging system. I have an Entity descriptor table connected to a factory to subclass Entity at runtime, and I would like to have it so that they can be created line by line:
EntityManager manager;
Now I know that I can do a simple switch statement, but I would like the system to be extensible, that is, when I (or other users of my system) want to create a new Entity subclass, I donβt have to go to the switch block and add it . I am currently using macros to create helper classes that I create statically, so the constructor adds an entry to the entity table. These classes also initialize entities and remove many templates from constructors.
//EHandle is a wrapper for Entity*. Currently std::shared_ptr<Entity> class GenericDesc { public: virtual ~GenericDesc() {} virtual EHandle makeEntity() const =0; }; namespace Descriptor { //Adds a descriptor to an internal map<string, GenericDesc*> void addEntityDescriptor(const std::string& type, GenericDesc& desc); EHandle newEntity(const std::string& type); //Factory method } //Add this to every entity class definition
The idea is that you create a BEGIN_ENTITY_TYPE (...) END_ENTITY_TYPE (...) condition, in which the ADD_ENTITY_x bit is in the middle. My question is whether there is a way to do this with smaller macros, which still minimizes the template and does not require changing any files outside the one defined by the Entity subclass. The template class may work, but I do not know how I would do ADD_ENTITY_INPUT / OUTPUT with the template class.
source share