Pass param packed args into a std::queue to call with a different function later

243 views Asked by At

I asked a similar question earlier without realizing that that wasn't quite specific enough.

So I have this function that has to take in all the arguments of a print function, with the ... and all, and then put it into a queue that will call the actual print function later.

Something like:

std::queue<SOMETHING> queue;
template <typename... Params>
void printLater(int a, int b, char* fmt, Params ...args) {
    queue.push(args);
}

template <typename... Params>
void print(int a, int b, char* fmt, Param ...args) {
    //whatever
}

void actuallyPrint() {
    //whatever
    print(queue.pop());
}

Context: I'm working with a piece of hardware that can only handle requests every 50ms or else they're ignored. My goal is to create a wrapper that will automatically add the delays if I send it a bunch at once.

My fallback if I cant do this, although I'd rather do this is just sprintf (or C++ equivalent) into a string only store the string in the queue and call print() without all the args.

1

There are 1 answers

0
Igor Tandetnik On

Something like this perhaps:

std::queue<std::function<void()>> queue;

template <typename... Params>
void printLater(int a, int b, char* fmt, Params ...args) {
    queue.push([=](){ print(a, b, fmt, args...); } );
}

void actuallyPrint() {
    queue.front()();
    queue.pop();
}