network: Remove begin() and end() from Queue class

begin() and end() are not necessarily meaningful for all the containers.
A GetContainer() method returning a const reference to the container is
added, so that begin() and end() can be called through it.
This commit is contained in:
Stefano Avallone
2021-10-18 20:02:45 +02:00
parent e5805a53fb
commit 7c73228e78
6 changed files with 47 additions and 123 deletions

View File

@@ -52,8 +52,7 @@ public:
virtual Ptr<const Item> Peek (void) const;
private:
using Queue<Item>::begin;
using Queue<Item>::end;
using Queue<Item>::GetContainer;
using Queue<Item>::DoEnqueue;
using Queue<Item>::DoDequeue;
using Queue<Item>::DoRemove;
@@ -105,7 +104,7 @@ DropTailQueue<Item>::Enqueue (Ptr<Item> item)
{
NS_LOG_FUNCTION (this << item);
return DoEnqueue (end (), item);
return DoEnqueue (GetContainer ().end (), item);
}
template <typename Item>
@@ -114,7 +113,7 @@ DropTailQueue<Item>::Dequeue (void)
{
NS_LOG_FUNCTION (this);
Ptr<Item> item = DoDequeue (begin ());
Ptr<Item> item = DoDequeue (GetContainer ().begin ());
NS_LOG_LOGIC ("Popped " << item);
@@ -127,7 +126,7 @@ DropTailQueue<Item>::Remove (void)
{
NS_LOG_FUNCTION (this);
Ptr<Item> item = DoRemove (begin ());
Ptr<Item> item = DoRemove (GetContainer ().begin ());
NS_LOG_LOGIC ("Removed " << item);
@@ -140,7 +139,7 @@ DropTailQueue<Item>::Peek (void) const
{
NS_LOG_FUNCTION (this);
return DoPeek (begin ());
return DoPeek (GetContainer ().begin ());
}
// The following explicit template instantiation declarations prevent all the

View File

@@ -313,68 +313,11 @@ protected:
typedef typename Container::iterator Iterator;
/**
* \brief Get a const iterator which refers to the first item in the queue.
* Get a const reference to the container of queue items.
*
* Subclasses can browse the items in the queue by using a const iterator
*
* \code
* for (auto i = begin (); i != end (); ++i)
* {
* (*i)->method (); // some const method of the Item class
* }
* \endcode
*
* \returns a const iterator which refers to the first item in the queue.
* \return a const reference to the container of queue items
*/
ConstIterator begin (void) const;
/**
* \brief Get an iterator which refers to the first item in the queue.
*
* Subclasses can browse the items in the queue by using an iterator
*
* \code
* for (auto i = begin (); i != end (); ++i)
* {
* (*i)->method (); // some method of the Item class
* }
* \endcode
*
* \returns an iterator which refers to the first item in the queue.
*/
Iterator begin (void);
/**
* \brief Get a const iterator which indicates past-the-last item in the queue.
*
* Subclasses can browse the items in the queue by using a const iterator
*
* \code
* for (auto i = begin (); i != end (); ++i)
* {
* (*i)->method (); // some const method of the Item class
* }
* \endcode
*
* \returns a const iterator which indicates past-the-last item in the queue.
*/
ConstIterator end (void) const;
/**
* \brief Get an iterator which indicates past-the-last item in the queue.
*
* Subclasses can browse the items in the queue by using an iterator
*
* \code
* for (auto i = begin (); i != end (); ++i)
* {
* (*i)->method (); // some method of the Item class
* }
* \endcode
*
* \returns an iterator which indicates past-the-last item in the queue.
*/
Iterator end (void);
const Container& GetContainer (void) const;
/**
* Push an item in the queue
@@ -500,6 +443,13 @@ Queue<Item, Container>::~Queue ()
{
}
template <typename Item, typename Container>
const Container&
Queue<Item, Container>::GetContainer (void) const
{
return m_packets;
}
template <typename Item, typename Container>
bool
Queue<Item, Container>::DoEnqueue (ConstIterator pos, Ptr<Item> item)
@@ -632,30 +582,6 @@ Queue<Item, Container>::DoPeek (ConstIterator pos) const
return *pos;
}
template <typename Item, typename Container>
typename Queue<Item, Container>::ConstIterator Queue<Item, Container>::begin (void) const
{
return m_packets.cbegin ();
}
template <typename Item, typename Container>
typename Queue<Item, Container>::Iterator Queue<Item, Container>::begin (void)
{
return m_packets.begin ();
}
template <typename Item, typename Container>
typename Queue<Item, Container>::ConstIterator Queue<Item, Container>::end (void) const
{
return m_packets.cend ();
}
template <typename Item, typename Container>
typename Queue<Item, Container>::Iterator Queue<Item, Container>::end (void)
{
return m_packets.end ();
}
template <typename Item, typename Container>
void
Queue<Item, Container>::DropBeforeEnqueue (Ptr<Item> item)

View File

@@ -115,7 +115,7 @@ WifiMacQueue::Enqueue (Ptr<WifiMacQueueItem> item)
{
NS_LOG_FUNCTION (this << *item);
return Insert (end (), item);
return Insert (GetContainer ().cend (), item);
}
bool
@@ -123,7 +123,7 @@ WifiMacQueue::PushFront (Ptr<WifiMacQueueItem> item)
{
NS_LOG_FUNCTION (this << *item);
return Insert (begin (), item);
return Insert (GetContainer ().cbegin (), item);
}
bool
@@ -140,9 +140,9 @@ WifiMacQueue::Insert (ConstIterator pos, Ptr<WifiMacQueueItem> item)
}
// the queue is full; scan the list in the attempt to remove stale packets
ConstIterator it = begin ();
ConstIterator it = GetContainer ().cbegin ();
const Time now = Simulator::Now ();
while (it != end ())
while (it != GetContainer ().cend ())
{
if (it == pos && TtlExceeded (it, now))
{
@@ -159,15 +159,15 @@ WifiMacQueue::Insert (ConstIterator pos, Ptr<WifiMacQueueItem> item)
if (m_dropPolicy == DROP_OLDEST)
{
NS_LOG_DEBUG ("Remove the oldest item in the queue");
if (pos == begin ())
if (pos == GetContainer ().cbegin ())
{
// Avoid invalidating pos
DoRemove (begin ());
pos = begin ();
DoRemove (GetContainer ().cbegin ());
pos = GetContainer ().cbegin ();
}
else
{
DoRemove (begin ());
DoRemove (GetContainer ().cbegin ());
}
}
@@ -179,7 +179,7 @@ WifiMacQueue::Dequeue (void)
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); )
{
if (!TtlExceeded (it, now))
{
@@ -209,7 +209,7 @@ WifiMacQueue::Peek (void) const
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (auto it = begin (); it != end (); it++)
for (auto it = GetContainer ().cbegin (); it != GetContainer ().cend (); it++)
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
@@ -228,9 +228,9 @@ WifiMacQueue::PeekByAddress (Mac48Address dest, Ptr<const WifiMacQueueItem> item
NS_LOG_FUNCTION (this << dest << item);
NS_ASSERT (item == nullptr || item->IsQueued ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ());
const Time now = Simulator::Now ();
while (it != end ())
while (it != GetContainer ().cend ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
@@ -254,9 +254,9 @@ WifiMacQueue::PeekByTid (uint8_t tid, Ptr<const WifiMacQueueItem> item) const
NS_LOG_FUNCTION (this << +tid << item);
NS_ASSERT (item == nullptr || item->IsQueued ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ());
const Time now = Simulator::Now ();
while (it != end ())
while (it != GetContainer ().cend ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
@@ -279,9 +279,9 @@ WifiMacQueue::PeekByTidAndAddress (uint8_t tid, Mac48Address dest, Ptr<const Wif
NS_LOG_FUNCTION (this << +tid << dest << item);
NS_ASSERT (item == nullptr || item->IsQueued ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ());
const Time now = Simulator::Now ();
while (it != end ())
while (it != GetContainer ().cend ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
@@ -306,9 +306,9 @@ WifiMacQueue::PeekFirstAvailable (const Ptr<QosBlockedDestinations> blockedPacke
NS_LOG_FUNCTION (this << item);
NS_ASSERT (item == nullptr || item->IsQueued ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ());
ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ());
const Time now = Simulator::Now ();
while (it != end ())
while (it != GetContainer ().cend ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
@@ -332,7 +332,7 @@ WifiMacQueue::Remove (void)
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); )
{
if (!TtlExceeded (it, now))
{
@@ -353,20 +353,20 @@ WifiMacQueue::Remove (Ptr<const WifiMacQueueItem> item, bool removeExpired)
{
ConstIterator next = std::next (item->m_queueIt);
DoRemove (item->m_queueIt);
return (next == end () ? nullptr : *next);
return (next == GetContainer ().cend () ? nullptr : *next);
}
const Time now = Simulator::Now ();
// remove stale items queued before the given position
ConstIterator it = begin ();
while (it != end ())
ConstIterator it = GetContainer ().cbegin ();
while (it != GetContainer ().cend ())
{
if (*it == item)
{
ConstIterator next = std::next (item->m_queueIt);
DoRemove (item->m_queueIt);
return (next == end () ? nullptr : *next);
return (next == GetContainer ().cend () ? nullptr : *next);
}
else if (!TtlExceeded (it, now))
{
@@ -402,7 +402,7 @@ WifiMacQueue::GetNPacketsByAddress (Mac48Address dest)
uint32_t nPackets = 0;
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); )
{
if (!TtlExceeded (it, now))
{
@@ -425,7 +425,7 @@ WifiMacQueue::GetNPacketsByTidAndAddress (uint8_t tid, Mac48Address dest)
uint32_t nPackets = 0;
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); )
{
if (!TtlExceeded (it, now))
{
@@ -448,7 +448,7 @@ WifiMacQueue::IsEmpty (void)
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); )
{
if (!TtlExceeded (it, now))
{
@@ -467,7 +467,7 @@ WifiMacQueue::GetNPackets (void)
const Time now = Simulator::Now ();
// remove packets that stayed in the queue for too long
for (ConstIterator it = begin (); it != end (); )
for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); )
{
if (!TtlExceeded (it, now))
{
@@ -484,7 +484,7 @@ WifiMacQueue::GetNBytes (void)
const Time now = Simulator::Now ();
// remove packets that stayed in the queue for too long
for (ConstIterator it = begin (); it != end (); )
for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); )
{
if (!TtlExceeded (it, now))
{

View File

@@ -85,8 +85,7 @@ public:
/// allow the usage of iterators and const iterators
using Queue<WifiMacQueueItem>::ConstIterator;
using Queue<WifiMacQueueItem>::Iterator;
using Queue<WifiMacQueueItem>::begin;
using Queue<WifiMacQueueItem>::end;
using Queue<WifiMacQueueItem>::GetContainer;
/**
* Set the maximum delay before the packet is discarded.

View File

@@ -480,7 +480,7 @@ OfdmaAckSequenceTest::Transmit (std::string context, WifiConstPsduMap psduMap, W
auto dev = DynamicCast<WifiNetDevice> (m_apDevice);
Ptr<WifiMacQueue> queue = dev->GetMac ()->GetQosTxop (AC_BE)->GetWifiMacQueue ();
m_flushed = 0;
for (auto it = queue->begin (); it != queue->end (); )
for (auto it = queue->GetContainer ().begin (); it != queue->GetContainer ().end (); )
{
auto tmp = it++;
if (!(*tmp)->IsInFlight ())

View File

@@ -72,7 +72,7 @@ WifiMacQueueDropOldestTest::DoRun ()
}
// Check that all elements are inserted successfully.
auto it = wifiMacQueue->begin ();
auto it = wifiMacQueue->GetContainer ().begin ();
NS_TEST_EXPECT_MSG_EQ (wifiMacQueue->GetNPackets (), 5, "Queue has unexpected number of elements");
for (uint32_t i = 5; i > 0; i--)
{
@@ -94,7 +94,7 @@ WifiMacQueueDropOldestTest::DoRun ()
packetUids.at (4) = packet->GetUid ();
// Check that front packet was replaced correctly.
it = wifiMacQueue->begin ();
it = wifiMacQueue->GetContainer ().begin ();
NS_TEST_EXPECT_MSG_EQ (wifiMacQueue->GetNPackets (), 5, "Queue has unexpected number of elements");
for (uint32_t i = 5; i > 0; i--)
{