How to generically pass parameters to a function. General C++ question

Hello,

I would like some help writing a function. The need is the following:

I have an arduino and an LCD display attached on it.
I use lcd.print function to write things on the LCD.
I need to write a function that enables or disables all writes on the LCD replacing the lcd.print depending on a "condition".

Something like that:

void my_lcd_print (?????????  txt)
{
  if (condition)
  lcd.print (txt);
}

"condition" will be a globally defined variable.
I need to pass to this function (my_lcd_print) all kind of types of arguments lcd.print accepts (int, strings etc) without casting compilation problems.

Thanks for the help!

Not familiar with the LCD screen, but as far as my (limited) knowledge of Arduino programming goes, its rather impossible to define an argument for a method that can take multiple or all forms. I would suggest to either store the to-be-printed material in global variables, and handle the different formats with different methods, or use an identifier in the argument to tell the method what kind of data is being printed. Perhaps with a switch case or something.

int a
string b
byte c

int identifier

void print(identifier){
   switch(identifier)
     case 1:  print a
     case 2:  print b
     case 3:  print c

}

I have no clue exactly what the extend of the expected data formats is, but this is a general solution which should be easily implementable

EDIT: perhaps you can set up 'condition' as to take care of selecting the correct method to handle the data. Some actual code would be useful, primarily the part which fills/selects the data for the final printing. How do you decide which variables are printed? The casting question I cannot answer, but would be useful if the method would simply print what you feed it...

Turn the problem inside-out, and just use a macro.

AWOL:
Turn the problem inside-out, and just use a macro.

Can U please give me a hint on macros?

Templates are custom-made for that sort of thing.

#define MY_LCD_PRINT(x) do {if (condition) lcd.print (x);  } while (0)

AWOL:

#define MY_LCD_PRINT(x) do {if (condition) lcd.print (x);  } while (0)

Thx AWOL.
will try that.....

Is there a "non compiler" way to do that? (pass arguments)?

Macros are non-compiler - they use the preprocessor.

For a compiled, type-safe method, do as Nick suggests and use templates.

AWOL:

#define MY_LCD_PRINT(x) do {if (condition) lcd.print (x);  } while (0)

This seems a lot like what OP already gave, so I guess the difference is is that this macro can take any argument? (string byte int)

A macro is simply a text-replacement mechanism, so yes, it can take any type as argument.

AWOL:
A macro is simply a text-replacement mechanism, so yes, it can take any type as argument.

Ok thanks....Im now at a level that I can program virtually anything I want on Arduino, but it is things like this I still have to read up about. My JAVA-only background doesnt help.... :wink:

This is how you would do it with templates:

boolean condition = true;

template <typename T> void my_lcd_print (T arg);
template <typename T> void my_lcd_print (T arg)
  {
  if (condition)
    Serial.println (arg);
  }

void setup ()
  {
  Serial.begin (115200);
  my_lcd_print (42);
  my_lcd_print ("Hi there");
  my_lcd_print (12.34);
  }  // end of setup

void loop () { }

Output:

42
Hi there
12.34

I'm passing three different types there, to a single function. The "template" part makes the compiler generate one function per type.

I have not needed to use templates but they sound rather neat.
Automatic generation of an overloaded function by the compiler.

Yes, they're neat.

That how the STL (standard template library) does things like make lists of any type. The actual code to do a list is a templated function (or functions) so it can handle any type.