Search code examples
c++boosttimercompiler-errorsboost-bind

Usage of boost::bind ... what am I doing wrong


I have adapted the code from the following question's first answer for making a periodic timer: How do I make the boost/asio library repeat a timer?

I removed the "count" variable as the method I want to repeatedly execute doesn't take any parameter for computation.

Compiler fails with the following errors:

Error   C2825   'F': must be a class or namespace when followed by '::' C:\.conan\wkypad\1\include\boost\bind\bind.hpp  75  
Error   C2510   'F': left of '::' must be a class/struct/union  C:\.conan\wkypad\1\include\boost\bind\bind.hpp  75  
Error   C3646   'type': unknown override specifier  C:\.conan\wkypad\1\include\boost\bind\bind.hpp  75  
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int C:\.conan\wkypad\1\include\boost\bind\bind.hpp 75  
Error   C2039   'type': is not a member of 'boost::_bi::result_traits<R,F>' C:\.conan\wkypad\1\include\boost\bind\bind.hpp  1284    

What am I doing wrong? I guess my usage of boost::bind is wrong but I can't figure out the exact reason.

Here is the relevant part of my code

class ProjectBrowserComponent ...

private:
    void fetchData(const boost::system::error_code& /*e*/,
        boost::asio::deadline_timer* t);
    std::vector<Project> projects;
    ProjectsController pc;
    std::future<std::vector<Project>> projectsFuture;
    int interval_secs = 1;

...

template<typename R>
bool isReady(std::future<R> const& f)
{
    Logger::writeToLog("check future");
    return f.wait_for(std::chrono::seconds(-1)) == std::future_status::ready;
}

void ProjectBrowserComponent::initData() {
    Logger::writeToLog("requesting projects");
    projectsFuture = std::async(std::launch::async, &ProjectsController::getProjects, &pc);
    Logger::writeToLog("requested projects");
    boost::asio::io_service io_service;
    boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(interval_secs));
    timer.async_wait(boost::bind(&ProjectBrowserComponent::fetchData, boost::asio::placeholders::error, &timer));
    io_service.run();
}

void ProjectBrowserComponent::fetchData(const boost::system::error_code& /*e*/,
    boost::asio::deadline_timer* timer) {
    if (isReady(projectsFuture)) {
        projects = projectsFuture.get();
        std::cout << "got projs";
    }
    else {
        timer->expires_at(timer->expires_at() + boost::posix_time::seconds(interval_secs));
        // Posts the event
        timer->async_wait(boost::bind(&ProjectBrowserComponent::fetchData, boost::asio::placeholders::error, timer));
    }
}

Solution

  • How about this:

    timer.async_wait(boost::bind(&ProjectBrowserComponent::fetchData, this, boost::asio::placeholders::error, &timer));
    

    If you bind member functions, you must bind them also to the correct instance of the object of the class. Here I bound it to this, which I assume is what you're doing from the looks of your code.

    Note from sehe from the comments:

    It's important to note that this MUST stay valid for the entire duration of the async operation. A popular way to achieve this is is to make ProjectBrowserComponent inherit from enable_shared_from_this and bind to shared_from_this()