Hello,
I know that there is no such thing like reflection in C++, but I need something a tad bit in that direction. I want to refactor and improve my component system, by automating most loading/serialization/editor-related stuff. For this, I plan on adding a component declaration class, where attributes with a certain known type (int, float, string, ...) are specified. This would look something like that:
class Transform : public ecs::Component<Transform>
{
math::Vector3 vPosition;
static const ComponentDeclaration& Declaration(void)
{
const ComponentDeclaration::AttributeVector vAttributes =
{
{ AttributeType::FLOAT, "x" },
{ AttributeType::FLOAT, "y" },
{ AttributeType::FLOAT, "z" },
}
static const ComponentDeclaration decl(vAttributes);
return decl;
}
}
Now, I just could have a "serialize/deserialize"-method, but I want to ultimately get rid of those, to minimize work required for each component. Is there any way to calculate/store the "offset" of a member variable and use this to access/set that variable? Something like:
// in the declaration
{ AttributeType::FLOAT, "x", OFFSET(Component::vPosition::x) },
// later on
auto& component = ComponentFactory::CreateComponent(type);
const auto& declaration = component.GetDeclaration();
for(auto& attribute : declaration.GetAttributes())
{
switch(type)
{
case AttributeType::FLOAT:
{
float* pAttribute = (float*)(((char*)(void*)(component)) + attribute.offset);
}
}
}
How would you do so?
PS: Though I don't care about 100% (type)safety and cross-compiler right now, I'm also open for all other, probably better suggestions for that problem you might have.