我想寫一個調用boost :: asio操作的庫模塊。根據結果,該模塊將被應用程序邏輯層用於進一步處理。boost :: bind如何定義一個函數,將回調作爲參數
我的要求很簡單明瞭。我需要的是能夠在下面的Util類中指定一個回調函數fn,它可以使用任何函數ptr或boost :: bind(member fn)或其他函數,並在異步代碼完成後調用該回調函數。
class Util {
public:
void sendMsg(const Msg& m, <some callback fn here...>) {
// Make a call to boost::asio approximately as below..
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
// Here, I want to pass the "<some callback fn here>" to async_write,
// instead of calling boost::bind here. How to do that?
}
};
所以,我的具體問題是.. 應該我SENDMSG簽名是什麼樣子?我無法到達fn簽名,特別是回調部分。
感謝任何讓我知道如何做到這一點。
'的std :: function'?或模板類型? – Arcinde
好的。這些天我已經失去了很多C++的聯繫。你可以提供一個簡單的例子..請 –
好吧,thx的提示。我似乎已經明白了。我有點老派的C++。熟悉提升,異步的東西。 –