Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A nonexhaustive list of macro hackerys
- */
- /* -- default parameters from dev.to/rdentato (annotated) -- */
- /* vrg.h - reuseable for multiple programs */
- /* count how many args are passed to a variadic macro */
- #define vrg_cnt(vrg1, vrg2, vrg3, vrgN, ...) vrgN
- #define vrg_argn(...) vrg_cnt(__VA_ARGS__, 3, 2, 1, 0)
- /* concatenate name with number of args: "_MyFunc" + "1" -> "_MyFunc1" */
- #define vrg_cat0(x, y) x ## y
- #define vrg_cat(x, y) vrg_cat0(x, y)
- /* main macro used in definition */
- #define vrg(vrg_f, ...) vrg_cat(vrg_f, vrg_argn(__VA_ARGS__))(__VA_ARGS__)
- /* example.c */
- #include "vrg.h"
- #include <stdio.h>
- /* using the number of arguments to decide which function to call */
- #define MyFunc(...) vrg(_MyFunc, __VA_ARGS__)
- /* prepare a macro for every possible number of arguments */
- #define _MyFunc1(element) _MyFunc(element, -1, NULL)
- #define _MyFunc2(element, id) _MyFunc(element, id, NULL)
- #define _MyFunc3(element, id, pInput) _MyFunc(element, id, pInput)
- void *_MyFunc(void *element, int id, const void *pInput) {
- printf("Calling MyFunc() with args %p, %d, %p\n", element, id, pInput);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement