无法理解 ns-3 源代码中 getter 方法的 Constructor() 语法
Unable to understand Constructor() syntax of a getter method in ns-3 source code
下面是代码片段
TypeId
UdpEchoClient::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::UdpEchoClient")
.SetParent<Application> ()
.SetGroupName("Applications")
.AddConstructor<UdpEchoClient> ()
.AddAttribute ("MaxPackets",
"The maximum number of packets the application will send",
UintegerValue (100),
MakeUintegerAccessor (&UdpEchoClient::m_count),
MakeUintegerChecker<uint32_t> ())
.AddAttribute ("Interval",
"The time to wait between packets",
TimeValue (Seconds (1.0)),
MakeTimeAccessor (&UdpEchoClient::m_interval),
MakeTimeChecker ())
.AddAttribute ("RemoteAddress",
"The destination Address of the outbound packets",
AddressValue (),
MakeAddressAccessor (&UdpEchoClient::m_peerAddress),
MakeAddressChecker ())
.AddAttribute ("RemotePort",
"The destination port of the outbound packets",
UintegerValue (0),
MakeUintegerAccessor (&UdpEchoClient::m_peerPort),
MakeUintegerChecker<uint16_t> ())
.AddAttribute ("PacketSize", "Size of echo data in outbound packets",
UintegerValue (100),
MakeUintegerAccessor (&UdpEchoClient::SetDataSize,
&UdpEchoClient::GetDataSize),
MakeUintegerChecker<uint32_t> ())
.AddTraceSource ("Tx", "A new packet is created and is sent",
MakeTraceSourceAccessor (&UdpEchoClient::m_txTrace),
"ns3::Packet::TracedCallback")
.AddTraceSource ("Rx", "A packet has been received",
MakeTraceSourceAccessor (&UdpEchoClient::m_rxTrace),
"ns3::Packet::TracedCallback")
.AddTraceSource ("TxWithAddresses", "A new packet is created and is sent",
MakeTraceSourceAccessor (&UdpEchoClient::m_txTraceWithAddresses),
"ns3::Packet::TwoAddressTracedCallback")
.AddTraceSource ("RxWithAddresses", "A packet has been received",
MakeTraceSourceAccessor (&UdpEchoClient::m_rxTraceWithAddresses),
"ns3::Packet::TwoAddressTracedCallback")
;
return tid;
}
尤其是下面这个片段
static TypeId tid = TypeId ("ns3::UdpEchoClient")
.SetParent<Application> ()
.SetGroupName("Applications")
在此 SetParent() 方法中 "Application" 是什么? SetParent() 的声明是:
TypeId SetParent (TypeId tid);
and
template <typename T>
TypeId SetParent (void);
有人可以解释一下这个 GetTypeId() 吗,在 TypeId 构造函数上使用嵌套点 (.) 运算符是怎么回事?
这里是 link 文件 :
[1] https://github.com/signetlabdei/quic-ns-3/blob/master/src/applications/model/udp-echo-client.cc
[2] https://github.com/signetlabdei/quic-ns-3/blob/master/src/core/model/type-id.h
[3] https://github.com/signetlabdei/quic-ns-3/blob/master/src/core/model/type-id.cc
[4] https://github.com/signetlabdei/quic-ns-3/blob/master/src/applications/model/udp-echo-client.h
Can somebody explain this GetTypeId(), what is going on here with nested dot(.) operators over a TypeId constructor?
如果你查看头文件中符号的声明,你会注意到所有这些 "nested dot operators" 只是 class TypeId
return 的方法正在 TypeId
:
template <typename T> TypeId SetParent (void);
如果您查看方法的源代码:
TypeId
TypeId::SetParent (TypeId tid)
{
NS_LOG_FUNCTION (this << tid.GetUid ());
IidManager::Get ()->SetParent (m_tid, tid.m_tid);
return *this; //here the copy takes place
}
很明显它 return 是它自己的副本。
但为什么它 return 是它自己的副本?当再次查看头文件时,很明显 struct TypeId 只有一个值:
class TypeId
{
// lots and lots of method declarations
// [...]
/** The TypeId value. */
uint16_t m_tid;
};
因此文案只是实现算子嵌套的一个工具
那么它有什么作用呢?因为我没有写过这段代码,所以我不能 100% 确定,但对我来说,它似乎包含一些关于传递类型的元信息,您可以在运行时更改这些信息(例如更改构造函数或超级 class)
下面是代码片段
TypeId
UdpEchoClient::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::UdpEchoClient")
.SetParent<Application> ()
.SetGroupName("Applications")
.AddConstructor<UdpEchoClient> ()
.AddAttribute ("MaxPackets",
"The maximum number of packets the application will send",
UintegerValue (100),
MakeUintegerAccessor (&UdpEchoClient::m_count),
MakeUintegerChecker<uint32_t> ())
.AddAttribute ("Interval",
"The time to wait between packets",
TimeValue (Seconds (1.0)),
MakeTimeAccessor (&UdpEchoClient::m_interval),
MakeTimeChecker ())
.AddAttribute ("RemoteAddress",
"The destination Address of the outbound packets",
AddressValue (),
MakeAddressAccessor (&UdpEchoClient::m_peerAddress),
MakeAddressChecker ())
.AddAttribute ("RemotePort",
"The destination port of the outbound packets",
UintegerValue (0),
MakeUintegerAccessor (&UdpEchoClient::m_peerPort),
MakeUintegerChecker<uint16_t> ())
.AddAttribute ("PacketSize", "Size of echo data in outbound packets",
UintegerValue (100),
MakeUintegerAccessor (&UdpEchoClient::SetDataSize,
&UdpEchoClient::GetDataSize),
MakeUintegerChecker<uint32_t> ())
.AddTraceSource ("Tx", "A new packet is created and is sent",
MakeTraceSourceAccessor (&UdpEchoClient::m_txTrace),
"ns3::Packet::TracedCallback")
.AddTraceSource ("Rx", "A packet has been received",
MakeTraceSourceAccessor (&UdpEchoClient::m_rxTrace),
"ns3::Packet::TracedCallback")
.AddTraceSource ("TxWithAddresses", "A new packet is created and is sent",
MakeTraceSourceAccessor (&UdpEchoClient::m_txTraceWithAddresses),
"ns3::Packet::TwoAddressTracedCallback")
.AddTraceSource ("RxWithAddresses", "A packet has been received",
MakeTraceSourceAccessor (&UdpEchoClient::m_rxTraceWithAddresses),
"ns3::Packet::TwoAddressTracedCallback")
;
return tid;
}
尤其是下面这个片段
static TypeId tid = TypeId ("ns3::UdpEchoClient")
.SetParent<Application> ()
.SetGroupName("Applications")
在此 SetParent() 方法中 "Application" 是什么? SetParent() 的声明是:
TypeId SetParent (TypeId tid);
and
template <typename T> TypeId SetParent (void);
有人可以解释一下这个 GetTypeId() 吗,在 TypeId 构造函数上使用嵌套点 (.) 运算符是怎么回事?
这里是 link 文件 :
[1] https://github.com/signetlabdei/quic-ns-3/blob/master/src/applications/model/udp-echo-client.cc
[2] https://github.com/signetlabdei/quic-ns-3/blob/master/src/core/model/type-id.h
[3] https://github.com/signetlabdei/quic-ns-3/blob/master/src/core/model/type-id.cc
[4] https://github.com/signetlabdei/quic-ns-3/blob/master/src/applications/model/udp-echo-client.h
Can somebody explain this GetTypeId(), what is going on here with nested dot(.) operators over a TypeId constructor?
如果你查看头文件中符号的声明,你会注意到所有这些 "nested dot operators" 只是 class TypeId
return 的方法正在 TypeId
:
template <typename T> TypeId SetParent (void);
如果您查看方法的源代码:
TypeId
TypeId::SetParent (TypeId tid)
{
NS_LOG_FUNCTION (this << tid.GetUid ());
IidManager::Get ()->SetParent (m_tid, tid.m_tid);
return *this; //here the copy takes place
}
很明显它 return 是它自己的副本。
但为什么它 return 是它自己的副本?当再次查看头文件时,很明显 struct TypeId 只有一个值:
class TypeId
{
// lots and lots of method declarations
// [...]
/** The TypeId value. */
uint16_t m_tid;
};
因此文案只是实现算子嵌套的一个工具
那么它有什么作用呢?因为我没有写过这段代码,所以我不能 100% 确定,但对我来说,它似乎包含一些关于传递类型的元信息,您可以在运行时更改这些信息(例如更改构造函数或超级 class)