Re: how to store list of varying types

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
.



Relevant Pages

  • Re: how to store list of varying types
    ... I always tell people "design the data structure FIRST" Once that is done, ... packet into individual chunks of varying sizes, based on the Opcode tag of ... the packet. ... Think about the fundamental object you need and build from that ...
    (microsoft.public.vc.mfc)
  • Re: how to store list of varying types
    ... packet into individual chunks of varying sizes, based on the Opcode tag of ... the packet. ... Think about the fundamental object you need and build from that for the simplest start. ...
    (microsoft.public.vc.mfc)
  • how to store list of varying types
    ... 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. ... and contains a vector of dataElements. ...
    (microsoft.public.vc.mfc)
  • how to store list of varying types
    ... 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. ... and contains a vector of dataElements. ...
    (microsoft.public.vc.language)
  • Re: planning next years garden
    ... One packet that I recall... ... How do you store your seed ... prior to planting? ...
    (rec.gardens)