473,396 Members | 1,907 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,396 software developers and data experts.

Template class .. Porting C to C++

Slowly embracing C++ and as such I've got C code (see snippet below)
I'd like to essentially port to C++. The idea is to

1. Use a TEMPLATE class to handle the 'double' buffering scheme
outlined in the code. Perhaps an operator= approach would be ideal.
I've looked at benchmarks per these newsgroups on std::copy and
realize it's too slow when compared to memcopy.
2. Inside the template class, create a function that'll return the
'latest' double buffered copy of the the appropriate struct
3. Perhaps create a function to compare the two buffers.

Of course I'll leave the class to implement whatever else I could
dream of. The question. Could I garner assistance on an
implementation approach. Perhapts the LDS_MESSAGE_STRUCT version is a
good start.

Assume the LSD_MESSAGE_STRUCT is

typedef struct _LDS_MESSAGE_STRUCT
{
unsigned int x : 16;
unsinged int y : 16;
} LDS_MESSAGE_STRUCT ;
Just unsure which direction to run.

void Rx_SMML_Msg()
{
char buffer[PAYLOAD];
STATUS status;
int size, i;
LDS_HEADER_RECORD MessageID, Message_type;
// call Rx_Message() to process received messages
Rx_Message( (UCHAR*)buffer, &size, &MsgStatus );

// check status
if( MsgStatus != OK )
{
rx_error_count++; // increment error count
printf( "Rx_Message interrupt process error -- %d\n",
rx_error_count);
return;
}

MessageID = ((LDS_HEADER_RECORD *)buffer)->ID;

printf("\n\Received msg ==> MessageID = %x\n",MessageID );

// parse out message type - check error bit
if ( (MessageID & MSG_ID_ERROR_MASK) == 0 )
{
// Good message. Parse out message type from MessageID
Message_type = (((( MessageID >> 4 ) & MSG_ID_MASK ) <<3 )) | (
MessageID & MSG_ID_MASK );

switch( Message_type )
{
case LDS_Control_Message__Control:
if( lds_system_control_message_ready ) // check if not
emptied
{
lds_system_control_message_in_use++;

printf( "LDS_System_Control error ==> buffer use -- %d\n",
lds_system_control_message_in_use);
}
else
{
printf( "LDS_System_Control Message loaded !!!\n");

// start double buffering scheme.

i = lds_system_control_message_index ^ 1;
memcpy( &lds_system_control_message[i], buffer,
sizeof(LDS_MESSAGE_STRUCT) );
lds_system_control_message_ready = TRUE;
// toggle index 0/1
lds_system_control_message_index ^= 1;
}
break;

case RDS_Message__System_Control:
if( rds_system_control_message_ready ) // check if not
emptied
{
rds_system_control_message_in_use++;

printf( "RDS_System_Control error ==> buffer use -- %d\n",
rds_system_control_message_in_use);
}
else
{
printf( "RDS_System_Control Message loaded !!!\n");

// start double buffering scheme.

i = rds_system_control_message_index ^ 1;
memcpy( &rds_system_control_message[i], buffer,
sizeof(RDS_MESSAGE_STRUCT) );
rds_system_control_message_ready = TRUE;
// toggle index 0/1
rds_system_control_message_index ^= 1;
}
break;

// LOTS more like the two case statement above

default:// unknown message
printf("UNKNOWN MESSAGE RCV'D !!!\n");
break; // add an error
}
}
else//message error bit set
{
switch( MessageID )
{
case LDS_System_Control_Err:
lds_system_control_message_error++;
printf( "LDS_System_Control_Msg_Err_Bit -- %d\n",
lds_system_control_message_error);
break;
case RDS_System_Control_Err:
rds_system_control_message_error++;
printf( "RDS_System_Control_Msg_Err_Bit -- %d\n",
rds_system_control_message_error);
break;

default:
printf( "Message_Err of some sort -- %d\n");
break; // add an error
}
}
}

return;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #1
2 2009
ma******@pegasus.cc.ucf.edu (Michele) wrote in message news:<a5*************************@posting.google.c om>...
Slowly embracing C++ and as such I've got C code (see snippet below)
I'd like to essentially port to C++. The idea is to

1. Use a TEMPLATE class to handle the 'double' buffering scheme
outlined in the code. Perhaps an operator= approach would be ideal.
I've looked at benchmarks per these newsgroups on std::copy and
realize it's too slow when compared to memcopy.


And memcpy is too dangerous when compared to std::copy.

The solution we selected on our system was to specialize the
implementation of std::copy when it could determine, at compile time,
that it was safe to call memcpy instead of inlining the code itself.
We did the same thing with std::fill and memset. Memset/memcpy=evil.

our copy uses memcpy when both iterators are pointers, both pointer's
non-cv qualified value_types are the same, and the non-cv qualified
value_type is a builtin type.

-joshua lehrer
factset research systems
NYSE:FDS

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #2
"Joshua Lehrer" <us********@lehrerfamily.com> wrote in message
news:31**************************@posting.google.c om...
And memcpy is too dangerous when compared to std::copy.
Could you elaborate this? How is copy more or less dangerous when applied
to raw storage as presented here?
We did the same thing with std::fill and memset. Memset/memcpy=evil.


Because?

Paul

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #3

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

Similar topics

3
by: Yamin | last post by:
Hi all, I was porting some code over to GCC, when I came across some code. GCC gave a compile error. I looked it over and to me at least it looked like it should have never compiled. But this...
8
by: Exits Funnel | last post by:
Hello, I've been tasked with porting some C++ code from Windows to Linux. The following excerpt is giving me trouble: //BEGIN CODE #include <string> class TempTestBase_t {
2
by: weatherman | last post by:
Dear all, When porting some code from g++ 2.95/3.2 to g++ 4.01 we encountered some difficulties with the following construction. The code is known to compile on g++ 2.95 and g++ 3.2.2. This...
3
by: derek.google | last post by:
While porting an application to Linux I hit a strange compiler error with GCC 3.3.2. Here is the most stripped down version of the code I could write: 1 template <typename T> struct SmartPtr 2...
2
by: Jay | last post by:
Hi, Can anyone tell me why the following code doesn't work on 7.1? It's been ok with previous versions. The error given by the linker is: rtwnd.obj : error LNK2019: unresolved external symbol...
4
by: Chuck Chopp | last post by:
I have an application that I originally built using Visual Studio .NET 2003 as native C++ . This application includes a template class that was derived from the string class that's part of the C++...
2
by: Ole Nielsby | last post by:
Porting a project from VC8 to GCC, I stumbled on this: I have certain templates that use STL collections with a value type that is a pointer to a template parameter type. When I declare...
5
by: prakash.mirji | last post by:
I am using evaluation copy of RW 9.0 for porting one of C++ application on RHEL4 (x86 platform). We are getting some issues into RW template classes. Please need assistance on this issue. Here...
2
by: =?ISO-8859-1?Q?Andr=E9_Luiz_Carvalho?= | last post by:
Hi there, I'm porting an application from Java to C++ / BREW so I can't use the stl, therefore, I have to implement the basic structures that the app use in Java. I'm implementing a Hashtable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.