I am programming an app that allows a user to select the bit-size of an integer storage and do math with it. They can select 8, 16, 32, or 64 bit storage as signed or unsigned. This is set and changed at runtime, and the app will do math with the given type.
User Interaction Example:
- Enter 16 bit mode
- Type 5C
- Press Plus
- Type 2A
- Press Evaluate
- return { value1.getSigned16() + value2.getSigned16(); }
I would like to avoid programming 8 cases for each operator. I figure a pointer or function-pointer might work well for this. The evaluate method doesn't care what size of an integer I'm using as long as they are both the same. Problem is I can't figure out how to implement this, as the pointers care what kind of variable is returned. I considered using a generic pointer but that doesn't help me when I need to dereference, I still would need the 8 cases.
value1.getProperSize() + value2.getProperSize();
// * This obviously won't work
int* getProperSize() {
if (size == 16) return (int16_t)storageValue;
if (size == 32) return (int32_t)storageValue;
// Etc...
}
Any thoughts or suggestions on attacking this problem are greatly appreciated.