Re: How do I do generic programming in C?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • swengineer001@gmail.com

    Re: How do I do generic programming in C?

    >
    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
    Why not use a structure containing a type value and a union of the 2
    structures that can be passed:

    typedef enum
    {
    firststructure = 1,
    secondstructure = 2
    } MyStructures;

    typedef struct
    {
    ..........
    } structure1;
    typedef struct
    {
    ........
    } structure2;

    typedef union
    {
    structure1 first;
    structure2 second;
    } MyUnion;

    typedef struct
    {
    MyStructures structur_type;
    MyUnion TheStructure;
    } TheArgument;

    Obviously you would want to name everything appropriately.
  • Peter Nilsson

    #2
    Re: How do I do generic programming in C?

    swengineer001@g mail.com wrote:

    So... is there any way of emulating templates in C?
    Or do I just have to live with duplicate functions?
    >
    Why not use a structure containing a type value and a union
    of the 2 structures that can be passed:
    <snip>
    typedef union
    {
    structure1 first;
    structure2 second;
    } MyUnion;
    >
    typedef struct
    {
    MyStructures structur_type;
    MyUnion TheStructure;
    } TheArgument;
    Two seconds thought should tell you why this is not
    a good idea for converting one struct type to another.

    --
    Peter

    Comment

    • soscpd@terra.com.br

      #3
      Re: How do I do generic programming in C?

      Dummy way:

      int Setup1400 (setup1400_t const *exp_in, setup_t *exp_out, setup_t
      const *ext_in, setup1400_t *ext_out)
      {

      if (exp_in)
      do ext_in related stuff
      else
      do ext_in related stuff

      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 ExpandSetupFrom 1400()

      Comment

      Working...