I've got a map where keys are entity id's (uint32_t) and values are "signatures," aka uint64_t's with certain bits set that represent a type: std::unordered_map<uint32_t, uint64_t> m_entityComponents{};
My goal is to write a template function that accepts a variable amount of types and returns whether or not ALL of those types exist inside an entity. Let's call that function bool hasAllComponents(uint32_t entity).
To do this, I've written a method which returns a unique id for each type:
template <class T>
uint8_t getTypeId()
{
static uint8_t s_type = ++m_nextComponentType;
return s_type;
}
And then, given any number of types, I construct a new "signature" by calling getTypeId() using a fold expression to set each bit for every type present to 1:
template<typename... Types>
uint64_t getTypesSignature()
{
return ((1 << getTypeId<Types>()) | ... | 0);
}
Given these two helper functions, I should be able to check if an entity in my map has all of the passed in types with my hasAllComponents method, which uses a bitwise & operator to compare the cached signature in the map with the newly created one:
template<typename... Types>
bool hasAllComponents(uint32_t entity)
{
uint64_t signature = getTypesSignature<Types...>();
return (m_entityComponents[entity] & signature);
}
However, hasAllComponents still returns true when only some of the bits match, not all. I think the issue might be with my getTypesSignature fold logic not setting all bits correctly, although printing out the results looks fine to me. There's a flaw in my logic somewhere and would appreciate any help tracking it down!