Re: how to store list of varying types
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 27 Jun 2008 17:24:45 -0400
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 wasJoseph M. Newcomer [MVP]
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
****
Joseph M. Newcomer [MVP]
Thanks,
Nick
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- how to store list of varying types
- From: Nick Schultz
- Re: how to store list of varying types
- From: Joseph M . Newcomer
- Re: how to store list of varying types
- From: Nick Schultz
- how to store list of varying types
- Prev by Date: Re: how to store list of varying types
- Next by Date: Re: Fragmented memory compaction ???
- Previous by thread: Re: how to store list of varying types
- Next by thread: Re: Creating static libraries w/MFC for multiple versions of Visual St
- Index(es):
Relevant Pages
|