473,221 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Container Class

I'm going through the Koeing book Accelerated C++ in an attempt to
understand Container classes. Of course I'm going through a paradigm
shift from C to C++.
So now I've got

struct Header
{
int source : 3;
int length : 4;
int count : 9
}

struct My_Struct
{
Header header;
// data
}

struct My_Other_Struct
{
Header header;
// data
}
// then later ... Rx_1553_Data
void Rx_1553_Data ( void )
{
unsigned char buffer [ 512 ];

// later
Read_1553_Data ( CHANNEL_FIVE, &buffer );
Parse_Rx_Message (&buffer);
// more stuff.
}

Now Parse_Rx_Message will look at the appropriate header then memcopy
to the appropriate struct (either My_Stuct or My_Other_Struct) .
Trouble is I'd like to keep 'this' generic. In essense, use a
container class to handle all the memcopy etc. This 'generic'
container class will setup structs, compare structs etc. Does anyone
have an example that does this.

Wading through Koeing and would like to overhaul some 'test' code
which will hopefully help me understand the template/container aspect
of ++.

Thanks in advance
Jul 19 '05 #1
5 4790
"MPowell" <mp********@hotmail.com> wrote...
I'm going through the Koeing book Accelerated C++ in an attempt to
understand Container classes. Of course I'm going through a paradigm
shift from C to C++.
So now I've got

struct Header
{
int source : 3;
int length : 4;
int count : 9
} ;
struct My_Struct
{
Header header;
// data
} ;
struct My_Other_Struct
{
Header header;
// data
} ;

// then later ... Rx_1553_Data
void Rx_1553_Data ( void )
{
unsigned char buffer [ 512 ];

// later
Read_1553_Data ( CHANNEL_FIVE, &buffer );
Parse_Rx_Message (&buffer);
// more stuff.
}

Now Parse_Rx_Message will look at the appropriate header then memcopy
to the appropriate struct (either My_Stuct or My_Other_Struct) .
Trouble is I'd like to keep 'this' generic. In essense, use a
container class to handle all the memcopy etc.
Why not let each class handle its own copying/creation from bytes?
This 'generic'
container class will setup structs, compare structs etc. Does anyone
have an example that does this.
Find and read about serialisation. It's not as simple an issue
to be described in a reply to a newsgroup posting.

In general, there should be some kind of distinctive marking in
the memory that would make it one object and not any other. If
you find that mark, then a serialiser object should be used to
create a certain structure from the stream/bunch of bytes. The
serialisers usually are registered with the stream reader and
associated with those marks (or you could query all registered
serialisers to know which one will be responsible)...
Wading through Koeing and would like to overhaul some 'test' code
which will hopefully help me understand the template/container aspect
of ++.


I don't think you're in that area "of ++". I may be mistaken,
of course.

Victor
Jul 19 '05 #2
MPowell wrote:
I'm going through the Koeing book Accelerated C++ in an attempt to
understand Container classes. Of course I'm going through a paradigm
shift from C to C++.
So now I've got

struct Header
{
int source : 3;
int length : 4;
int count : 9
}

struct My_Struct
{
Header header;
// data
}

struct My_Other_Struct
{
Header header;
// data
}
// then later ... Rx_1553_Data
void Rx_1553_Data ( void )
{
unsigned char buffer [ 512 ];

// later
Read_1553_Data ( CHANNEL_FIVE, &buffer );
Parse_Rx_Message (&buffer);
// more stuff.
}
One way is this:

==============
interfaces - in your header file
==============

//
// declare all the different "types" of messages
class AlphaMessageType;
class BetaMessageType;
....
class GammaMessageType;

//
// define the base message interface
class BaseMessageType
{
public:
virtual ~BaseMessageType() {};

virtual bool ProcessMe( MessageProcessor * processor ) = 0;

virtual bool Deserialize( void * data ) = 0;
};

//
// All classes that need to "process" a message derive from
// this class and implement the Process() methods they care about.
//
class MessageProcessor
{
public:
bool Process( AlphaMessageType * message ) { return false };
bool Process( BetaMessageType * message ) { return false };
...
bool Process( GammaMessageType * message ) { return false };
};

//
// define the specific INTERFACES for each type of message
//
class AlphaMessageType : public BaseMessageType
{
... stuff particular to AlpaMessageType ...

};
==========================

Message Implementations

//
// Each message has an implementation
//
class AlphaMessageTypeImpl : public AlphaMessageType
{

virtual bool ProcessMe( MessageProcessor * processor )
{
return processor->Process( this );
}

AlphaSerializedDataStuff * stuff;
... must implement message specific deserialiser
... and serializer
... and copier
... and whatever else that is message specific

};

//
// register the message in a factory registry ...
//
REGISTER_IN_FACTORY( AlphaMessageTypeImpl, AlphaMessageKey );

etc for all the message types.
BaseMessageType * Parse_Rx_Message( void * buffer )
{

Header * hdr = reinterpet_cast<Header *>( buffer );

if ( ! CheckHeader( hdr ) )
{
return false;
}

BaseMessageType * msg =
MessageFactoryCreate( GetMessageType( hdr ) );

if ( msg == 0 )
{
return 0;
}

if ( msg->Deserialize( buffer ) == false )
{
delete msg;
return 0;
}

return msg;
}

=========================

If you get clever, you can use a template to create message
implementations. There's a few more steps than what I show here but
this should be enough. Still a number of issues need to be solved here,
- partial deserialization issues
- endian issues
- forward compatability
- error recovery
Now, to process the message you can derive from MessageProcessor and
implement the methods that work for you or you can simply "cast" using
somthing like this:

template <typename T> class MessageCast
: public virtual MessageProcessor
{
T m_message;
public:

MessageCast( BaseMessageType * message )
: m_message( 0 )
{
if ( message != 0 ) {
message->ProcessMe( this );
}
}

bool Process( T w_message )
{
m_message = w_message;
return true;
}

operator T ()
{
return m_message;
}

};
This may be used just like dynamic_cast

e.g.

AlphaMessageType * amsg =
MessageCast<AlphaMessageType *>( base_msg );

if ( amsg != 0 )
{
// base_msg is an Alpha message - do somthing
.....

Now Parse_Rx_Message will look at the appropriate header then memcopy
to the appropriate struct (either My_Stuct or My_Other_Struct) .
Trouble is I'd like to keep 'this' generic. In essense, use a
container class to handle all the memcopy etc. This 'generic'
container class will setup structs, compare structs etc. Does anyone
have an example that does this.

Wading through Koeing and would like to overhaul some 'test' code
which will hopefully help me understand the template/container aspect
of ++.


In the above design, templates enter the picture once you have
solidified the requirements. At this point, I would write some test
code that would exercise all the failure scenarios so that you can
create a more generic interface and hence do s better job designing a
class hierarchy for message implementations.

Jul 19 '05 #3
<MPowell>
I'm going through the Koeing book Accelerated C++ in an attempt to
understand Container classes.
</MPowell>

What part of Accelerated C++ are you in?
Jul 19 '05 #4
MPowell wrote:
[redacted]


Totally OT, but what the hey. You have my sympathy. Actually, anybody who writes MIL-STD-1553 drivers
has my sympathy (since I've been there, done that, and got the T-Shirt).

red floyd
Jul 19 '05 #5
Gianni Mariani <gi*******@mariani.ws> wrote in message news:<bj********@dispatch.concentric.net>...
MPowell wrote:
I'm going through the Koeing book Accelerated C++ in an attempt to
understand Container classes. Of course I'm going through a paradigm
shift from C to C++.
So now I've got

struct Header
{
int source : 3;
int length : 4;
int count : 9
}

struct My_Struct
{
Header header;
// data
}

struct My_Other_Struct
{
Header header;
// data
}
// then later ... Rx_1553_Data
void Rx_1553_Data ( void )
{
unsigned char buffer [ 512 ];

// later
Read_1553_Data ( CHANNEL_FIVE, &buffer );
Parse_Rx_Message (&buffer);
// more stuff.
}
One way is this:

==============
interfaces - in your header file
==============

//
// declare all the different "types" of messages
class AlphaMessageType;
class BetaMessageType;
...
class GammaMessageType;

//
// define the base message interface
class BaseMessageType
{
public:
virtual ~BaseMessageType() {};

virtual bool ProcessMe( MessageProcessor * processor ) = 0;

virtual bool Deserialize( void * data ) = 0;
};

//
// All classes that need to "process" a message derive from
// this class and implement the Process() methods they care about.
//
class MessageProcessor
{
public:
bool Process( AlphaMessageType * message ) { return false };
bool Process( BetaMessageType * message ) { return false };
...
bool Process( GammaMessageType * message ) { return false };
};

//
// define the specific INTERFACES for each type of message
//
class AlphaMessageType : public BaseMessageType
{
... stuff particular to AlpaMessageType ...

};
==========================

Message Implementations

//
// Each message has an implementation
//
class AlphaMessageTypeImpl : public AlphaMessageType
{

virtual bool ProcessMe( MessageProcessor * processor )
{
return processor->Process( this );
}

AlphaSerializedDataStuff * stuff;
... must implement message specific deserialiser
... and serializer
... and copier
... and whatever else that is message specific

};


What exactly is a 'copier'? Book didnt seem to highlight that.
Truly appreaciate the help. I suspect "Serialization" is the way to
go. Would it be too much to ask for some assistance on say ONE
complete class that highlights serialization from top to bottom, more
specifically i'm trying to garner a feel for what the above means?

//
// register the message in a factory registry ...
//
REGISTER_IN_FACTORY( AlphaMessageTypeImpl, AlphaMessageKey );

etc for all the message types.
BaseMessageType * Parse_Rx_Message( void * buffer )
{

Header * hdr = reinterpet_cast<Header *>( buffer );

if ( ! CheckHeader( hdr ) )
{
return false;
}

BaseMessageType * msg =
MessageFactoryCreate( GetMessageType( hdr ) );

if ( msg == 0 )
{
return 0;
}

if ( msg->Deserialize( buffer ) == false )
{
delete msg;
return 0;
}

return msg;
}

=========================

If you get clever, you can use a template to create message
implementations. There's a few more steps than what I show here but
this should be enough. Still a number of issues need to be solved here,
- partial deserialization issues
- endian issues
- forward compatability
- error recovery
Here again I'd really like to garner a feel for full understanding on
some of this.

Now, to process the message you can derive from MessageProcessor and
implement the methods that work for you or you can simply "cast" using
somthing like this:

template <typename T> class MessageCast
: public virtual MessageProcessor
{
T m_message;
public:

MessageCast( BaseMessageType * message )
: m_message( 0 )
{
if ( message != 0 ) {
message->ProcessMe( this );
}
}

bool Process( T w_message )
{
m_message = w_message;
return true;
}

operator T ()
{
return m_message;
}

};
Does teh Process (below) function above do a memcopy of sorts, or is
that resident in the implementation of the specific class, derived off
the base (MessageProcessor) class

I'm off to read up on Serializing.

This may be used just like dynamic_cast

e.g.

AlphaMessageType * amsg =
MessageCast<AlphaMessageType *>( base_msg );

if ( amsg != 0 )
{
// base_msg is an Alpha message - do somthing
....

Now Parse_Rx_Message will look at the appropriate header then memcopy
to the appropriate struct (either My_Stuct or My_Other_Struct) .
Trouble is I'd like to keep 'this' generic. In essense, use a
container class to handle all the memcopy etc. This 'generic'
container class will setup structs, compare structs etc. Does anyone
have an example that does this.

Wading through Koeing and would like to overhaul some 'test' code
which will hopefully help me understand the template/container aspect
of ++.


In the above design, templates enter the picture once you have
solidified the requirements. At this point, I would write some test
code that would exercise all the failure scenarios so that you can
create a more generic interface and hence do s better job designing a
class hierarchy for message implementations.


Thanks a lot.... Good start.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shaun Marshall | last post by:
Hope someone can help, for an assignment i was given a starter application package, with that starter package i had to create some employee classes and test harnesses. below iv pasted my Container...
0
by: skscpp | last post by:
What's wrong with the following code? Compiler error is at the bottom. The compiler I am using is gcc version 2.95. ============================= // traits.h =============================...
19
by: Nafai | last post by:
Hi I want to write a function which erases al the repeated elements in a range. How should be the prototype? template <class Iterator> void eraseRepeated(Iterator begin, Iterator end); ...
2
by: Patrick Kowalzick | last post by:
Dear NG, I have two containers (standard library) which are nested, e.g.: std::vector< std::vector <int> > A; std::list< std::vector<int> > B; These structures where put in another class...
6
by: Michael | last post by:
Hi, In real world, when to use class, container, class & container? It seems to me that container is more useful to number chain, class is for implementing complex porjects? Right? Please give...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
3
by: toton | last post by:
Hi, I have a container class, and I want to iterate over a portion of the container class while I insert/remove item from it. Noting down the present location & constructing iterator from there is...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.