What about using a function templates like these ones:
template <typename T>
T read_value(char*& ptr) {
T result = *(reinterpret_cast<T*>(ptr));
ptr += sizeof(T);
return result;
};
template <typename T>
void write_value(char*& ptr, const T& val) {
*(reinterpret_cast<T*>(ptr)) = val;
ptr += sizeof(T);
};
And then do this:
char* Data = static_cast<char*>(GivenPtr);
char* cur_ptr = Data;
switch( read_value<int>(cur_ptr) ) {
case OBJECT:
{
// ...
double some_value = read_value<double>(cur_ptr);
//... etc..
};
break;
case OBJECT2:
// ...
};
How can I tell what Indicies to write to after each cast?
Whenever you read/write something from the buffer, increment the pointer by the size of the value you read/wrote. This is easiest with a function template as I showed above.
Is there a way to just write it straight to a char* and then read straight from that char?
That's pretty much what you are doing already. The type casts will not really cause any overhead, beyond that of breaking word-alignment.
Better yet, can I do this with a void?
It is preferrable to do it with a char*
because it has a guaranteed size of 1 byte, while a void has undefined size, which is going to cause trouble when doing the pointer incrementing.