Re: outputting data
From: ma740988 (ma740988_at_pegasus.cc.ucf.edu)
Date: 11/23/03
- Next message: ma740988: "Re: outputting data"
- Previous message: Gavin Deane: "Re: Whats the difference? string and std::string"
- In reply to: ellie fant: "Re: outputting data"
- Next in thread: ellie fant: "Re: outputting data"
- Reply: ellie fant: "Re: outputting data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 23 Nov 2003 07:40:42 -0800
"ellie fant" <pcrcutitout1000011@uko2.co.uk> wrote in message news:<1069555327.10197@news.minx.net.uk>...
> "ma740988" <ma740988@pegasus.cc.ucf.edu> wrote in message
> news:a5ae824.0311221453.11c5c5a8@posting.google.com...
> > I've been banging my head on a solution that'll print the contents of
> > a template type for sometime.
> >
> > Consider
> >
> > template <class T>
> > class BUFFER
> > {
> > public:
> >
> > BUFFER();
> > ~BUFFER();
> > void Store(unsigned char const* buffer)
> > {
> >
> > store_index ^= 1;
> > memcpy( &msg[store_index], buffer, sizeof(T));
> > }
> >
> > private:
> >
> > static const int SIZE = 2;
> > T msg [ SIZE ];
> >
> > unsigned int store_index;
> >
> > };
> >
> >
> > int main()
> > {
> >
> > struct MSGA
> > {
> > unsigned char idx;
> > unsinged char jdx;
> > }
> >
> > unsigned char buffer [ 2 ] = { 12, 1 };
> > BUFFER<MSGA> msga;
> > msga.Store ( buffer );
> >
> > }
> >
> > I'd like to have a print function such as
> >
> >
> > template<class T>
> > void BUFFER<T>::Print()
> > {
> > printf("%s, %s\n", msg[0].toString().c_str(),
> > msg[1].toString().c_str());
> > }
> >
> > where the toString function for the class T and as such will print the
> > contents of T for any struct. For instance the one above and perhaps
> > another that looks like this
> >
> > struct MSGB
> > {
> > MSGA aa; // uses MSGA above
> > unsinged char kk : 4;
> > }
> >
> Why don't you make the structs print themselves?
> I noticed a few errors in your code so I have made some changes but this may
> not be to your needs:
>
> #include <iostream>
>
> template<class T>
> class BUFFER{
> public:
> void StoreMSGA(unsigned const char* buffer)
> {
> msg.idx = buffer[0];
> msg.jdx = buffer[1];
> }
> void StoreMSGB(unsigned const char* buffer)
> {
> msg.kk = buffer[0];
> msg.aa.idx = buffer[1];
> msg.aa.jdx = buffer[2];
> }
> void Print(){
> msg.Print();
> }
> private:
> T msg;
> };
>
> struct MSGA{
> unsigned char idx;
> unsigned char jdx;
> void Print(){printf("%c, %c\n",idx, jdx);}
> };
>
> struct MSGB{
> MSGA aa;
> unsigned char kk;
> void Print(){printf("%c , ",kk); aa.Print();}
> };
>
> int main()
> {
> unsigned char buffera[2] = { 12, 1 };
> unsigned char bufferb[3] = { 65,66,67};
> BUFFER<MSGA> msga;
> msga.StoreMSGA(buffera);
> msga.Print();
> BUFFER<MSGB> msgb;
> msgb.StoreMSGB(bufferb);
> msgb.Print();
>
> return 0;
> }
>
> The fact that you I needed to create a store function for each type is
> probably poor design but your implementation didn't seem to work , so
> perhaps you need to redesign the store function.
> HTH
For starters, Perhaps I should show the Retrieve function in
conjuction with the Store function.
void BUFFER<T>::Store( unsigned char const* buffer)
{
store_index ^= 1;
memcpy( &msg[store_index], buffer, sizeof(T));
new_msg_available = true;
store_count++; // increment the message stored count
}
// retrieve the most CURRENT item at ALL times
template<class T>
void BUFFER<T>::Retrieve(T& data)
{
if (new_msg_available)
{
memcpy( &data, &msg[store_index], sizeof(T));
retrieve_count++;
new_msg_available = false;
}
}
What this means is I've lots (in excess of 15+) structs that i'll be
storign and Retrieving. Here's a more robust example.
enum BOGUS_MSG { msg1Msg,
msg2Msg };
// For simplicity make bit fields 8 deep
struct MSG1
{
unsigned int jdx1 : 8;
unsigned int jdx2 : 8;
unsigned int jdx3 : 8;
unsigned int jdx4 : 8;
};
struct MSG2
{
MSG1 msg;
unsigned int jdx1 : 8;
unsigned int jdx2 : 8;
unsigned int jdx3 : 8;
unsigned int jdx4 : 8;
};
int main ( void )
{
BOGUS_MSG msgType;
MSG1 localMsg;
// Done once at initilization.
BUFFER<MSG1> msg1;
// for test purposes create a buffer with bogus values. The real
data
// received off the port will vary with the max being 512
unsigned char buffer [ 4 ] = { 12, 1, 15, 5 };
msgType = msg1Msg;
switch ( msgType )
{
case msg1Msg:
msg1.Store ( buffer );
break;
case msg2Msg:
break;
default:
break;
}
// then later .. in a 'separate' thread. we'll come along and
Retrieve
msg1.Retrieve(localMsg);
};
Now out of curiosity I'd like to know what the msg[0] and msg[1] has
as result for MSG1. Of course the same basic premise applies for ALL
the different Stores and Retrieves on individual stucts.
Trouble is it seems to defeat the purpose of having a template class
if i have to StoreMsg1 .......... StoreMsg15+ No??
What I suspect though is i'll have to have 15+ 'print' functions or
your thoughts/example? Here again the design aspects .. well.
- Next message: ma740988: "Re: outputting data"
- Previous message: Gavin Deane: "Re: Whats the difference? string and std::string"
- In reply to: ellie fant: "Re: outputting data"
- Next in thread: ellie fant: "Re: outputting data"
- Reply: ellie fant: "Re: outputting data"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|