TheNitesWhoSay / RareCpp

Creating a simpler, more intuitive means of C++ reflection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get the offset of a field?

hooyuser opened this issue · comments

Thanks for your excellent library! I wonder if there are some ways to get the address offset of a field, like the offsetof macro.

After a little research/discussion with others I decided there is no great alternative to offsetof, so I'll have to add "offset" to the Field structure (enhanced flavor), though depending on your compiler/OS something like this based on member pointers might give good results: https://godbolt.org/z/6GG3Ge73j

I'm curious what your use case is, I struggled to find much that sensibly paired with reflection (though interoping with legacy code that takes some sort of field list was compelling enough for me).

Thank you for your reply! Having an enhanced flavor for field sounds great. Currently I use offsetof because Vulkan C-style API typically requires address offset information of struct members.

Started an implementation (and got a draft working on at least one compiler), but this will be non-trivial to complete...

1.) offsetof can't be evaluated inside the struct (where the REFLECT macro lives), you can (at least in MSVC) define a function that returns the offset before completing the declaration of the struct, so long as it's not called elsewhere within that same struct
2.) functions, references, and static data members need to be filtered (which is doable with SFINAE/partial template specialization)
3.) providing the function within "Field" is problematic, "Field" being its own struct outside of the macro means it can't have the member name as an identifier/can't call offsetof itself, so it needs to have the function somehow supplied to it; std::function is unusable as it's not constexpr, Clang seems to reject C-style function pointers in this context for one reason or another; creating a lambda member in Field somehow may work, but I think CRTP is the best option - supply field with the type of the intermediate fieldName_ struct and call or alias a getter method from there

If efforts to create a field.getOffset() method fail alternatively could be creating a getter method in Class, or thoroughly testing the pointer-based solution (on all 3 compilers) and putting that in extended type support.

Successfully got offsets in the field structure (not strictly CRTP, just having the method use the parent class type template param) as of #86

Example usage: https://godbolt.org/z/v68x15hja

I've got the new version and tested getOffset(). It's very handy and works perfectly for my project. Thanks for getting this done!