Variable data type as function argument

I am working on a modified EEPROM library which allows the easy storage of ints, floats, and longs, in addition to the standard bytes. The functions are not complex, however, I must have a write function and a read function for every type (writeInt, writeLong, writeFloat; readInt, readLong, readFloat). What I want to do is condense these functions into one function which would accept any data type, figure ou the variable type, and then write the according data. Is this possible with variable arguments (...)? If so, how?

Right now, I am using this protocol:

Integers are written like this: (each comma separated value represents an address)

'i',1st byte of int, 2nd byte

longs:

'l',1st, 2nd, 3rd, 4th

floats:

'f',1st,2nd,3rd,4th

Thanks in advance!

baum

I am working on a modified EEPROM library which allows the easy storage of ints, floats, and longs, in addition to the standard bytes.

In C or C++? If it is in C++, templates are your friend. Not trivial, but not all that difficult, either.

Hmm... isn't arduino in c? That's what I've been using. :frowning:

baum:
Hmm... isn't arduino in c? That's what I've been using. :frowning:

arduino uses the avr-gcc compiler which is C++. C is a subset of C++ so you can intermix C and C++ in your sketches. However C functions don't allow for overloading of function arguments, C++ does. At least that is my understanding of the situation, but I'm still learning. :wink:

I'll try it. Just reading http://www.cplusplus.com/doc/tutorial/templates/

:slight_smile:
baum

Doesn't the EEPROM WriteAnything use templates? - You could take a look at that.

I looked at that... however, the read function requires you to know what you are writing back. My idea was to put a letter in front of the data to signify what type of data follows, and how long it is. However, is there a way using templates to find the datatype? sizeof won't work because both floats and longs are 4 bytes.

baum

the read function requires you to know what you are writing back

I'm sorry, that doesn't make a great deal of sense - a read function should not be writing back.

Sorry... I don't completely understand halley's code:

#include <EEPROM.h>

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

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

Why does the read function require a parameter T& value? and why does it return an int? Is "value" the variable which is read to?

It requires a reference to return the value read.
The int returned is the number of bytes read.

Alright... looks fine. I guess I'll just be using that one. One more question: Is there a way, when using templates, to get the datatype of a variable without writing a function or specialized template for every type? I have seen typeid() used before but supposedly it is not a good idea to use. Also, how is the CPU able to distinguish between, say, int and unsigned int? Consider the two numbers:

int number1 = -100;
unsigned int number2 = 65436;

I think those both have the same binary representation.

baum

Also, how is the CPU able to distinguish between, say, int and unsigned int?

It can't.
That's for you to do.

OK. Thanks a lot for all the help!

baum