From d1a14b6d329de7db3b6d30cb173a423ee8ee8ddd Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 16 Feb 2017 13:29:55 +0100 Subject: [PATCH] Add a USE_PLUGINS() macro The `USE_PLUGINS()` macro is a clever hack, to make it seem like `Kaleidoscope.use()` is type-safe. It pushes its arguments into an appropriately typed array, so anything that does not fit the criteria, will trigger a compiler error. It then never uses the array, and passes the plugins over to `Kaleidoscope.use`, adding the trailing `NULL`, making it even easier to use. Since the array this macro creates is never used, the compiler will optimize it out fully. As such, by using this macro, we incur neither any size penalties, nor any run-time penalties. Everything happens at compile-time. Fixes #100. Signed-off-by: Gergely Nagy --- src/Kaleidoscope.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Kaleidoscope.h b/src/Kaleidoscope.h index 0f07e240..f9212efa 100644 --- a/src/Kaleidoscope.h +++ b/src/Kaleidoscope.h @@ -34,6 +34,24 @@ extern HARDWARE_IMPLEMENTATION KeyboardHardware; #define KEYMAP_SIZE (sizeof(keymaps) / ROWS / COLS / sizeof(Key)) +/* + * The `USE_PLUGINS()` macro is a clever hack, to make it seem like + * `Kaleidoscope.use()` is type-safe. It pushes its arguments into an + * appropriately typed array, so anything that does not fit the criteria, will + * trigger a compiler error. + * + * It then never uses the array, and passes the plugins over to + * `Kaleidoscope.use`, adding the trailing `NULL`, making it even easier to use. + * + * Since the array this macro creates is never used, the compiler will optimize + * it out fully. As such, by using this macro, we incur neither any size + * penalties, nor any run-time penalties. Everything happens at compile-time. + */ +#define USE_PLUGINS(plugins...) ({ \ + static KaleidoscopePlugin *__p[] = {plugins, NULL}; \ + Kaleidoscope.use(plugins, NULL); \ + }) + class KaleidoscopePlugin { public: virtual void begin(void) = 0;