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

问题描述 投票:1回答:1

我为这个漫长的,可能令人困惑的标题道歉,我将尽力清理。所以这就是情况,我有一个组件向量元组(自定义类)。每个组件都有一个id,它对应于它在元组中各自向量中的索引。组件属于实体,因此我希望实体跟踪它拥有的组件的所有ID。当然,在不知道它所属的Component类型的情况下,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,这只是我想到的第一件事)。提前致谢。

c++ templates tuples entity c++14
1个回答
0
投票

正如Igor Tandetkik所建议的那样,答案是使用unordered_mapstd::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!

© www.soinside.com 2019 - 2024. All rights reserved.