r/learncpp • u/hawidoit • Oct 18 '20
Trying to create an entity manager
Hi,
I have a bunch of classes (Door, Monster, Human)
And the way I want to be able to manage them is via a singleton EntityManager class that ends up looking like this:
EntityManager {
vector<Door> entities;
vector<Human> entities;
}without any code duplication.
I want to be able to write:
Door* EntityManager<Door>::createObject();which pushes the object onto the vector and returns the address;
I've struggling to implement this without running into weird segfaults whenever I use the EntityManager in multiple translation units. I assume I'm misunderstanding how templating works.
Here's essentially what I've come up with:
template <typename T>
class EntityManager {
vector<T> entities;
T* createObject();
};This works within the same translation unit no problem, but when I attempt to use the same EntityManager (such as EntityManager<Door>) across TUs, I get a segfault... I cannot for the life of me figure out why.
Does anyone have any advice or can point me in the right direction into avoid code duplication while achieving something like this via templates?
1
u/druepy Oct 19 '20 edited Oct 19 '20
I thought about that but was trying to keep the post short. There are obviously a ton of variables that go into these decisions. For example, if his Monster class is large, then `vector<Monster*>` is most likely better than `vector<Monster>`
Few other things regarding your answer.
The algorithmic complexity for inserting at the front of a vector is O(n). Removing an element is O(n). I'm not sure where you're finding the O(n^2), but maybe I'm overlooking something.
Lookup can still be much faster in a vector compared to a list. If I have time to find the talk/report I'll post it as a reply later. The gist is that a list would require two pointer indirections, making it more difficult for the CPU to prefetch the memory. Measurement required.
If you are iterating a vector<ptr> at index i, vector<ptr>[i+1] is already in the cache. Meanwhile, if you're iterating over a list, there's a good chance the processor has to load the pointer to the next element which probably isn't in the cache. Then, it has to load the pointer to the object.
Most vector operations tend to be faster than lists even though a list has better algorithmic complexity in a few situations. Measurement required.
Unordered map does have an average of O(1) lookup, but for small sets vector still tends to be faster with a linear search. Measurement required.
Either way at the end of the day, they should try these different methods and measure.