properties - Maintainability of library in C++ -



properties - Maintainability of library in C++ -

i creating library in c++ intent of utilizing in future applications (games). decided on using entity-property design base of operations entity can extended attaching various properties it. properties derivatives of base of operations gameproperty class allows them stored in single vector of polymorphic pointers, fellow member of gameentity class. have entitymanager class creates specialized entities attaching right set of properties them, in accordance entitytemplate can loaded file.

here problem: want easy create , add together new properties suit needs of given project. each new property cannot extend gameproperty, needs seemlessly possible integratable entitymanager. entitymanager expects strings "graphicsproperty", "inputproperty", etc , selects property attach newly created entities based on that. how can construction property , entitymanager classes in such way typespecific code not need added entitymanager each new property write?

consider illustration below:

class gameproperty { virtual void update() = 0; }; class graphicsproperty : public gameproperty { void update(); }; class inputproperty : public gameproperty { void update(); }; class gameentity { public: void attachproperty(shared_ptr<gameproperty> newproperty) { properties.push_back(newproperty); }; vector<shared_ptr<gameproperty>> properties; }; class entitytemplate { public: vector<string> properties; }; class entitymanager { public: std::shared_ptr<gameentity> entitymanager::createentityfromtemplate(shared_ptr<entitytemplate> entitytemplate) { std::shared_ptr<gameentity> newentity = make_shared<gameentity>(); each(string propertyname in entitytemplate->properties) { if(propertyname == "graphicsproperty") { shared_ptr<graphicsproperty> gproperty = make_shared<graphics::graphicsproperty>(); newentity->attachproperty(gproperty); } else if(propertyname == "inputproperty") { shared_ptr<input::inputproperty> iproperty = make_shared<input::inputproperty>(); newentity->attachproperty(iproperty); } } }; };

not seen above entityloader creates entitytemplate objects xml file.

you can register properties build array of property factories , utilize factories in entity manager create them, e.g.

class gamepropertyfactorybase { public: virtual gameproperty *create() const=0; }; template<class t> class gamepropertyfactory: public gamepropertyfactorybase { public: virtual gameproperty *create() const {return new t;} }; typedef std::pair<const gamepropertyfactorybase*, const char*> factoryentry_t; std::vector<factoryentry_t> s_propertyfactories; template<class t> void registerproperty(const char *name_) { static const gamepropertyfactory<t> s_factory; s_propertyfactories.push_back(factoryentry_t(&s_factory, name_)); } void registerentities() { // register properties here registerproperty<graphicsproperty>("graphicsproperty"); };

then in entitymanager search name array , call:

gameproperty *prop=it->first->create();

c++ properties entity

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -