对于冗长的、可能令人困惑的标题,我深表歉意,我将尽力澄清。所以情况就是这样,我有一个组件 vector 元组(自定义类)。每个组件都有一个 id,它对应于它在元组中各自 vector 中的索引。组件属于一个实体,所以我希望该实体跟踪它拥有的组件的所有 ID。当然,在不知道它所属的组件类型的情况下,ID 将毫无意义,这样我就可以将它从元组中拉出来。所以我希望实体有一个集合,也许是一个 std::unordered_map
,通过它我可以提供一个类类型并得到适当的索引,或者一些数字(在这种情况下 SHRT_MAX
) 告诉我该实体没有这样的组件。
这似乎可以使用枚举和 switch 语句,但似乎也没有必要遍历 switch 语句的每个分支,只是为了为元组的 get 函数提供正确的类,所以我想知道是否有更好的方法。
我提供了我的代码示例,其中注释掉了我正在寻找的示例:
#include <tuple>
#include <vector>
#include <unordered_map>
class Component {
unsigned short id;
};
class CameraComponent : public Component {
};
class VehicleComponent : public Component {
};
class Entity {
//This is kind of the data structure I am thinking about so far
//std::unordered_map<ComponentType, unsigned short> components
};
class EntityManager {
private:
//This is the tuple I am talking about
static std::tuple<std::vector<CameraComponent>, std::vector<VehicleComponent>> components;
public:
//This is used to make accessing the tuple more convenient
template<class T>
static auto& Components();
//This is kind of function I would like to be able to use
template<class T>
static T& GetComponentFromEntity(Entity& e);
};
//Initialize the static tuple
std::tuple<std::vector<CameraComponent>, std::vector<VehicleComponent>> EntityManager::components;
//This is used to make accessing the tuple more convenient
template<class T>
auto& EntityManager::Components()
{
return std::get<std::vector<T>>(components);
}
//This is kind of function I would like to be able to use
template<class T>
T& EntityManager::GetComponentFromEntity(Entity& e) {
//unorded_map<ComponentType, unsigned short>::iterator itr;
//itr = e.components.find(T);
//if(itr = e.components.end())
//return SHRT_MAX;
//return Components<T>()[itr];
}
int main() {
return 0;
}
如有任何帮助,我们将不胜感激(我还想强调,我并不喜欢使用 unordered_map,这只是我想到的第一件事)。提前致谢。
最佳答案
正如 Igor Tandetkik 所建议的那样,答案是在类似于以下的方法中将 unordered_map
与 std::type_index
一起使用:
#include <tuple>
#include <vector>
#include <unordered_map>
#include <typeindex>
#include<iostream>
using std::cout;
using std::getchar;
class Component {
public:
unsigned short id = SHRT_MAX;
};
class CameraComponent : public Component {
};
class VehicleComponent : public Component {
};
class Entity {
public:
std::unordered_map<std::type_index, unsigned short> components;
};
class EntityManager {
private:
//This is the tuple I am talking about
static std::tuple<std::vector<CameraComponent>, std::vector<VehicleComponent>> components;
public:
//This is used to make accessing the tuple more convenient
template<class T>
static auto& Components();
//This is what I want
template<class T>
static T* GetComponentFromEntity(Entity& e);
};
//Initialize the static tuple
std::tuple<std::vector<CameraComponent>, std::vector<VehicleComponent>> EntityManager::components;
//This is used to make accessing the tuple more convenient
template<class T>
auto& EntityManager::Components()
{
return std::get<std::vector<T>>(components);
}
template<class T>
T* EntityManager::GetComponentFromEntity(Entity& e) {
std::unordered_map<std::type_index, unsigned short>::iterator itr;
itr = e.components.find(typeid(T));
if(itr == e.components.end())
return nullptr;
return &Components<T>()[itr->second];
}
int main() {
Entity e;
VehicleComponent v;
v.id = EntityManager::Components<VehicleComponent>().size();
EntityManager::Components<VehicleComponent>().push_back(v);
e.components.insert(std::make_pair<std::type_index, unsigned short>(typeid(VehicleComponent), 0));
cout << EntityManager::GetComponentFromEntity<VehicleComponent>(e)->id;
getchar();
return 0;
}
再次感谢 Igor Tandetnik!
关于c++ - 我可以按类类型访问什么类型的集合,并在必要时返回找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49103396/