dimanche 16 août 2020

Weak POD static reflection

I am trying to achieve the following: Given a POD struct I want to generate compile time metadata on the struct automatically (i.e I don't want to have to register every field by hand).

The metadata I need is just the number of fields and the byte size of each field.

So for example for the struct:

struct MyData
{
    float field1;
    int field2;
}

All we need is FIELD_NUM(MyData) to return 2 and FIELD_SIZE(MyData, 0) to return 4.

My current approach is terrible. I don't even use the built in preprocessor, I have a python parser that searches for a comment on top of the structure and it builds a dictionary of all registered structures, it then replaces the patterns in the code with their numeric values. But this is very limited, for example I don't even handle templates because at that point I might as well make my own C++ compiler.

Is this achievable?





Aucun commentaire:

Enregistrer un commentaire