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)
baum:
Hmm... isn't arduino in c? That's what I've been using.
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.
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.
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.