繁体   English   中英

从组件的 std::type_index 获取派生最多的类型

[英]Get the most derived type from std::type_index for component

我有以下代码允许我使用实体组件系统。 然而,由于模板的性质,从std::vector<HE2_Component*>添加组件会导致它们添加 HE2_Component 的HE2_Component而不是它们的派生形式(代码底部的示例)。 如何强制它正确识别组件类型?

template<typename T,
    typename = std::enable_if_t<std::is_base_of_v<HE2_Component, T>>>
    void addComponent(T* component)
{
    components.insert(std::make_pair(std::type_index(typeid(T)), component));
    component->host = this;
}


template<typename CompType,
    typename = std::enable_if_t<std::is_base_of_v<HE2_Component, CompType>>>
    inline void removeComponent()
{
    auto it = components.find(std::type_index(typeid(CompType)));

    if (it == components.end())
        return;

    components.erase(it->first);
}


template<typename CompType,
    typename = std::enable_if_t<std::is_base_of_v<HE2_Component, CompType>>>
    inline CompType* getComponent()
{
    auto it = components.find(std::type_index(typeid(CompType)));

    if (it == components.end())
    {
        throw std::runtime_error("Object does not contain this component!");
        return nullptr;
    }


    return dynamic_cast<CompType*>(it->second);
}


//EXAMPLE HERE 
//Setup
HE2_ComponentOwner* obj = new HE2_ComponentOwner();
HE2_ComponentOwner* obj2 = new HE2_ComponentOwner();

class A : virtual public HE2_Component { double f = 0.0; };
class B : virtual public HE2_Component { float b = 0.0f; };
class C : public HE2_Component { int x = 0; };

//Add some components from a vector to obj
std::vector<HE2_Component*> comps = { new A(), new B(), new C() };
for (auto x : comps)
    obj->addComponent(x);

//Add some manually to obj2
obj2->addComponent(new A());
obj2->addComponent(new B());
obj2->addComponent(new C());

//This doesn't work
A* a = obj->getComponent<A>();
B* a = obj->getComponent<B>();
C* c = obj->getComponent<C>();

//This does work

A* a = obj2->getComponent<A>();
B* b = obj2->getComponent<B>();
C* c = obj2->getComponent<C>();

您需要执行typeid(*component)来获取指向对象的动态类型,而不是其类型名或指向它的指针的声明类型。

自然, typeid(T)将始终是T 因此,如果您传递派生程度较低的指针类型T* component ,则typeid(T)不等同于typeid(*component)

此外,如果您正在尝试typeid(component) ,而没有取消引用,那么您应该获得指针的类型,而不是它指向的类型,这不应该是正确的。

最后,虽然这在您的情况下似乎已经确定,但值得注意的是,对象需要是多态的才能使其工作,即至少具有一个虚拟成员函数。 否则,这个需要的 RTTI 将不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM