veselink1 / refl-cpp

Static reflection for C++17 (compile-time enumeration, attributes, proxies, overloads, template functions, metaprogramming).
https://veselink1.github.io/refl-cpp/md__introduction.html
MIT License
1.05k stars 76 forks source link

Field offsetof #79

Open W4RH4WK opened 1 year ago

W4RH4WK commented 1 year ago

Apparently getting the offset of a field within a struct from a pointer-to-member is not trivial (see).

It would be very helpful to get a field's offset from its respective field_descriptor.

veselink1 commented 1 year ago

That would only work on standard-layout types, no?

I think it is a good idea to provide it somehow, just clarifying the restrictions the standard imposes. Non-std::is_standard_layout types would not have field offsets.

W4RH4WK commented 1 year ago

Yes, you are right. offsetof is only defined for standard layout types. I forgot about this restriction.

veselink1 commented 1 year ago

Would that restriction prevent you from using it? It might be nice to know what the use-case is :)

W4RH4WK commented 1 year ago

Wouldn't restrict me at all. My use-case is the same as in the reference from my initial post.

Given the definition of a vertex:

struct StandardVertex {
    vec3 position;
    vec3 normal;
    vec4 tangent;
    vec2 uv;
};

I'd like to generate the shader input description, required by the graphics API, using reflection. Currently I call offsetof manually and store that value in an attribute:

REFL_TYPE(ikaros::StandardVertex)
REFL_FIELD(position, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, position), "POSITION"));
REFL_FIELD(normal, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, normal), "NORMAL"));
REFL_FIELD(tangent, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, tangent), "TANGENT"));
REFL_FIELD(uv, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, uv), "TEXCOORD", 0));
REFL_END

However, this feature is not critical at all. I am not 100% convinced yet myself whether this is the best approach. But I feel like having the field offset being part of the descriptor would be very helpful for this case, and probably a few others. The standard layout restriction isn't too problematic, one can practically always wrap a management class around a standard layout struct.