A lot of communication is done by creating packets (for sockets/IPC etc). Often you end up with variable sized packets for example strings in it. For example.
Code:
struct StringPacketBase
{
protected:
int stringLen;
};
Then the name comes after the struct. If you do a class of this
Code:
class StringPacket : public StringPacketBase
{
public:
uint getLength { return stringLen; }
char *GetString { return (char*)this + sizeof(StringPacketBase) }
};
I wonder if there is a better way to automate packet creation classes. Also a problem is that the string will never show up in debugging information.
I've seen this:
Code:
struct StringPacketBase
{
protected:
int stringLen;
char string[];
};
Now string is a part of the struct but what happens if you want two strings after each other? Then this method only work for the first string. Also if you use sizeof with StringPacketBase you have to be more careful now.
The question is how you can create packets so that it becomes easy to write. Right now you do a lot of calculations with offsets and I wonder if there is a method make writing all these packets (which is a lot packets) more conveniently.