Callback 与 MakeCallback是成对出现的。
1. Callback
http://www.nsnam.org/doxygen/classns3_1_1_callback.html
个人理解: Callback其实类似于“指向函数的指针"
Callback模板类: Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 >
其中 R为Callback的返回类型,必选
T1...T9是Callback的实参,可选,默认值为empty
2. MakeCallback
http://www.nsnam.org/doxygen/group___make_callback.html
template<typename T , typename OBJ , typename R , typename T1 >
Callback< R, T1 > ns3::MakeCallback (R(T::*)(T1) mem_ptr, OBJ objPtr)
模板类MakeCallback的返回值为Callback类型
mem_ptr: class method member pointer
objPtr: class instance
返回值: a wrapper Callback Build Callbacks for class method members which takes one argument and potentially return a value.
3. 一个简单的例子:
typedef Callback<void, Ptr<Vehicle>, std::string, Ptr<const Packet> > DeviceTraceCallback; //在类外定义
private:
DeviceTraceCallback m_devTxTrace;
public:
DeviceTraceCallback GetDevTxTraceCallback() {return m_devTxTrace;}
void SetDevTxTraceCallback(DeviceTraceCallback devTxTrace) { m_devTxTrace = devTxTrace; }
protected:
void DevTxTrace (std::string context, Ptr<const Packet> p) { m_devTxTrace(Ptr<Vehicle>(this), context, p);}
实现:
std::ostringstream oss;
oss << "/NodeList/" << m_node->GetId()<< "/DeviceList/0/Mac/MacTx";
Config::Connect (oss.str(), MakeCallback (&Vehicle::DevTxTrace, this));
1. Callback
http://www.nsnam.org/doxygen/classns3_1_1_callback.html
个人理解: Callback其实类似于“指向函数的指针"
Callback模板类: Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 >
其中 R为Callback的返回类型,必选
T1...T9是Callback的实参,可选,默认值为empty
2. MakeCallback
http://www.nsnam.org/doxygen/group___make_callback.html
template<typename T , typename OBJ , typename R , typename T1 >
Callback< R, T1 > ns3::MakeCallback (R(T::*)(T1) mem_ptr, OBJ objPtr)
模板类MakeCallback的返回值为Callback类型
mem_ptr: class method member pointer
objPtr: class instance
返回值: a wrapper Callback Build Callbacks for class method members which takes one argument and potentially return a value.
3. 一个简单的例子:
typedef Callback<void, Ptr<Vehicle>, std::string, Ptr<const Packet> > DeviceTraceCallback; //在类外定义
private:
DeviceTraceCallback m_devTxTrace;
public:
DeviceTraceCallback GetDevTxTraceCallback() {return m_devTxTrace;}
void SetDevTxTraceCallback(DeviceTraceCallback devTxTrace) { m_devTxTrace = devTxTrace; }
protected:
void DevTxTrace (std::string context, Ptr<const Packet> p) { m_devTxTrace(Ptr<Vehicle>(this), context, p);}
实现:
std::ostringstream oss;
oss << "/NodeList/" << m_node->GetId()<< "/DeviceList/0/Mac/MacTx";
Config::Connect (oss.str(), MakeCallback (&Vehicle::DevTxTrace, this));