Re: how to store list of varying types

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Same technique would be used in Java.
joe

On Fri, 27 Jun 2008 12:00:37 -0700, "Nick Schultz" <nick.schultz@xxxxxxxx> wrote:

Well that's great, I'm using ideas that were in use 11 years BEFORE I was
born... Thank you college education. My computer science curriculum (I'm
class of 08) covered Java (as introductory to programming), and only C in
upper classes. So I haven't really had much experience with C++, as well as
programming design techniques for nontrivial problems.



Nick

"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:erut54d58p1ugsjgc936t3h9mtv5gjt739@xxxxxxxxxx
Size is irrelevant. You need to say more about the packet.

I would tend to do something like

class Packet {
public:
virtual void SomeMethod() PURE;
};

class Thing : public Packet {
public:
...Thing data here
virtual void SomeMethod();
};

class Whatever : public Packet {
public:
...Whatever data here
virtual void SomeMethod();
};

Now, when you decode the opcode, you would do


switch(opcode)
{
case OpThing:
{
Thing * p = new Thing(etc...);
...set Thing members
Enqueue(p);
}
break;
case OpWhatever:
...

}

void Enqueue(Packet * p)
{
....
}

void Dequeue()
{
Packet * p = RemoveFromQueue();
p->SomeMethod();
}

and so on. This is C++, and you should not be thinking of it as if it
were K&R C in 1975.
joe

On Wed, 18 Jun 2008 14:50:49 -0700, "Nick Schultz" <nick.schultz@xxxxxxxx>
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. The parser is part of a pipeline that works on the
incoming packets so that an user at the end can do whatever he/she wants
to
do with the data (display it, apply operations, etc)



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.
****
So why is this difficult?
class ProtocolPacket {
public:
CStringA text;
};

doesn't seem to be difficult. Your error is in thinking about char* as a
representation,
instead of CString, or, since the packets are probably defined as 8-bit
packets, CStringA.
****



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?
****
This is C++. Why would you consider malloc a reasonable solution to
*anything*? You
could use 'new', but malloc is irrelevant. But why use any of it, when
you can use
CStringA? Or, if the data contains embedded NUL characters, CByteArray.
You are
programming using 1975 ideas.

Your design philosophy is right, but your implementation ideas are decades
out of date.
Forget you ever heard of malloc, and in general, you would not use 'new'
if there is a
collection class that solves the problem without requiring explicit
allocation/deallocation.
joe
****



Thanks,



Nick




Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: how to store list of varying types
    ... Since the packet type implicitly indicates what the ... prefer using std::vector as container (instead of raw pointer), ... start with an upper-case letter (so, I would use DataElement ...
    (microsoft.public.vc.mfc)
  • Re: This is tougher than I thought?
    ... > first programming language, Pascal, that is now Delphi. ... > packet term. ... > refers to NT 5 features is separated properly and doesn't crash. ... > complete it will work on any Windows from 95 above. ...
    (comp.protocols.time.ntp)
  • Re: how to store list of varying types
    ... virtual void SomeMethod() PURE; ... class Thing: public Packet { ... packets, CStringA. ... Why would you consider malloc a reasonable solution to ...
    (microsoft.public.vc.mfc)
  • Using ethernet on a Xilnx board (Help appreciated)
    ... Up until very recenty my programming experience was ... limited to higher level programming language: ... What I attempted to do was to build a valid packet, ... board into a router. ...
    (comp.arch.fpga)
  • Re: how to store list of varying types
    ... You need to say more about the packet. ... virtual void SomeMethod() PURE; ... and contains a vector of dataElements. ... instead of CString, or, since the packets are probably defined as 8-bit packets, CStringA. ...
    (microsoft.public.vc.mfc)