I'm using a union so I can break my ints and floats into bytes so they can be saved to an eeprom by a microcontroller. There are 12 "banks" each having 12 objects. (the code below is abbreviated hopefully avoid distractions.)
I have a something working, but I'd like to do something less ugly and more reusable and scalable.
I thought an array of pointers might work. I've used typedef to create an array of pointers to functions and member functions, but I'm not really sure how to do this with object arrays. I'm not even sure that would be any less ugly.
so I can break my ints and floats into bytes so they can be saved to an eeprom
you can do this directly.
float f;
unsigned char* cp = (unsigned char*)&f;
cp[0] .. cp[n] are the bytes of f -- you can use sizeof for the different types you need. Byte order of the machine may throw you -- remember that cp[0] could be most or least significant byte of an integer type depending on the local OS/platform.
you can craft all sorts of ways to do this and which approach may vary a bit depending on *what else* you need besides just byte level access. Are there any additional requirements beyond the byte access?
I might be paranoid, but I'm working with 8kb RAM, so I'm not sure I'd want to use vector.
The lengths of the arrays will never change.
This still would have me adding all the different objects by name each time. I probably didn't ask my question very well.
so I can break my ints and floats into bytes so they can be saved to an eeprom
you can do this directly.
float f;
unsigned char* cp = (unsigned char*)&f;
cp[0] .. cp[n] are the bytes of f -- you can use sizeof for the different types you need. Byte order of the machine may throw you -- remember that cp[0] could be most or least significant byte of an integer type depending on the local OS/platform.
you can craft all sorts of ways to do this and which approach may vary a bit depending on *what else* you need besides just byte level access. Are there any additional requirements beyond the byte access?
For page writing to the eeprom the bytes need to be in a char array. The objects are used in the program and saved to eeprom by bank_num. So data would be saved or retrieved by the object names. so like this:
1 2 3
bits[1].inum
ratio[1].fnum
period[1].inum
but NOT this:
1 2 3
bits[1].inum
bits[2].inum
bits[3].inum
I think it would be easiest if I could somehow refer to all of this in a for loop in by elements [0] through [n], etc.
I was thinking something like this, but I think my syntax is wrong and I'm not sure it would take elements, especially not as variables.
For page writing to the eeprom the bytes need to be in a char array.
No offense, but I do not believe you :)
I suspect a char* will work just fine there; in most cases (almost all) a char* and char array are interchangeable. Not always, so I could be wrong, but ....
the reverse also works: (I think this is what you were trying to do above?)
consider this (garbage code, but on an embedded system, it may be just what the DR ordered)
int index = 0;
char dafuq[1000];
int* ip1 = (int*) &dafuq[index];
index += sizeof(int);
int* ip2 = (int*) &dafuq[index];
index += sizeof(int);
double * dp1 = (double*) &dafuq[index];
index += sizeof(double);
...
your data will be in dafuq as a char array. you just made handles into the array to deal with groups of bytes as if they were the types in question. Any basic type (not stl containers, nothing with pointers inside it, same rules as serialization) can be done as above.
I believe it writes bytes to I2C from sequential memory. Any other type and it won't compile. (I've tried)
double d;
char* cp = (char*) &d;
cp is sequential bytes, sizeof(double) in length.
if you are trying to avoid calling the write a bunch of times for very small things and want to call it once for a larger block, the above reversed approach will do that, as will a carefully aligned struct and some pointer magic. I don't think the struct costs any more than the char array, and its a lot cleaner. deep down inside, its doing exactly the same thing, a block of bytes sliced up whatever way.