我正在编写一个简单的实体组件系统框架,想要使用可变参数模板来获取更灵活的接口。对于每个组件,我都将偏移量(从块的内存开始)存储在std::array
中。在我的"update()"方法中,我想要从该数组中读取偏移量,将其添加到块的指针上,并将指向特定组件的指针直接传递给lambda作为参数。我尝试使用std::index_sequence
,但我无法同时将其作为元组和数组的索引。
template<typename ...Cn>
class SystemGroup {
public:
using OffsetArray = std::array<uint16_t, sizeof...(Cn)>;
static constexpr size_t kMaxGroups = 16;
static constexpr GroupIndex kInvalidIndex = -1;
struct Group {
static constexpr uint8_t kNumComponents = sizeof...(Cn);
OffsetArray componentOffsets;
Chunk *pFirstChunk;
};
};
template<typename ...Cn>
void SystemGroup<Cn...>::update() {
for (auto group : m_groups) {
// iterate over archetype's chunks
ecs::Chunk *pChunk = group.pFirstChunk;
do {
// get component data
std::tuple<Cn*...> pointers;
// Here is the problem. I don't know how to iterate over tuple and array using variadic templates
// pointers[0] = pChunk->memory + m_groups.componentOffsets[0];
// pointers[1] = pChunk->memory + m_groups.componentOffsets[1];
// pointers[sizeof..(Cn)] = pChunk->memory + m_groups.componentOffsets[sizeof..(Cn)];
auto updateComponents = [](int *pIntegers, float *pFloats) {
};
std::apply(updateComponents, pointers);
pChunk = pChunk->header.pNext;
} while(pChunk);
}
}
编辑 感谢大家的帮助。我决定选择max66提出的解决方案。当然,我将定义和lambda的调用分开以使其更易读。
m_groups
是什么?),有些东西被声明但没有使用(所有常量)。我也不确定updateComponents
应该做什么(pointers
是Cn*...
的元组,但updateComponents
只需要两个指针?)。这与问题相关吗?还是问题只是如何构造pointers
? - Barry