purpose of code: need able feed in function type parameter constructor may call inside class.
why have way? because i'm using unreal engine 4 frunnable threading done such way thread call specific interfaces (virtual member functions) run thread unless want make new class every thread task assign, have try , feed generic functions can have maximum functionality threading class.
what using threading class for? sqlite3 queries, each query requires data saving requires different parameters select queries.
this code below, first part works fine second constructor, not;
threading class constructor
fthreading::fthreading(std::function<void()> functask) { //check if thread has not been defined , system supports multi threading if (!thread && fplatformprocess::supportsmultithreading()) { //create our thread voidnoparafunc = functask; curflag = 1; thread = frunnablethread::create(this, text("fthreading"), 0, tpri_belownormal); } else { //can't multithread, call function straight functask(); } }
the call:
std::function<void()> passfunc = std::bind(&ucp_gameinstance::initdb, gameinst); fthreading* threadpointer = new fthreading(passfunc);
now code won't work need with:
fthreading::fthreading(std::function<bool(fstring, fstring)> functask) { //check if thread has not been defined , system supports multi threading if (!thread && fplatformprocess::supportsmultithreading()) { //create our thread booltwostring = functask(); curflag = 2; thread = frunnablethread::create(this, text("fthreading"), 0, tpri_belownormal); } else { //can't multithread, call function straight functask(); } }
the call:
void ucp_gameinstance::addnewsurvivor(fstring first, fstring second, fstring nation, fstring age, fstring blood, fstring level, fstring spec) { fstring query = "insert survivordata (first, second, nation, age, blood, level, spec) " + "values (" + first + ", " + second + ", " + nation + ", " + age + ", " + blood + ", " + level + ", " + spec + ");"; std::function<bool(fstring, fstring)> passfunc = std::bind(&usqlitedatabase::execsql, getdbname(), query); fthreading* threadpointer = new fthreading(passfunc); }
any appreciated i've been stuck on issue while , now, design suggestions/changes more invited.
thank time.
as figured out std::bind
, can't pass random function - how fthreading
know how call it? there actual call instruction functask();
tells functask
takes no arguments.
now, in second overload, functask
takes 2 fstring
arguments. obviously, means functask();
call there nonsense. it's missing both arguments. cannot invent arguments you.
this general problem idea in title, "take type of function parameter". fails, not because it's impossible pass function, because it's impossible call function.
ps. booltwostring = functask();
should booltwostring = functask
- it's after not direct call there.
Comments
Post a Comment