Skip to content

Instantly share code, notes, and snippets.

@dvyukov
Created May 19, 2023 06:41
Show Gist options
  • Save dvyukov/8ac602782d1b0b7a8950aa848961b8dd to your computer and use it in GitHub Desktop.
Save dvyukov/8ac602782d1b0b7a8950aa848961b8dd to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define NARG(...) NARG_(__VA_ARGS__, RSEQ_N())
#define NARG_(...) ARG_N(__VA_ARGS__)
#define RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
#define ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
#define CONCAT_(x, y) x##y
#define CONCAT(x, y) CONCAT_( x, y )
#define OVERLOAD(X, ...) CONCAT(X, NARG(__VA_ARGS__))
#define INTERCEPT(ret, func, ...) OVERLOAD(INTER, __VA_ARGS__)(ret, func, __VA_ARGS__)
#define INTER1(ret, func, unused) ret func() { return _##func(); }
#define INTER2(ret, func, t0, a0) ret func(t0 a0) { return _##func(a0); }
#define INTER4(ret, func, t0, a0, t1, a1) ret func(t0 a0, t1 a1) { return _##func(a0, a1); }
void _bar() {
printf("_bar()\n");
}
int _foo(int x) {
printf("_foo(%d)\n", x);
return 0;
}
int _baz(int x, const char* s) {
printf("_baz(%d, %s)\n", x, s);
return 0;
}
INTERCEPT(void, bar);
INTERCEPT(int, foo, int, x);
INTERCEPT(int, baz, int, x, const char*, s);
int main() {
bar();
foo(1);
baz(2, "hello");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment