I'm working on some code which gets data that looks like this:
enum data_type { INT16 = 0, INT32, UINT64, FLOAT, TIMESTAMP };
struct buffer {
data_type element_type;
size_t size; // in elements of element_type, not bytes
void* data;
}
(this is simplified; in actuality there are quite a few more types, more fields in this struct etc.)
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 realize I need to do some of that I need to do the same at run-time as well, and with a variable number of buffers... so now, in addition to type-traits-based lookup of values and enum-template-parameter-based lookup of types - I'm writing code which looks up std::type_info
s. It's kind of 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?
Notes:
- I get these buffers at run time, and cannot just un-erase the type at compile time (e.g. using a type_traits).
- I can't change the API. Or rather, I could change whatever I wanted in my code, but I still get data in this layout in memory.
- I don't just take such buffers as input, I also need to produce them as output.
- I occasionally need to handle many buffers of different at once - even a variable number of them (e.g.
foo(buffer* buffers, int num_buffers);
. - C++11 solutions are preferred over newer-standard-version ones.
- I actually use
gsl
a lot, so you can use it in your answers if you like. As for Boost - that may be politically difficult to depend on, but for the purposes of a StackOverflow question, it's fine, I guess.