2015-03-31 142 views
0

我必須實現A valve Open function (for specified duration)。 我使用boost::asio::deadline_timerstd :: bind無法調用類成員函數:C++

我的類成員函數打開閥門:

bool Valves::valveOpen(ValveType type) 
    { 
     switch (type) 
     { 
     case eVentValve: 
      tblMap_.digitalInput[eVentValveK1].setBit(); 
      if (tblMap_.digitalOutput[eOutK1VentValve].getBit()) 
      { 
       isVentOpen_ = true; 
      } 
      return isVentOpen_; 

     case eVacuumPumpValve: 

.... 
.... 
} 

類的成員函數關閉閥門是:

bool Valves::valveClose(ValveType type) 
{ 
    switch (type) 
    { 
    case eVentValve: 
     tblMap_.digitalInput[eVentValveK1].clearBit(); 
     if (!tblMap_.digitalOutput[eOutK1VentValve].getBit()) 
     { 
      isVentOpen_ = false; 
     } 
     return !isVentOpen_; 

    case eVacuumPumpValve: 
.... 
.... 
} 

我想實現定時器行動低於

bool Valves::valveTimedOpen(ValveType type, int sec) 
{ 
    boost::asio::io_service io; 
    switch (type) 
    { 
    case eVentValve: 
    { 
        std::bind(&Valves::valveOpen, this, type); //Here 
        boost::asio::deadline_timer t(io, boost::posix_time::seconds(sec)); 
        t.async_wait(std::bind(&Valves::valveClose, this, type)); 
        boost::thread th(boost::bind(&boost::asio::io_service::run, &io)); 
        return true; 
    } 

    case eVacuumPumpValve: 

..... 
..... 
} 

代碼命中行Here

std::bind(&Valves::valveOpen, this, type);但它不去bool Valves::valveOpen(ValveType type)功能。

有人可以讓我知道這個代碼的問題?

回答

3

變量iot只要valveTimedOpen退出就退出範圍。您需要重新思考與boost asio組件交互的方式,例如io_service可能是您的類的成員,並且計時器可以動態分配並需要在完成處理程序中刪除。

此外,請記住,如果您計劃重新使用io_service對象,則還需要在調用run之前再調用reset

0
 auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1); 
     fn(type); 

正確調用Open()

io_serviceboost::deadline_timer我必須讓類成員的建議通過@Ralf

工作代碼:

#include <boost/date_time/posix_time/posix_time.hpp> 
#include <boost/thread.hpp> 
#include <boost/asio.hpp> 


class Test : public std::enable_shared_from_this <Test> 
{ 
public: 
    Test() :io(), timer(io){} 
    void Open(int num); 
    void Close(int num); 
    void TimedOpen(int num, int dur); 
    void Run(); 
private: 
    boost::asio::io_service io; 
    boost::asio::deadline_timer timer; 
}; 

void Test::Open(int num) 
{ 
    std::cout << "Open for Number : " << num << std::endl; 
} 

void Test::Close(int num) 
{ 
    std::cout << "Close for Number : " << num << std::endl; 
} 

void Test::TimedOpen(int num, int dur) 
{ 
    io.reset(); 
    auto fn = std::bind(&Test::Open, shared_from_this(), std::placeholders::_1); 
    fn(num); 
    timer.expires_from_now(boost::posix_time::seconds(dur)); 
    timer.async_wait(std::bind(&Test::Close, shared_from_this(), num)); 
    Run(); 
    std::cout << "Function Exiting" << std::endl; 
} 

void Test::Run() 
{ 
    boost::thread th(boost::bind(&boost::asio::io_service::run, &io)); 
} 

int main() 
{ 
    auto t = std::make_shared<Test>(); 
    t->TimedOpen(5, 5); 
    char line[128]; 
    while (std::cin.getline(line, 128)) 
    { 
     if (strcmp(line, "\n")) break; 
    } 
    return 0; 
} 
相關問題