A
Andrea Crotti
So I have the following situation, a simple Packet type which is the
superclass of all the possible packet types.
Then I have many subclasses, for example this:
class CoordAns : public Packet
{
private:
PadCoordinate ans;
PadCoordinate dest;
node_idx_t node;
and for this the code below creates and parses a stream.
Stream CoordAns::toStream() const
{
Stream res = FieldHeader::sizeField(ans);
res += FieldHeader::sizeField(dest);
res += FieldHeader::basicSizeField(node);
return FieldHeader::typeField(res, COORD_ANS);
}
CoordAns CoordAns:arseStream(Stream &raw)
{
CoordAns cr;
Stream tmp;
size_t next = FieldHeader::getNextSize(raw);
tmp = raw.trim(next);
cr.ans = PadCoordinate:arseStream(tmp);
next = FieldHeader::getNextSize(raw);
tmp = raw.trim(next);
cr.dest = PadCoordinate:arseStream(tmp);
next = FieldHeader::getNextSize(raw);
tmp = raw.trim(next);
cr.node = tmp.toBasic<node_idx_t>();
return cr;
}
It's a bit ugly and more importantly it's the same for every packet
class, increasing as the size of the fields increase.
Since I have already quite generic functions my idea was to do the
following.
std::vector<FieldType *> fields;
and then use typeid(x).name() to get the right type for the templated
functions.
I would already have a superclass for most of the fields, but some are
basic types (int, size_t).
But if this approach WOULD work maybe I'd better define (just a few)
also some wrapper classes for those simple types.
Any other idea (which does not involve boost unfortunately?)?
superclass of all the possible packet types.
Then I have many subclasses, for example this:
class CoordAns : public Packet
{
private:
PadCoordinate ans;
PadCoordinate dest;
node_idx_t node;
and for this the code below creates and parses a stream.
Stream CoordAns::toStream() const
{
Stream res = FieldHeader::sizeField(ans);
res += FieldHeader::sizeField(dest);
res += FieldHeader::basicSizeField(node);
return FieldHeader::typeField(res, COORD_ANS);
}
CoordAns CoordAns:arseStream(Stream &raw)
{
CoordAns cr;
Stream tmp;
size_t next = FieldHeader::getNextSize(raw);
tmp = raw.trim(next);
cr.ans = PadCoordinate:arseStream(tmp);
next = FieldHeader::getNextSize(raw);
tmp = raw.trim(next);
cr.dest = PadCoordinate:arseStream(tmp);
next = FieldHeader::getNextSize(raw);
tmp = raw.trim(next);
cr.node = tmp.toBasic<node_idx_t>();
return cr;
}
It's a bit ugly and more importantly it's the same for every packet
class, increasing as the size of the fields increase.
Since I have already quite generic functions my idea was to do the
following.
std::vector<FieldType *> fields;
and then use typeid(x).name() to get the right type for the templated
functions.
I would already have a superclass for most of the fields, but some are
basic types (int, size_t).
But if this approach WOULD work maybe I'd better define (just a few)
also some wrapper classes for those simple types.
Any other idea (which does not involve boost unfortunately?)?