Polymorphism with WinAVR?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This thing has been bothering me for some time now...

I have a program where I load variables of varying sizes from EEPROM. I use a special function for this. I need to use different version of this function depending on the size of the variable to read, however I don't want to explicitly specify it when coding (especially since some variable types could be changed in near future). In C++ you can simply define polymorph functions, i.e. same name but different return types. This is not supported by WinAVR, correct?
I tried doing it with macros, sth like this:

#define eeprom_read_poly(dest, eesource) 
(
#if sizeof(dest) == 1
	(dest = eeprom_read_b8((unsigned char*) &eesource);)
#endif
#if sizeof(dest) == 2
	(dest = eeprom_read_b16((unsigned char*) &eesource);)
#endif
#if sizeof(dest) == 4
	(dest = eeprom_read_b32((unsigned char*) &eesource);) 
#endif

however it does not compile, I get the error

Quote:
missing binary operator before token "("

for each line with sizeof().
Surely there is a way to implement this. Note that this is merely for conveniency; I can write code that implements the loading, but not the "correct" way...
Any help is appreciated.
Oh, I forgot one thing - I love WinAVR!

/Jakob Selbing

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

You're violating several preprocessor rules:

* Use of sizeof operator and functions (builtin or code defined) is not recognised in #if directives
* #define directive is limited to one line only (or append backslash to wrap code to the following lines)
* Conditional compiling is not allowed in the middle of #define

Just two ideas:

1) The easier: use C++. You can compile it with WinAVR (the avr-libc manual describes the way to do it)

2) Define a true C function to access the data with a third argument to indicate size and #define a macro to call that function inserting automatically (with the macro) the size of the data.

Regards,

Carlos.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aight, thanks for the help

/Jakob Selbing