Pages: [1] 2 3 ... 5   Go Down
Author Topic: A function to handle multiple datatypes  (Read 5378 times)
0 Members and 1 Guest are viewing this topic.
Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12773
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Certainly....

Code:
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) );
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 828
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: November 02, 2012, 01:29:36 am by pico » Logged

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.)

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 828
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Passing void basically defeats type checking.

Yes. That's the point.

 

Logged

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.)

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The point is, you don't want to defeat type checking. Why do you think compiler-writers built that in?
Logged

Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Multiple functions. If you have problems post your code.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

However I don't have any objections to templates, which, in effect, produce multiple functions.
Logged

Poole
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
I'm not a complete idiot. Some bits are missing
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
God Member
*****
Karma: 32
Posts: 828
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



Logged

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.)

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

EEPROMWriteAnything uses templates. Here:

Code:
#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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2 3 ... 5   Go Up
Jump to: