Skip to content

Instantly share code, notes, and snippets.

@ayourtch
Created February 26, 2020 11:08
Show Gist options
  • Save ayourtch/3b5521a509c8578c4c39fd8cc4b480df to your computer and use it in GitHub Desktop.
Save ayourtch/3b5521a509c8578c4c39fd8cc4b480df to your computer and use it in GitHub Desktop.
/* run this through CPP to see what this is all about */
/* this would be a separate include file but for simplicity we leave it here */
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define EXCEPTION_CHECK_N(x, n, ...) n
#define EXCEPTION_CHECK(...) EXCEPTION_CHECK_N(__VA_ARGS__, USUAL_HANDLING,)
#define DEFAULT_EXCEPTION SPECIAL_PROC(~)
#define SPECIAL_PROC(x) x, EXCEPTION_HANDLING,
#define SPECIAL_EXCEPTION(x) 1, x
#define HANDLING(x) EXCEPTION_CHECK(PRIMITIVE_CAT(EXCEPTION_, x))
/* include the above... then, define these: */
#define EXCEPTION_asd SPECIAL_EXCEPTION({ int baz; printf("Exceptional exception handling\n"); })
#define EXCEPTION_def DEFAULT_EXCEPTION
#define EXCEPTION_xxxx SPECIAL_EXCEPTION({ int baz2; printf("Exceptional exception handling #2\n"); })
#define USUAL_HANDLING { int usual; printf("This is business as usual\n"); }
#define EXCEPTION_HANDLING { int bar; printf("This is a default exception\n"); }
/* and then code-generate.... */
HANDLING(asd)
HANDLING(def)
HANDLING(xxxx)
HANDLING(whatever)
HANDLING(some_other_whatever)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment