Re: Field Header
On Thu, 2010-11-18, Andrea Crotti wrote:
I have a Stream class, which handles in short the manipulation of vector
of chars, and what I need also is something that creates a header.
Now I only have (now) two possibilities
- type + payload
- type + size + payload
And that's what I'm using:
class FieldHeader : public Stream
{
public:
stream_t type;
stream_t size;
static const size_t TYPE = sizeof(stream_t);
static const size_t SIZE = sizeof(stream_t);
static const size_t TYPE_SIZE = TYPE + SIZE;
FieldHeader(stream_t);
FieldHeader(stream_t, stream_t);
FieldHeader(const Stream&, bool full=true);
static Stream createField(const Serializable&, stream_t, bool full=true);
};
it works and with "createField" I create the whole stream containing
payload and header, BUT
- the TYPE, SIZE, TYPE_SIZE are quite ugly
I would like to use "sizeof(type)" for example if possible, but a
static variable can't use a non-static variable (of course).
- if I want to add other fields is quite painful
Any suggestions to make this better?
To be blunt, you are probably overdesigning this A LOT. You have
asked a dozen or so questions here recently, all revolving around
run-time polymorphism. Concentrate on making the program do its job
instead.
I spent three years[1] maintaining an overdesigned protocol en/decoder
for the GTP protocol (which uses tag-value and tag-length-value
message attributes, just like yours) and I still hate the original
author. There were abstract factories, dynamic memory allocation,
std::multimap, templates, inheritance abuse in one big mix ... and it
was obviously not under its authors control.
I could prove, later, that none of that was needed, helpful, or even
harmless.
/Jorgen
[1] I had real work to do too, and the software had to work --
or I would have rewritten it during the first year.
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .