A simple (rather contrived) example.
As Steve mentions - simulation is really not happening here.
There are some advantages over switch statements (at least for lots of options) - the code is smaller and the options run in constant time.
It also 'forces' the code to be split into multiple small macros rather than one large 'do-it-all' macro - which makes for more readable code.
I used an Arduino Nano - which has limited memory - so I can use 16 bit 'pointers'. If the MCU you are using has a larger address space JumpTable needs to be 32 bit. On the esp32 in might be necessary to 'cast' the macro address (FCM_Name) to a MX_UINT32 i.e. FCL_JUMPTABLE[0] = (MX_UINT32)FCM_Name;
One thing demonstrated here is the check on a option being defined - if(fn) - you can have a sparse table if needed.
I don't demonstrate passing arguments (or returning a result) - but it is certainly possible.
Martin
Switch with more decisions
-
- Valued Contributor
- Posts: 1453
- http://meble-kuchenne.info.pl
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 707 times