Closed ledvinap closed 7 years ago
In my opinion, too much preprocessor magic can obscure the code and make it harder to read/maintain. With the current code it is easy to see what is going on, this change would obscure that.
@martinbudden : Yes, that is why I'm asking. Preprocessor metaprogramming is fun, but it's difficult for me to find right balance between magic hidden inside macros and ease of use.
I understand. It's easy to get carried away...
let's leave this for now, I'd rather we focussed on reviewing and resolving existing PR's first. We can come back to it in the future. @ledvinap if you have any code snippets can you attach them to this issue?
@hydra: Sorry, no code yet ... basically it would use detection mechanism from https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms, possibly after adding test test values into id_def_generated.
Then just test if INVERTER_PIN_UART1
expands into IO tag (PA1), if not, then try DEFAULT_INVERTER_PIN_UART1
#define IS_IO(tag) <something> // return 1 if tag expands to io tag, 0 othrewise
#define IO_TAG_DEFAULT_GET(tag) PP_IIF(IS_IO(tag), tag, PP_IIF(IS_IO(CAT(DEFAULT_, tag), CAT(DEFAULT_, tag), tag))
With quite simple preprocessor magic, it may be possible to implement macro 'IO_TAG_DEFAULT(define, default)' that returns
default
whendefine
is not defined (and behaves as IO_TAG otherwise). Only limitation is that each supporteddefine
must be registered somewhere (ISIO_x
must be#defined
)for example:
can be replaced with:
Another possibility is `IO_TAG_DEFAULT(define)' and define macros with default value:
Any opinions? Is it worth the effort?
For example https://github.com/cleanflight/cleanflight/blob/master/src/main/drivers/serial_uart_stm32f30x.c#L42 can loose quite some lines ...