mercredi 31 octobre 2018

Handing type-erased data at runtime - how not to reinvent the wheel?

I'm working on some code which gets data that looks like this (simplifying of course:

enum data_type { INT16, INT32, FLOAT };
struct buffer {
    data_type element_type;
    void*     data;
    size_t    size; // in elements of element_type, not bytes
}

(in actuality there are quite a few more types.)

Now, I find myself writing a bunch of utility code to "convert" enum values to actual types and vice-versa, at compile time. Then I need some of that at run time, and now I'm writing code which looks up std::type_infos. I will probably also need to insert by-data-type-enum calls. So - a mess.

But really - I should not be doing this. It's repetitive and I am absolutely sure I'm reinventing the wheel - implementing something which has already been written many times already: Compilers, DBMSes, data file parsers, serialization libraries and so on.

What can I do to minimize my wasted effort on this endeavor?

Note: C++11 solutions are preferred over others.

Aucun commentaire:

Enregistrer un commentaire