c++ - Preventing unauthorised use of components -
i'm building component system abstract type component
inherited make components. far, have drawable, physical, movable , other components. seems go well, , in game
class perform following:
void game::init() { pplayer->addcomponent(pmovable); } void game::processevents() { if (sf::keyboard::iskeypressed(sf::keyboard::w)) pmovable->moveup(2.f); // etc.. pplayer->setvelocity(pmovable->getvelocity()); } void game::update() { pplayer->update(0); } void game::play() { while (pwindow->isopen()) { // ... processevents(); } }
so far, component system basic , simple. player of type object
, whenever call player's update function, have object's update function called. should automated, change in future. real problem this:
pplayer
can still access pmovable
's velocity even if has not added pmovable
component. problematic because means can velocity pmovable
, plug object without having add pmovable
part of component. now, tend happen movement becomes unmanaged since there no movable component regulate it. term unauthorised use of component, , want develop way component can 'deny' usage of functionality object not owned by. there many solutions problem, , need 1 efficient , practical use. here mine:
throw exception if client attempts allocate component function own without adding it;
create system objects , components identified , component keeps track of objects owned by, , objects keep track of components owns. because many-to-many relationship, intermediate class manages have created; avoids circular header inclusion.
have function not part of object 'deactivated'. require use of boolean 'componentadded' , functions have check whether component added or not, else function won't ought doing.
if have other solutions prevent unauthorised use of components, please share them i'm keen learn others how implemented/or implement component system have done here.
you can't prevent specific classes inheritance. it's or nothing proposition: class inherits base or none.
the best can hope narrow interface. example, instead of allowing decendents of object
in function, may want have class stationary_object
or enemy_object
refine interface. allows write functions take enemy_object
, won't take stationary_object
(like tree or wall).
edit 1: cheating friends
can allow member access declaring members private , granting access specific classes using friend
class. problem class data need modified whenever new type of friend created. believe use of friend
ship defeats purpose of reusability.
edit 2: detecting ownership
let's have 3 classes:
class engine; class car { engine car_engine; }; class boat { engine boat_engine; };
and function:
void fix_car_engine(engine& e) { }
there no method in c++ fix_car_engine
know fixing car engine or boat engine. function knows has been given generic engine fix (or knows common engine stuff of variable).
this issue can mitigated refining or narrowing interface:
class car_engine : public engine; class boat_engine : public engine; class car { car_engine diesel_engine; }; class boat { boat_engine propellor_engine; };
in above example, engine class has 2 specializations: car_engine
, boat_engine
. car
has car_engine
member , boat
has boat_engine
.
the functions can created operate on special engines:
void fix_boat_engine(boat_engine& be); void fix_car_engine(car_engine& ce);
the fix_car_engine
work car engines. works on class has-a car engine (as long pass car engine member). likewise, fix_boat_engine
function operates on boat engines.
given:
class rocket_engine : public engine;
also, specialization prevents rocket_engine being passed either function. functions require specific engine types.
Comments
Post a Comment