I am writing a simple Entity Component System framework in which I want to use variadic templates to get more flexible interface. For each component I have offset (from the begining of chunk's memory) stored in std::array
. In my 'update()' method I would like to read offset from this array, add it to chunk's pointer and pass pointer (to specific component) directly to lambda as a parameter. I tried to use std::index_sequence
, but I wasn't able to use this index as an index of tuple and array at the same time.
Thanks in advance for help.
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);
}
}
EDIT Thank you all for your help. I decided to choose solution proposed by max66. Of course I split definition and call of lambda to make it more readable.
m_groups
?) and there are things that are declared but not used (all the constants). I'm also not sure whatupdateComponents
is supposed to do (pointers
is a tuple ofCn*...
butupdateComponents
takes two pointers?). Is that relevant to the question? Or is the question just how to constructpointers
? – Ambrosius