gedankenexperimenter / Kaleidoscope

Firmware for the Keyboardio Model 01 and other keyboards with AVR or ARM MCUs.
http://keyboard.io
GNU General Public License v3.0
0 stars 1 forks source link

Replacing the Key union #10

Closed gedankenexperimenter closed 6 years ago

gedankenexperimenter commented 6 years ago

The problem

Unfortunately, unions are so restricted in C++ that they are all but useless. The Key union type that I've written for Kaleidoglyph isn't compliant with the standard, and only works because of a gcc extension. This does allow (in my opinion much greater code clarity, but it is technically undefined behaviour if any member of a union is read after a different member of that union is written. Therefore, the goal is now to replace that Key union (and any other unions) with a class or set of classes that are not crippled by the failure of the standard to provide reasonable guarantees of behaviour.


Requirements

The Key type and its variants must meet all of the following criteria:


Option 1

The Key type becomes a base class with only one data member: a two-byte integer (uint16_t).

Variant types such as KeyboardKey are derived from the base class Key, and do explicit bit-twiddling to access the different bit field members.

Option 2

The Key type is a class with one two-byte data member.

Variant types (e.g. KeyboardKey) are not inherited, and store their data in bit fields that handle the bit-twiddling for us. We use type-conversion functions to convert from Key to the variants.

Option 3

Like option 2, but the variant types are defined inside the Key class in the public section, so they're named Key::Keyboard instead of KeyboardKey. This would be nice and clear for the built-in types, but not as good for the plugin types (e.g. Key::Qukeys won't be possible, but QukeysKey will).

gedankenexperimenter commented 6 years ago

This is now done. See #12.