How Do I Go About Generalizing These Functions, And Should I?

I'm working on a game with an entity-component system. I have a Manager class that is handling all of the components by feeding them into vectors set up for each type of component. Right now I have a lot of repeated code for each kind of component.

1
2
3
4
5
6
7
void add_pos_comp(shared_ptr<Position> pos_comp);
shared_ptr<Position> get_pos_comp(int id);
const vector<shared_ptr<Position>>& get_pos_comps();

void add_vel_comp(shared_ptr<Velocity> vel_comp);
shared_ptr<Velocity> get_vel_comp(int id);
const vector<shared_ptr<Velocity>>& get_vel_comps();


I've tried generalizing this with templates, but I've been confused by how I should organize the collection of vectors for each component. I had it setup so that they were all in an unordered_map, but I couldn't figure out how to get the templated functions to find the right map slot to use based simply on the template's paramaters.

Further, recently I've read a couple of professional game developers that say they simply don't allow templates to be used in their developement at all. Any general advice for how to proceed would be appreciated, including suggestions to use a completely different approach. Thanks.

The full project is here for reference: https://github.com/kabbotta/last-ditch-cpp
Last edited on
You could use inheritance and/or polymorphism.
Topic archived. No new replies allowed.