Actions
Bug #9506
openCannot move callbacks
Status:
New
Priority:
Normal
Assignee:
-
Target version:
-
Start date:
12/15/2021
Due date:
% Done:
0%
Estimated time:
Description
Hi,
I'm posting some events to server's io loop (io_context).
But I can't move non-copyable data inside those arguments (lambdas with unique ptrs) because they are all
'const Function& ' (ApplicationEvent) or
'const std::function &' (WServer::post/schedule).
So I do ugly workarounds like this
void process_response(auto response) {
struct handler {
decltype(response) o;
};
auto h = std::make_shared<handler>(); // here make some copyable ptr
h->o = std::move(response);
vvv not it can be copied ok
environment().server()->post(session()->sessionId(), [this, h]() {
process_td_response2(std::move(h->o));
session()->setTriggerUpdate(true); // not always?
});
}
Wt is written in some old days conservative C and classes style.
But C++20 is out and C++23 is near, they bring great benefits to code effectiveness and code culture.
Template metaprogramming is very improved since C++98/11/14.
Updated by Egor Pugin almost 3 years ago
Possible solution is to use arguments like
std::function<void(void)> f // user will copy or std::move() his callback
or
std::function<void(void)> &&f // user will std::move() his callback
Updated by Egor Pugin almost 3 years ago
Also don't be scared of templates and pass everything directly to boost::asio::post() in case if you do not store these callbacks.
void post(auto &&f) {
schedule(std::forward<decltype(f)>(f));
...
io_context.post(std::forward<decltype(f)>(f));
}
Actions