Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > How do I do gen...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 18 Topic 26198 of 26960
Post > Topic >>

How do I do generic programming in C?

by "Robbie Hatley" <see.my.signature@[EMAIL PROTECTED] > May 13, 2008 at 10:21 AM

Greetings, group.  I have a situation at work in which our software
(running
on a PC) needs to talk to two different versions of our firmware (running
on
circuit boards in boxes hundreds of feet away, communicating via RS232).

The two firmware versions (1400 and 1600) are very similar, with 1600
having
a few more members in each of several key structs used for communicating
and
storing data.  (Alas, the added members were scattered through the structs
by our firmware designer, instead of being added only at the end.  Sigh.)

I use the 1600 structs in the PC software to hold data from both firmware
types, but for communications, the correct struct MUST be used,
because the firmware is expecting a data packet of a certain length, with
the data members in a certain order.

SO, i had to create "Expand" and "Extract" functions to convert between
the two versions of the "setup_t" structure, like so:

int ExpandSetupFrom1400 (setup1400_t const *in, setup_t *out)
{
  if (!in || !out) return 666;

  memset(out, 0, sizeof(*out));

  out->CoolStages         = in->CoolStages;
  out->HeatStages         = in->HeatStages;
  out->StageWidth         = in->StageWidth;
  out->TimePeriods        = in->TimePeriods;
  (several hundred more lines of "out->foo = in->foo;")

  return 42;
} // end ExpandSetupFrom1400()

int ExtractSetupTo1400 (setup_t const *in, setup1400_t *out)
{

  ***EXACT SAME FUNCTION BODY AS "ExpandSetupFrom1400()"!!!***

} // end ExtractSetupTo1400()

This is ridiculous!  I now have two HUGE functions with the exact
same function body!  The only difference is the paramater types.

I suppose I could put the function body in a separate C file and
#include it, like so:

int ExpandSetupFrom1400 (setup1400_t const *in, setup_t *out)
{
   #include "common_body.c"
}

int ExtractSetupTo1400 (setup_t const *in, setup1400_t *out)
{
   #include "common_body.c"
}

But that makes it impossible to assign breakpoints or use the
debugger.

Is there some way of switching parameter types at runtime, ala
C++ "templates"?  Basically, "generic programming", but in C rather
than in C++?

I tried some ideas, but they didn't work.  For example:

int ExpandExtract (void * in, void * out)
{
  if (!in || !out) return 666;

  memset(out, 0, sizeof(*out));
  out->CoolStages         = in->CoolStages;  // ERROR!
  out->HeatStages         = in->HeatStages;  // ERROR!
  out->StageWidth         = in->StageWidth;  // ERROR!
  out->TimePeriods        = in->TimePeriods; // ERROR!
  (several hundred more lines of "out->foo = in->foo;")
  return 42;
}

(Compiler screams: "Error! Can't access members of objects
via void pointers" or words to that effect.)

If this was C++, I'd make the expand/extract function a template:

template<class A, class B>
int ExpandExtract (A* in, void B* out)
{
   out->foo = in->foo;
   out->bar = in->bar;
   etc, etc, etc...
}

and call it like so:

   ExtractExpandSetup<setup_t, setup_1400_t>(a,b);

So... is there any way of emulating templates in C?
Or do I just have to live with duplicate functions?

-- 
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
 




 18 Posts in Topic:
How do I do generic programming in C?
"Robbie Hatley"  2008-05-13 10:21:28 
Re: How do I do generic programming in C?
roberson@[EMAIL PROTECTED  2008-05-13 17:45:12 
Re: How do I do generic programming in C?
viza <tom.viza@[EMAIL   2008-05-13 11:13:00 
Re: How do I do generic programming in C?
"swengineer001@[EMAI  2008-05-13 11:53:29 
Re: How do I do generic programming in C?
Peter Nilsson <airia@[  2008-05-13 15:03:25 
Re: How do I do generic programming in C?
"soscpd@[EMAIL PROTE  2008-05-13 16:28:32 
Re: How do I do generic programming in C?
"Robbie Hatley"  2008-05-14 18:14:42 
Re: How do I do generic programming in C?
Keith Thompson <kst-u@  2008-05-14 19:07:17 
Re: How do I do generic programming in C?
Ian Collins <ian-news@  2008-05-14 11:33:05 
Re: How do I do generic programming in C?
Nick Keighley <nick_ke  2008-05-14 00:46:46 
Re: How do I do generic programming in C?
Ian Collins <ian-news@  2008-05-14 20:05:15 
Re: How do I do generic programming in C?
Flash Gordon <spam@[EM  2008-05-14 22:25:39 
Re: How do I do generic programming in C?
Szabolcs Borsanyi <bor  2008-05-14 01:47:26 
Re: How do I do generic programming in C?
Paul Hsieh <websnarf@[  2008-05-14 15:33:49 
Re: How do I do generic programming in C?
user923005 <dcorbit@[E  2008-05-14 15:52:33 
Re: How do I do generic programming in C?
user923005 <dcorbit@[E  2008-05-14 16:01:17 
Re: How do I do generic programming in C?
Thad Smith <ThadSmith@  2008-05-14 19:58:35 
Re: How do I do generic programming in C?
George Peter Staplin <  2008-05-18 18:08:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 16:25:11 CDT 2008.