So, I'm trying to learn template metaprogramming better and I figure this is a good exercise for it.
I'm trying to write code that can callback a function with any number of arguments I like passed to it.
// First function to call int add( int x, int y ) ; // Second function to call double square( double x ) ; // Third func to call void go() ;
The callback creation code should look like:
// Write a callback object that // will be executed after 42ms for "add" Callback<int, int, int> c1 ; c1.func = add ; c1.args.push_back( 2 ); // these are the 2 args c1.args.push_back( 5 ); // to pass to the "add" function // when it is called Callback<double, double> c2 ; c2.func = square ; c2.args.push_back( 52.2 ) ;
What I'm thinking is, using template metaprogramming I want to be able to declare callbacks like, write a struct like this (please keep in mind this is VERY PSEUDOcode)
<TEMPLATING ACTION <<ANY NUMBER OF TYPES GO HERE>> > struct Callback { double execTime ; // when to execute TYPE1 (*func)( TYPE2 a, TYPE3 b ) ; void* argList ; // a stored list of arguments // to plug in when it is time to call __func__ } ;
So for when called with
Callback<int, int, int> c1 ;
You would automatically get constructed for you by < HARDCORE TEMPLATING ACTION > a struct like
struct Callback { double execTime ; // when to execute int (*func)( int a, int b ) ; void* argList ; // this would still be void*, // but I somehow need to remember // the types of the args.. } ;
Any pointers in the right direction to get started on writing this?