我正在尝试创建一个简单的vector::size
类,它包含void get_student_data(vector<student>& v)
{
for (size_t i = 0; i < v.size(); i++)
{
cout << "Enter Name of " << (i+1) <<"th student\n";
cin >> v[i].name;
}
}
的基本功能,例如BoostTimer
和deadline_timer
,以便我的程序只调用async_wait
和cancel
,我写了以下代码
startTimer
killTimer
#ifndef __BOOST_TIMER__
#define __BOOST_TIMER__
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/chrono.hpp>
#include <boost/chrono/duration.hpp>
#include <boost/function.hpp>
class BoostTimer{
public:
typedef boost::asio::deadline_timer deadline_timer;
typedef deadline_timer::duration_type duration;
typedef boost::function<void (boost::system::error_code, BoostTimer&)> handler_function;
BoostTimer(boost::asio::io_service& io_service, duration interval, handler_function handler);
~BoostTimer();
void startTimer();
void killTimer();
private:
deadline_timer _timer;
boost::asio::io_service& _ioService;
duration _interval;
handler_function _handler;
};
#endif
我的问题是如何从我的#include "boosttimer.h"
BoostTimer::BoostTimer(boost::asio::io_service& io_service, duration interval, handler_function handler) :
_timer(io_service),
_ioService(io_service),
_interval(interval),
_handler(handler)
{
}
BoostTimer::~BoostTimer()
{
}
void BoostTimer::startTimer()
{
_timer.expires_from_now(_interval);
_timer.async_wait(boost::bind(_handler, boost::asio::placeholders::error, boost::ref(*this))); //trying to pass placeholder argument but somehow it doesn't work
}
void BoostTimer::killTimer()
{
_timer.cancel();
}
班级#include <iostream>
#include <boost/asio.hpp>
#include "boosttimer.h"
//void timer_handler(const boost::system::error_code& /*e*/) // not able to take parameters
void timer_handler() //it runs fine without parameters
{
std::cout<<"timer function has been called" << std::endl;
}
int main(int argc, char* argv[])
{
boost::asio::io_service io_service;
BoostTimer timer(io_service,boost::posix_time::seconds(5), boost::bind(&timer_handler));
timer.startTimer();
io_service.run();
return 0;
}
函数中将参数传递给我的处理函数?我尝试了但是我错过了一些东西。
答案 0 :(得分:1)
如果您只想传递来自调用者的参数:
void timer_handler(std::string const& arg1, int arg2)
{
std::cout<<"timer function has been called with arg1='" << arg1 <<"', arg2=" << arg2 << std::endl;
}
int main()
{
boost::asio::io_service io_service;
BoostTimer timer(io_service,boost::posix_time::seconds(5), boost::bind(&timer_handler, "This is arg1", 42));
timer.startTimer();
io_service.run();
return 0;
}
看到 Live On Coliru ,输出:
timer function has been called with arg1='This is arg1', arg2=42
还要传递ec
和*this
:
CAVEAT 我认为这严重打破了任何封装,并使整个班级基本上多余。考虑在取消时不要调用完成处理程序,例如此外,让调用者绑定它需要的对象实例(对于用户定义的处理程序而言,它需要引用
BoostTimer
- 这是错误方向的紧密耦合)
void timer_handler(boost::system::error_code ec, BoostTimer& instance, std::string const& arg1, int arg2) //it runs fine without parameters
{
std::cout<<"timer function has been called with arg1='" << arg1 <<"', arg2=" << arg2 << " (" << ec.message() << ")\n";
}
int main()
{
boost::asio::io_service io_service;
BoostTimer timer(io_service,boost::posix_time::seconds(1), boost::bind(&timer_handler, _1, _2, "This is arg1", 42));
timer.startTimer();
io_service.run();
return 0;
}
同时查看 Live On Coliru ,输出:
timer function has been called with arg1='This is arg1', arg2=42 (Success)