mardi 30 décembre 2014

Can I cast a stream of bytes into a type from an MPL typelist whose index I don't know until runtime?

So, say I have:


A list of all the types:



typedef boost::mpl::list<T1, T2, T3, T4, T5>::type types;


A list of bytes of all objects serialised:



std::array<uint8_t, 100> bytes{0x...};


A list of offsets in bytes to each object in the array:



std::array<std::size_t, 10> offsets{0, 1, 2, 3, 5, 8, 10, 11, 16, 20};


And a list of index's to types for each object in the byte array:



// T1, T1, T1, T2, T3, T2, T1, T5, T4
std::array<std::size_t, 10> offset_types{0, 0, 0, 1, 2, 1, 0, 4, 3, 4};


So now say I wanted to get object N out of the byte array:



reinterpret_cast<
boost::mpl::at_c<
types,
offset_types[N] // <-- problem...
>::type
>(bytes[offsets[N]]);


Which obviously doesn't work because I can't get offset_types[N] in < ... > scope.


Is this possible?


Aucun commentaire:

Enregistrer un commentaire