Skip to content

Instantly share code, notes, and snippets.

@ksyx
Last active August 22, 2021 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksyx/6a7d40187ad9b62cf4e7bae2357d4928 to your computer and use it in GitHub Desktop.
Save ksyx/6a7d40187ad9b62cf4e7bae2357d4928 to your computer and use it in GitHub Desktop.
Argument passing utility with unified outside interface available
/*
A utility choosing version to call and carry extra information to the call
by argument with a unified outside interface. Call global functions foo, bar, foobar with
just one interface f.foo(int, int, void*, int, int), with arguments after void* directly
fed into what the function void* points to. Change bool types in whole code to add other information to pass.
Type conversions inside structure not so tidy yet.
Copyright (c) ksyx 2021. MIT Licensed.
*/
#include <cstdio>
// #define __PRETTY_FUNCTION__ __FUNCTION__ // nonstandard macro fallback
template<typename ...Args>
struct Foo {
void foo(void* func, Args... args) {
printf("%s\n",__PRETTY_FUNCTION__);
((void (*)(Args...))(func))(args...);
}
void foo(bool flag1, void* func, Args... args) {
printf("%s\n",__PRETTY_FUNCTION__);
if(!flag1) {
foo((void*)(func), args...);
} else ((void (*)(bool, Args...))(func))(1, args...);
}
void foo(bool flag1, bool flag2, void* func, Args... args) {
printf("%s\n",__PRETTY_FUNCTION__ );
if(!flag1) {
foo(flag2, (void*)(func), args...);
} else ((void (*)(bool, bool, Args...))(func))(1, 1, args...);
}
};
void foobar(bool f, bool p, int x, int y) {
printf("%s\n",__PRETTY_FUNCTION__);
printf("%d %d %d %d\n", f, p, x, y);
}
void foo(bool p, int x, int y) {
printf("%s\n",__PRETTY_FUNCTION__);
printf("%d %d %d\n", p, x, y);
}
void bar(int x, int y) {
printf("%s\n",__PRETTY_FUNCTION__);
printf("%d %d\n", x, y);
}
int main() {
Foo<int, int> f;
printf("=== DIRECT CALLS ===\n");
f.foo(1, 1, (void *)(foobar), 2, 3);
f.foo(1, (void *)(foo), 2, 3);
f.foo((void *)(bar), 2, 3);
printf("=== START FROM BBII ===\n");
f.foo(0, 1, (void *)(foo), 2, 3);
f.foo(0, 0, (void *)(bar), 2, 3);
printf("=== START FROM BII ===\n");
f.foo(0, (void *)(bar), 2, 3);
return 0;
}
=== DIRECT CALLS ===
void Foo<Args>::foo(bool, bool, void*, Args ...) [with Args = {int, int}]
void foobar(bool, bool, int, int)
1 1 2 3
void Foo<Args>::foo(bool, void*, Args ...) [with Args = {int, int}]
void foo(bool, int, int)
1 2 3
void Foo<Args>::foo(void*, Args ...) [with Args = {int, int}]
void bar(int, int)
2 3
=== START FROM BBII ===
void Foo<Args>::foo(bool, bool, void*, Args ...) [with Args = {int, int}]
void Foo<Args>::foo(bool, void*, Args ...) [with Args = {int, int}]
void foo(bool, int, int)
1 2 3
void Foo<Args>::foo(bool, bool, void*, Args ...) [with Args = {int, int}]
void Foo<Args>::foo(bool, void*, Args ...) [with Args = {int, int}]
void Foo<Args>::foo(void*, Args ...) [with Args = {int, int}]
void bar(int, int)
2 3
=== START FROM BII ===
void Foo<Args>::foo(bool, void*, Args ...) [with Args = {int, int}]
void Foo<Args>::foo(void*, Args ...) [with Args = {int, int}]
void bar(int, int)
2 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment