From 535fc291b68534d4da53c894d9f84cb185de72d5 Mon Sep 17 00:00:00 2001 From: Aiden Woodruff Date: Fri, 21 Sep 2018 21:25:50 -0500 Subject: Abuse the preprocessor instead of using typeof Add -Wextra to CFLAGS Created foreach.h with the "magic" macros Update calls in life.c Signed-off-by: Aiden Woodruff --- src/foreach.h | 24 ++++++++++++++++++++++++ src/life-macros.h | 7 +------ src/life.c | 20 ++++++++++---------- 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 src/foreach.h (limited to 'src') diff --git a/src/foreach.h b/src/foreach.h new file mode 100644 index 0000000..e684b93 --- /dev/null +++ b/src/foreach.h @@ -0,0 +1,24 @@ +#ifndef NARG +#define NARG(...) NARG_(__VA_ARGS__, RSEQ_N) +#define NARG_(...) NARG_N(__VA_ARGS__) +#define RSEQ_N 8, 7, 6, 5, 4, 3, 2, 1, 0 +#define NARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N +#endif + +#ifndef foreach +#define FOREACH_0(func) } while (0) +#define FOREACH_1(func, first) func(first); FOREACH_0(func) +#define FOREACH_2(func, first,...) func(first); FOREACH_1(func, __VA_ARGS__) +#define FOREACH_3(func, first,...) func(first); FOREACH_2(func, __VA_ARGS__) +#define FOREACH_4(func, first,...) func(first); FOREACH_3(func, __VA_ARGS__) +#define FOREACH_5(func, first,...) func(first); FOREACH_4(func, __VA_ARGS__) +#define FOREACH_6(func, first,...) func(first); FOREACH_5(func, __VA_ARGS__) +#define FOREACH_7(func, first,...) func(first); FOREACH_6(func, __VA_ARGS__) +#define FOREACH_8(func, first,...) func(first); FOREACH_7(func, __VA_ARGS__) + +#define CONCAT1(x, y) x##y +#define CONCATENATE(x, y) CONCAT1(x, y) + +#define foreach(func, ...) do { CONCATENATE(FOREACH_,NARG(__VA_ARGS__))(func, __VA_ARGS__) +#endif + diff --git a/src/life-macros.h b/src/life-macros.h index 32eb3f0..f2a6a8b 100644 --- a/src/life-macros.h +++ b/src/life-macros.h @@ -2,9 +2,4 @@ #define has(var, bit) (((var) & (bit)) == (bit)) #endif -#ifndef foreach -#define foreach(func, count, first,...) \ - {typeof(first) things[] = {__VA_ARGS__}; \ - func(first); \ - for (int _i = 0; _i < count - 1; _i++) func(things[_i]);} -#endif +#include "foreach.h" diff --git a/src/life.c b/src/life.c index 7fa8a8c..24117fa 100644 --- a/src/life.c +++ b/src/life.c @@ -127,7 +127,7 @@ int main (int argc, char * argv[]) { x = width >> 2; y = height >> 2; while (ch != 'q') { - foreach(werase, 4, stdscr, board, stat_bar, entry); + foreach(werase, stdscr, board, stat_bar, entry); if (playing == FALSE) { // Numpad and arrow directions switch(ch) { @@ -189,26 +189,26 @@ int main (int argc, char * argv[]) { break; case 'h': wtimeout(board, -1); - foreach(werase, 3, stdscr, board, stat_bar); + foreach(werase, stdscr, board, stat_bar); stat_bar_print(stat_bar, "Press any key to return"); print_help(board); - foreach(wnoutrefresh, 3, stdscr, board, stat_bar); + foreach(wnoutrefresh, stdscr, board, stat_bar); doupdate(); getch(); wtimeout(board, timeout_val); break; case 'w': wtimeout(board, -1); - foreach(werase, 4, stdscr, board, stat_bar, entry); + foreach(werase, stdscr, board, stat_bar, entry); stat_bar_print(stat_bar, "Press any key to return"); print_copying_warranty(board); - foreach(wnoutrefresh, 3, stdscr, entry, stat_bar); + foreach(wnoutrefresh, stdscr, entry, stat_bar); doupdate(); getch(); wtimeout(board, timeout_val); break; case CTRL('r'): - foreach(werase, 4, stdscr, board, stat_bar, entry); + foreach(werase, stdscr, board, stat_bar, entry); stat_bar_print(stat_bar, "Click Cancel to exit or Done to exit and save rule"); wrefresh(stat_bar); wrefresh(rule_entry_box); @@ -234,14 +234,14 @@ int main (int argc, char * argv[]) { } else if (ch == '.') { delaymax += (delaymax < 20 ? 1 : 0); } else if (ch== CTRL('l')) { - foreach(wclear, 4, stdscr, entry, stat_bar, board); + foreach(wclear, stdscr, entry, stat_bar, board); } for (int i = 0; i < height; ++i) { mvwaddnstr(board, i, 0, map + (i * width), width); } mvwchgat(board, y, x, 1, A_STANDOUT, COLOR_PAIR(0), NULL); stat_bar_print(stat_bar, "(%d, %d)\t\tGeneration: %d\t\tDelay Time: %d", x, y, generation, delaymax); - foreach(wnoutrefresh, 4, stdscr, board, entry, stat_bar); + foreach(wnoutrefresh, stdscr, board, entry, stat_bar); doupdate(); if (playing == TRUE) { delay++; @@ -254,8 +254,8 @@ int main (int argc, char * argv[]) { ch = wgetch(board); ch = tolower(ch); } - foreach(werase, 6, stdscr, board, stat_bar, entry, rule_entry, rule_entry_box); - foreach(delwin, 5, entry, stat_bar, board, rule_entry, rule_entry_box); + foreach(werase, stdscr, board, stat_bar, entry, rule_entry, rule_entry_box); + foreach(delwin, entry, stat_bar, board, rule_entry, rule_entry_box); board = stat_bar = entry = rule_entry = rule_entry_box = NULL; endwin(); delwin(stdscr); -- cgit