我可以按类型访问什么类型的集合,并在必要时找不到

时间:2018-03-05 04:14:24

标签: c++ templates tuples entity c++14

我为这个漫长的,可能令人困惑的标题道歉,我将尽力清理。所以在这种情况下,我有一个组件向量元组(自定义类)。每个组件都有一个id,它对应于它在元组中的相应向量中的索引。组件属于实体,因此我希望实体跟踪它拥有的组件的所有ID。当然,在不知道它所属的Component类型的情况下,ID会毫无意义,这样我就可以将它从元组中拉出来。所以我希望实体有一个集合,也许是一个std::unordered_map,通过它我可以提供一个类类型并输出相应的索引,或者一些数字(在这种情况下为SHRT_MAX)告诉我该实体没有这样的组成部分。

这似乎可以使用枚举和switch语句,但似乎也没必要遍历switch语句的每个分支,只是为了为元组的get函数提供正确的类,所以我&#39 ;我想知道是否有更好的方法。

我提供了一个代码示例,其中列出了我正在寻找的一些示例:

#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,这只是我想到的第一件事)。提前谢谢。

1 个答案:

答案 0 :(得分:0)

根据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!