class - Timer Thread with passed Function* and Param -
i'm working on finishing server first iphone application, , want implement simple little feature.
i run function (perhaps method well), if function returns value after waiting period. simple concept.... right?
here's basic foundation.
template <typename t,class typ> struct funcpar{ t (*function)(typ); typ parameter; funcpar(t (*func)(typ),typ param); funcpar& operator=(const funcpar& fp); };
the goal here able call funcpar::function(funcpar::parameter)
run stored function , parameter, , not have worry else...
when attempted use void* parameter instead of template, couldn't copy memory object (because didn't know end object going be, or beginning matter) , when tried multiple timers, every single object's parameter change new parameter passed new timer... previous struct have
question:
is possible make all-inclusive pointer type of object inside method of class? can templatize method, , not whole class? work function template?
i have managing class holds vector of these "jobs" , takes care of well. don't know how use templatized function struct, or how utilize templates on single method in class..
i'm utilizing in custom simple threadpool, , that's working well, , has same problems...
i have question:
can possibly store function parameter before it's run? torun = dontrunmeyet(withthisparameter);
? struct necessary?
am going whole thing incorrectly?
if overly ambiguous, can set whole code context
in order create class method takes template parameter, yes, work function template. example:
class { public: template<typename t> void my_function(const t& value) { } }; int main() { test; test.my_function(5); return 0; }
secondly, structure, can turn functor-object overloading operator()
, lets call structure as-if function rather having call specific function pointer members inside structure. instance, structure re-written this:
#include <iostream> template <class returntype, class parametertype> class funcpar { private: returntype (*function)(parametertype); parametertype parameter; public: funcpar(returntype (*func)(parametertype),parametertype param): function(func), parameter(param) {} funcpar& operator=(const funcpar& fp); //operator() overloaded function takes no arguments //and returns type returntype returntype operator() () { return function(parameter); } }; int sample_func(int value) { return value + 1; } int main() { funcpar<int, int> test_functor(sample_func, 5); //you can call instance of funcpar normal function std::cout << test_functor() << std::endl; return 0; }
btw, need functor object (or structure, etc.) in order bind dynamic parameter function before function called in c/c++ ... can't "store" parameter actual function. binding parameter function called closure, , in c/c++, creating closure requires structure/class or type of associated data-structure can use bind function specific parameter stored in memory used specific instance of function call.
Comments
Post a Comment