I am reworking an Entity Component System's Entity Manager. Since components don't have overlapping functionalities, i dont want them to have a shared base I could store.
So i came up with something like this:
#include <vector>
#include <memory>
#include <iostream>
class Component1{};
class Component2{};
class Component3{};
class Manager{
public:
template<typename T> static std::vector<std::shared_ptr<T>> component;
template<typename T> static std::shared_ptr<T> getComponent(int nth){
return component<T>[nth];
}
template<typename T> static std::shared_ptr<T> addComponent(int nth){
return component<T>[nth] = shared_ptr<T>(new T());
}
static void addEntity(){
// push back a nullptr for every instantiated component<>
}
static void removeEntity(int nth){
// set the nth index of every component<> to nullptr
}
};
template<typename T>
std::vector<std::shared_ptr<T>> Manager::component = std::vector<std::shared_ptr<T>>();
int main(){
Manager::component<Component1>;
Manager::component<Component2>;
Manager::component<Component3>;
Manager::addEntity();
auto cmp2 = Manager::getComponent<Component2>(0);
Manager::removeEntity(0);
std::cin.get();
return 0;
}
How can I iterate over the instantiated components for the two functions? Tried using a vector of type_info's to store the Component types, but I could never the get the proper type out of them to use as a template argument.