Search code examples
wt

WApplication::bind() usage in combination with Wt::WServer::post


I want to bind a callback, which will be called by a thread outside the Wt event loop. So obviously I want to use Wt::WServer::post, but I don't get how WApplication::bind should be used, since it's a nonstatic function. First attempt was this:

auto loaded_callback = [](const decltype(Wt::WApplication::sessionId) &session){
    Wt::WServer::post(session, 
Wt::WApplication::bind(&table_model::member_func),)
};

Which of course didn't work because bind is nonstatic. However my next attempt

auto object_protect_bind = 
Wt::WApplication::instance()->bind(&order_aggregate_table_model::load_future_in_map);

failed with a shitload of compiler errors

Error 153 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \boost\function\function_template.hpp 922 1 MDDB_Web Error 156 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \boost\function\function_template.hpp 926 1 MDDB_Web Error 160 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int \boost\function\function_template.hpp 927 1 MDDB_Web Error 150 error C2903: 'apply' : symbol is neither a class template nor a function template \boost\function\function_template.hpp 922 1 MDDB_Web Error 162 error C2825: 'manager_type': must be a class or namespace when followed by '::' \boost\function\function_template.hpp 934 1 MDDB_Web Error 154 error C2653: 'handler_type' : is not a class or namespace name \boost\function\function_template.hpp 926 1 MDDB_Web Error 158 error C2653: 'handler_type' : is not a class or namespace name \boost\function\function_template.hpp 927 1 MDDB_Web Error 164 error C2275: 'manager_type' : illegal use of this type as an expression \boost\function\function_template.hpp 934 1 MDDB_Web Error 165 error C2146: syntax error : missing '}' before identifier 'manage' \boost\function\function_template.hpp 934 1 MDDB_Web Error 159 error C2146: syntax error : missing ';' before identifier 'manager_type' \boost\function\function_template.hpp 927 1 MDDB_Web Error 155 error C2146: syntax error : missing ';' before identifier 'invoker_type' \boost\function\function_template.hpp 926 1 MDDB_Web Error 152 error C2143: syntax error : missing ';' before '<' \boost\function\function_template.hpp 922 1 MDDB_Web Error 163 error C2039: 'manage' : is not a member of '`global namespace'' \boost\function\function_template.hpp 934 1 MDDB_Web Error 151 error C2039: 'apply' : is not a member of 'boost::detail::function::get_invoker0' \boost\function\function_template.hpp 922 1 MDDB_Web Error 166 error C1903: unable to recover from previous error(s); stopping compilation \boost\function\function_template.hpp 934 1 MDDB_Web

while the overall solution I had in mind was:

auto sessionId = Wt::WApplication::instance()->sessionId();
auto server_ptr = Wt::WServer::instance();

auto object_protect_bind = Wt::WApplication::instance()->bind(&order_aggregate_table_model::load_future_in_map);
auto inner_bind = std::bind(object_protect_bind, this);
auto loaded_callback = [] 
    (Wt::WServer* server,
    const std::string &session,
    boost::function<void()> widget_bind)
-> void {
    server->post(session, widget_bind, boost::function<void()>());
};

this->data_future =
    std::async(std::launch::async,
    table_model::load_quiet_a_bunch_of_data,
    query, database, std::bind(loaded_callback, server_ptr, sessionId, inner_bind));
Wt::WTimer::singleShot(20 * 1000, this, &table_model::load_future_in_map);

Any suggestions?


Solution

  • Thanks to Koen Deforche in the official forum, the problem was: Wt::WApplication::bind is supposed to take the already binded method, not the method itself.

    Also there is a surprising (for me at least) template detail when using lambdas, so for the sake of an example, my solution for a callback, used by a data loading thread, is:

    static std::map<decltype(views::measurements_grouped_by_orders::order_number),
        order_value> 
        order_aggregate_table_model::async_load_order_values(
        const odb::query<views::measurements_grouped_by_orders> &query,
        std::shared_ptr<odb::database> mddb,
        std::function<void(void)> callback) {...
        if (callback){ callback(); }
        return map;
        }
    
    void order_aggregate_table_model::get_data(const odb::query<views::measurements_grouped_by_orders> &query){
    
    auto sessionId = Wt::WApplication::instance()->sessionId();
    auto server_ptr = Wt::WServer::instance();
    
    auto object_protect_bind = 
        Wt::WApplication::instance()->bind(/*Wt::WApplication::bind
                                           handles the case that 
                                           the widget might already been destroyed*/
            std::bind(&order_aggregate_table_model::load_future_in_map,this));
    auto loaded_callback = [] 
        (Wt::WServer* server,
        const std::string &session,
        std::function<void()> widget_bind)
    -> void {
        server->post(session, widget_bind, boost::function<void()>()); 
        //Wt::Server::post handles the case when the session is already been destroyed
    };
    std::function<void()> final_callback = //Because of template quirks had to stick the type
        std::bind(loaded_callback, server_ptr, sessionId, object_protect_bind);
    this->order_aggregate_map_future =
        std::async(std::launch::async,
        order_aggregate_table_model::async_load_order_values,
        query, this->mddb, final_callback);
    
    Wt::WTimer::singleShot(30 * 1000, this, 
        &order_aggregate_table_model::load_future_in_map); //For the case that the async loader crashed
        }