Go Down

Topic: A function to handle multiple datatypes (Read 7341 times) previous topic - next topic

Chris Parish

Hello all,

If there any way that I can write a function that will accept multiple datatypes?
This is what I am trying to do:
I want a function that I can pass either a string of characters or an integer or an object and it will treat it as a stream of bytes so that I can write them out to the serial port one byte at a time. The function will also include things like byte stuffing, but I can deal with that later.

I remember that there was an EEPROMWriteAnything function that used template <class> and things like that but I can't figure out how to adapt it to my needs. I tried and all I get is errors; things like "expected ',' or '...' before '&' token" and "error: ISO C++ forbids declaration of 'T' with no type"

Help?

jwatte

All you neediest function overloading. Write both functions andngive them the same name and the compiler will figure it out based on the data type of the argument.

Coding Badly


Certainly....

Code: [Select]
void SendIt( void * const raw, unsigned size )
{
  uint8_t const * rover;

  rover = (unit8_t const *) raw;
  while ( count > 0 )
  {
    Serial.write( *rover );
    ++rover;
    --count;
  }
}

SendIt( &MyInt, sizeof(MyInt) );


Nick Gammon


I tried and all I get is errors; things like "expected ',' or '...' before '&' token" and "error: ISO C++ forbids declaration of 'T' with no type"


That's so sad, I can't tell you.

Read this before posting a programming question

What code? What errors? <sigh>

Not "things like". Copy and paste the errors.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

pico

#4
Nov 02, 2012, 07:24 am Last Edit: Nov 02, 2012, 07:29 am by pico Reason: 1

Hello all,

If there any way that I can write a function that will accept multiple datatypes?



Function overloading is really just syntactic sugar for having multiple functions with the same name but different formal parameter lists. Name mangling behind the scenes ensures the linker can tell them apart, but they are all really completely different functions, of course.

If you really want just a single function, you could consider passing a void pointer to your function that holds the address of your variable/array/structure. You would have to pass a second argument as well to tell the function how many bytes the variable/array/structure holds. Defining a macro using sizeof() might be a neat solution for passing the second argument automatically.

Less coding and less flash memory than overloading.

Edit: Oops, just saw Coding Badly already did this. Must read more carefully before posting next time!
WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Nick Gammon

Please don't. Passing void basically defeats type checking. And then all the extra code to work out what type it has received will negate any savings of only having one function.

You now have one complex function that isn't type safe, rather than two simple functions which are.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

pico

WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Nick Gammon

The point is, you don't want to defeat type checking. Why do you think compiler-writers built that in?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Chris Parish

Quote
That's so sad, I can't tell you.


Apologies, Nick. Those were actually the errors I got when I tried to compile my sketch with the EEPROMWriteAnything function in it. Word for word, copied and pasted. I just didn't make it clear. Sorry. Just finished a night shift.

So, which approach should I use? Passing a void pointer or writing multiple functions?

Chris

Nick Gammon

Multiple functions. If you have problems post your code.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

However I don't have any objections to templates, which, in effect, produce multiple functions.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Chris Parish

How do templates work? I have tried googleing but I can't seem to find a nice concise guide.

pico


The point is, you don't want to defeat type checking.

Yes. Sometimes you do.

Why do you think compiler-writers built that in?

Equally, why do you think they built in void pointer?

Type checking is very useful, a great invention. Sometimes however, it is nice to be able to side-step it if it gets in your way. C allows that.



WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Nick Gammon

EEPROMWriteAnything uses templates. Here:

Code: [Select]

#include <Arduino.h>  // for type definitions
#include <EEPROM.h>

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
   const byte* p = (const byte*)(const void*)&value;
   unsigned int i;
   for (i = 0; i < sizeof(value); i++)
   EEPROM.write(ee++, *p++);
   return i;
}


The templated type T is used to write to EEPROM.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


Type checking is very useful, a great invention. Sometimes however, it is nice to be able to side-step it if it gets in your way. C allows that.


If you buy a gun, you can shoot yourself in the foot. If you want to.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up