我正在尝试编写一个 EntityType
类,它可以接收和存储可变数量的 Component
类型。
struct Health { int amount; }
struct Position { float x, y; }
EntityType entityType = new EntityType<Health, Position>();
稍后我将使用此 EntityType
类作为为组件分配紧凑内存的蓝图。
EntityManager.BatchCreate(3, entityType);
// Result: Health | Health | Health | Position | Position | Position
创建具有多个参数的类模板很容易,但是:
- 如何存储类型以便稍后用作分配蓝图?
- 我可以查询 EntityType 中有哪些类型吗?
我对存储的第一个想法是元组,但我不确定。这些采用传入类型的实际值,而不是类型本身。我能以某种方式使用 typeid
吗?
我基本上是在尝试用 C++ 复制 Unity 在 C# 中用 EntityArchetype 做的事情,我相信这是在使用反射。
最佳答案
- How do I store the types to be used as a allocation blueprint later?
由于您在编译时知道组件的类型,因此可以使用这样的类型别名:
template<class... Components>
struct Entities {
/* ... to be implemented ... */
};
using HealthsPositions = Entities<Health, Position>;
- Can I query what types are in the EntityType?
是的,这在编译时也是已知的。似乎 std
命名空间中没有帮助器来测试类型列表中是否包含类型(请参阅 this question 的答案多样性)。所以这里是解决 C++14 中的模板元编程任务的另一种方法:
template<class Component, class EntitiesCs>
struct IsComponentOf;
template<class Component, class... Cs>
struct IsComponentOf<Component, Entities<Cs...>> {// partial specialization
static constexpr bool value_() {
bool ret = false;
for(bool is_same : {std::is_same<Component, Cs>{}()...}) {
ret |= is_same;
}
return ret;
// C++17 version with fold expression:
// return (... || std::is_same<Component, Cs>{});
}
static constexpr bool value = value_();
constexpr operator bool() const { return value; }
};
static_assert(IsComponentOf<Health, HealthsPositions>{}, "");
static_assert(IsComponentOf<Position, HealthsPositions>{}, "");
static_assert(not IsComponentOf<int, HealthsPositions>{}, "");
Can I work with typeid's somehow?
是的,但这是我上面描述的另一种方法:上面的方法在编译时工作。 typeid
operator来自运行时类型信息 (RTTI) 的世界。不幸的是,std::type_info
不能在编译时使用。
I will then use this EntityType class later as a blueprint for allocating tightly packed memory for the components.
EntityManager.BatchCreate(3, entityType); // Result: Health | Health | Health | Position | Position | Position
如果您真的希望组件紧密排列,并且希望能够调整“容器”的大小,那么我看不到一个简单的解决方案。在理想情况下,HealthsPositions
商店,例如,
- 指向第一个
Health
组件开始的内存的类似指针的成员, std::size_t
(或其他)成员,用于存储每种类型的组件数量,以及std::size_t
(或其他)成员,用于存储每种类型的组件的容量。
理想情况需要一些自定义内存管理(包括对齐注意事项)。
但是,这个另一种简单设计可能是一个很好的起点:
#include <cstddef>
#include <iostream>
#include <tuple>
#include <type_traits>
#include <vector>
struct Health { int amount; };
struct Position { float x; float y; };
template<class C0, class... Cs>
struct Entities {
std::tuple<
std::vector<C0>, std::vector<Cs>...
> components;
Entities(std::size_t size)
: components{size, (0*sizeof(Cs) + size)...}
{}
};
template<class Component, class... Cs>
constexpr std::vector<Component>& get(Entities<Cs...>& e) {
using ComponentVector = std::vector<Component>;
return std::get<ComponentVector>(e.components);
}
template<class Component, class... Cs>
constexpr const std::vector<Component>& get(const Entities<Cs...>& e) {
using ComponentVector = std::vector<Component>;
return std::get<ComponentVector>(e.components);
}
////////////////////////////////////////////////////////////////////////////////
using HealthsPositions = Entities<Health, Position>;
constexpr std::size_t expected_size =
sizeof(std::vector<Health>) + sizeof(std::vector<Position>);
static_assert(sizeof(HealthsPositions) == expected_size, "");
int main() {
std::size_t entity_count = 7;
HealthsPositions hps(entity_count);
get<Health>(hps).at(2).amount = 40;
get<Position>(hps).at(5) = Position{3.5f, 8.4f};
std::cout << "health address and value:\n";
for(auto&& h : get<Health>(hps)) {
std::cout << &h << "\t" << h.amount << "\n";
}
std::cout << "position address and value:\n";
for(auto&& p : get<Position>(hps)) {
std::cout << &p << "\t" << p.x << "\t" << p.y << "\n";
}
}
示例输出:
health address and value:
0x55adba092eb0 0
0x55adba092eb4 0
0x55adba092eb8 40
0x55adba092ebc 0
0x55adba092ec0 0
0x55adba092ec4 0
0x55adba092ec8 0
position address and value:
0x55adba092e70 0 0
0x55adba092e78 0 0
0x55adba092e80 0 0
0x55adba092e88 0 0
0x55adba092e90 0 0
0x55adba092e98 3.5 8.4
0x55adba092ea0 0 0
关于c++ - 如何存储模板类型以供以后分配使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53685679/