Re: how to store list of varying types
- From: Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx>
- Date: Thu, 19 Jun 2008 12:08:22 +0200
Nick Schultz wrote:
I am writing a program that parses data found in a proprietary protocol[...]
packet into individual chunks of varying sizes, based on the Opcode tag of
the packet. sizes could be from a single byte to an very large null
terminating string.
I planned on creating a ProtocolPacket class that represents an entire
packet, and contains a vector of dataElements. dataElement is a class
that contains a pointer to the data, its size(in bytes) and a char* that
stores its field name.
Why would you ever use a char* to store a string? Use std::string!
My original implementation called for malloc'ing the necessary space on
the heap, and passing the user a void pointer to the data on the heap
along with a data length. This then allows me to have a
vector<dataElement> that point to dataElements that represent various
types. Does C++ ( or anything simpler and safe) cover this type of
functionality? Or is my design philosophy flat out flawed?
I'm not sure. The question is if you need the concrete object and the byte
representations at the same time. If not, I would create a parser that
takes a byte-stream and emits boost::variant<..> or maybe boost::any. If
you need both and also the type to change when you modify the byte
representation I would just store bytes and cache a concrete object
representation.
Anyway, my first choice would be a variant type, my second one would be a
common baseclass with derived types.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
.
- References:
- how to store list of varying types
- From: Nick Schultz
- how to store list of varying types
- Prev by Date: Re: Free SMTP implementation av
- Next by Date: std::ifstream
- Previous by thread: how to store list of varying types
- Next by thread: /clr:safe
- Index(es):
Relevant Pages
|