jack7
jack7

Reputation: 127

Unable to understand Constructor() syntax of a getter method in ns-3 source code

Below is the code snippet


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;
}

particularly this below segment

static TypeId tid = TypeId ("ns3::UdpEchoClient")
    .SetParent<Application> ()
    .SetGroupName("Applications")

What is "Application" here in this SetParent() method? The declaration of SetParent() is :

TypeId SetParent (TypeId tid);
and
template <typename T> TypeId SetParent (void);

Can somebody explain this GetTypeId(), what is going on here with nested dot(.) operators over a TypeId constructor?

Here is the link to files :
[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

Upvotes: 0

Views: 88

Answers (1)

user3520616
user3520616

Reputation: 101

Can somebody explain this GetTypeId(), what is going on here with nested dot(.) operators over a TypeId constructor?

If you look into the declaration of the symbols in the header file you'll notice that all those "nested dot operators" are just methods of the class TypeId returning a TypeId:

template <typename T> TypeId SetParent (void);

If you look into the source code of the method:

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
}

it comes clear that it returns a copy of itself.

But why does it return a copy of itself? When looking into the header file again it comes clear that the struct TypeId just has one value it holds:

class TypeId
{
// lots and lots of method declarations
// [...]
/** The TypeId value. */ 
  uint16_t m_tid;
};

The copy is therefore just a tool to achieve the nesting of the operators.

So what does it do? As I haven't written this code I can't be 100% sure but for me it seems that it holds some meta information about passed types that you can change during runtime (e.g. change the constructor or the super class)

Upvotes: 2

Related Questions