Entity Component System의 Entity Manager가 수정되었습니다. 구성 요소에는 기능이 중복되어 있지 않기 때문에 내가 저장할 수있는 공유 기반을 갖기를 원하지 않습니다. 인스턴스화 된 템플릿 형식을 추적하십시오. C++
그래서 나는 이런 식으로 뭔가를 내놓았다 :#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;
}
나는 두 가지 기능에 대한 인스턴스화 된 구성 요소를 반복 할 수 있습니까? Type_info의 벡터를 사용하여 Component 유형을 저장하려고 시도했지만 템플릿 인자로 사용할 적절한 유형을 얻을 수 없었습니다.